SimpleXML with XPath
Say you have an xml that looks like the following,
<xml>
<book>
<title>Radical Things</title>
<author>Joe Foo</author>
<isbn>2345235</isbn>
</book>
<book>
<title>The Best Stuff</title>
<author>Foo Bar Jones</author>
<isbn>2342135</isbn>
</book>
<book>
<title>More Than You Know</title>
<author>Joe Foo</author>
<isbn>235236</isbn>
</book>
</xml>
<book>
<title>Radical Things</title>
<author>Joe Foo</author>
<isbn>2345235</isbn>
</book>
<book>
<title>The Best Stuff</title>
<author>Foo Bar Jones</author>
<isbn>2342135</isbn>
</book>
<book>
<title>More Than You Know</title>
<author>Joe Foo</author>
<isbn>235236</isbn>
</book>
</xml>
Let's say it's called file.xml.
Normally you would do something like,
$xml = simplexml_load_file("file.xml");
and that would give you an xml object (array) that contains all of your books.
With XPath, we can get that same xml object, but this time let's get only the books by the author Joe Foo.
$xml = simplexml_load_file("file.xml");
$xml_xpath = $xml->xpath('/xml/book[author="Joe Foo"]');
$xml_xpath = $xml->xpath('/xml/book[author="Joe Foo"]');
[Click to add or edit comments])
Please prepend comments below including a date