-1

I have the following xml, coming from a URL.

<rss xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Videos</title>
<link>https://www.example.com/r/videos/</link>
<description>A long description of the video.</description>
<image>...</image>
<atom:link rel="self" href="http://www.example.com/videos/.xml" type="application/rss+xml"/>
<item>
    <title>The most used Jazz lick in history.</title>
    <link>
    http://www.example.com/
    </link>
    <guid isPermaLink="true">
     http://www.example.com/
    </guid>
<pubDate>Mon, 07 Sep 2015 14:43:34 +0000</pubDate>
<description>
<table>
    <tr>
        <td>
            <a href="http://www.example.com/">
                <img src="http://www.example.com/.jpg" alt="The most used Jazz lick in history." title="The most used Jazz lick in history." />
            </a>
        </td>
        <td> submitted by 
            <a href="http://www.example.com/"> jcepiano </a>
            <br/>
            <a href="http://www.youtube.com/">[link]</a>
            <a href="http://www.example.com/">
                [508 comments]
            </a>
        </td>
    </tr>
</table>
</description>
<media:title>The most used Jazz lick in history.</media:title>
<media:thumbnail url="http://example.jpg"/>
</item>
<item>
    <title>The most used Jazz lick in history.</title>
    <link>
    http://www.example.com/
    </link>
    <guid isPermaLink="true">
     http://www.example.com/
    </guid>
<pubDate>Mon, 07 Sep 2015 14:43:34 +0000</pubDate>
<description>
<table>
    <tr>
        <td>
            <a href="http://www.example.com/">
                <img src="http://www.example.com/.jpg" alt="The most used Jazz lick in history." title="The most used Jazz lick in history." />
            </a>
        </td>
        <td> submitted by 
            <a href="http://www.example.com/"> jcepiano </a>
            <br/>
            <a href="http://www.youtube.com/">[link]</a>
            <a href="http://www.example.com/">
                [508 comments]
            </a>
        </td>
    </tr>
</table>
</description>
<media:title>The most used Jazz lick in history.</media:title>
<media:thumbnail url="http://example.jpg"/>
</item>
<item>
    <title>The most used Jazz lick in history.</title>
    <link>
    http://www.example.com/
    </link>
    <guid isPermaLink="true">
     http://www.example.com/
    </guid>
<pubDate>Mon, 07 Sep 2015 14:43:34 +0000</pubDate>
<description>
<table>
    <tr>
        <td>
            <a href="http://www.example.com/">
                <img src="http://www.example.com/.jpg" alt="The most used Jazz lick in history." title="The most used Jazz lick in history." />
            </a>
        </td>
        <td> submitted by 
            <a href="http://www.example.com/"> jcepiano </a>
            <br/>
            <a href="http://www.youtube.com/">[link]</a>
            <a href="http://www.example.com/">
                [508 comments]
            </a>
        </td>
    </tr>
</table>
</description>
<media:title>The most used Jazz lick in history.</media:title>
<media:thumbnail url="http://example.jpg"/>
</item>
<item>
    <title>The most used Jazz lick in history.</title>
    <link>
    http://www.example.com/
    </link>
    <guid isPermaLink="true">
     http://www.example.com/
    </guid>
<pubDate>Mon, 07 Sep 2015 14:43:34 +0000</pubDate>
<description>
<table>
    <tr>
        <td>
            <a href="http://www.example.com/">
                <img src="http://www.example.com/.jpg" alt="The most used Jazz lick in history." title="The most used Jazz lick in history." />
            </a>
        </td>
        <td> submitted by 
            <a href="http://www.example.com/"> jcepiano </a>
            <br/>
            <a href="http://www.youtube.com/">[link]</a>
            <a href="http://www.example.com/">
                [508 comments]
            </a>
        </td>
    </tr>
</table>
</description>
<media:title>The most used Jazz lick in history.</media:title>
<media:thumbnail url="http://example.jpg"/>
</item>
</channel>
</rss>

I want to echo every occurrence of nodeValue of title under every item and href of a with the nodeValue = "[link]" under every description under the same item.

Can you please write the full code to achieve the same? I'll then learn what every bit of code does.

[I am looking for code from performance perspective.]

I tried doing it with DomDocument, with loadXML and loadHTML, but no luck.

Here is my code below:

$url = "https://www.example.com/r/videos/.xml";
$dom = new domDocument; 
$dom->load($url); 
$dom->preserveWhiteSpace = false;
$items = $dom->getElementsByTagName('item');
$descs = $dom->getElementsByTagName('description');
foreach($items as $item){
    $title = $item->getElementsByTagName('title')->item(0)->nodeValue;
    echo $title . "<br>"; //This is echoing well

    foreach($item->getElementsByTagName('description') as $desc){

            $domH = new domDocument();
            $domH->loadHTML((string)$desc)); // here I get the error, mentioned below

            $td = $domH->getElementsByTagName('td')->item(1);
            $anchors = $td->getElementsByTagName('a')->item(1);

            echo $anchors->item(0)->getAttribute('href');
        }
}

I get the error: Catchable fatal error: Object of class DOMElement could not be converted to string in /home/thanksbelieve/public_html/vsi/trend_vids.php on line 16

I think I need a way to convert the object into string, and then it should work fine, I also tried doing saveHTML() in 2nd foreach loop before doing loadHTML((string)$desc)) but no luck.

I didn't find an easy to learn tutorial on web. Any help will be greatly appreciated.

Thanks :)

3
  • 2
    I'm sorry to see that you apparently didn't get this working after our previous discussion. Unfortunately "Can you please write the full code to achieve the same?" is not a valid question on StackOverflow. If you have a specific problem, i.e. an error of some sort, then show your code (see minimal reproducible example) and we can try to help. If you really need someone to do the job for you, many consultancy firms exist, but SO is not the place for it. Commented Sep 8, 2015 at 11:03
  • 1
    In addition to that. If you start with a new technology, start small. Take a minimal code set (i.e. just <item><title>Hello world</title></item> and expand from there. If you get stuck with the simple example, it will also be easier to ask specific questions about where you are going wrong or are having trouble, and it will be easier to answer them. Commented Sep 8, 2015 at 11:05
  • As you suggested, I have added my code, if you could help! I tried your way as well with Xquery, and this works well — //channel/item/title/text() however this didn't work at all — //channel/item/description/table/tr/td/a[. = "[link]"][1]/@href So I had to nest the foreach to access the HTML table inside XML feed. Commented Sep 8, 2015 at 15:23

1 Answer 1

1

I finally could make it work with the code below

<?php    
$url = "https://www.example.com/r/videos/.xml";
$feed_dom = new domDocument; 
$feed_dom->load($url); 
$feed_dom->preserveWhiteSpace = false;
$items = $feed_dom->getElementsByTagName('item');

foreach($items as $item){
    $title = $item->getElementsByTagName('title')->item(0)->nodeValue;
    $desc_table = $item->getElementsByTagName('description')->item(0)->nodeValue;
    echo $title . "<br>";

    $table_dom = new domDocument;
    $table_dom->loadHTML($desc_table);
    $xpath = new DOMXpath($table_dom);
    $table_dom->preserveWhiteSpace = false;
    $yt_link_node = $xpath->query("//table/tr/td[2]/a[2]");

    foreach($yt_link_node as $yt_link){

        $yt = $yt_link->getAttribute('href');
        echo $yt . "<br>";
        echo "<br>";
    }
}
?>

I thank Abel, your comments were greatly helpful to achieve the code! :)

Sign up to request clarification or add additional context in comments.

1 Comment

Glad you got it working. I had to step away, didn't see your latest comments until now. I see you found ->item(0)->nodeValue, which is indeed the way to get properties, in this case the node's value, from a list of DOM nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.