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 :)
<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.//channel/item/title/text()however this didn't work at all —//channel/item/description/table/tr/td/a[. = "[link]"][1]/@hrefSo I had to nest the foreach to access the HTML table inside XML feed.