Search simplexml_load_file() object

Status
Not open for further replies.

ziycon

New Member
How can I search the object from simplexml_load_file() to pull out certain nodes? Haven't had any luck searching for help on it
 

php.allstar

New Member
If you provided an example of the file you have and what you want to pull out I could give you an exact answer, so please feel free to post an example file and what you want out of it.

With simpleXML it really is simple to iterate over an xml file or string and pull elements from nodes. However if you are using SimpleXML to do this, you're better using "SimpleXML xpath" do a google for xpath queries to get more info on this.

But the best way to do this is to use "DomXPath" (google it!) as its supposed to be more powerful than SimpleXMLXPath
 

ziycon

New Member
I'll have a look at them now, basically what I'm doing is reading in an XML file with information about release dates like the example below and I want to order based on date descending.

I figure if I read them into an array I could sort them that way and then display them, maybe this is what xpath could be used for??

Code:
<?xml version="1.0" encoding="UTF-8"?>

<releases-structure>
    <releases>
        <release>
            <title>test</title>
            <date>01/01/2010</date>
            <description>test description 100</description>
        </release>
        <release>
            <title>test 2</title>
            <date>02/01/2010</date>
            <description>test data 56730 test</description>
        </release>
    </releases>
</releases-structure>
Code:
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

foreach ($xml->releases->release as $release) {
      print $release->title.' - '.$release->date.'<br/>';
}
 

ziycon

New Member
I'm trying the below code, not sure if I'm understanding xpath correctly or not?
Code:
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

foreach ($xml->releases->release as $release) {
    $data .= $release->xpath("date");

    print_r($data);
}
 
Status
Not open for further replies.
Top