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>

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"]');

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.