Jump to content

Php And Xml


Guest Signing

Recommended Posts

Guest Signing

hello, i want to get value of ONE childnode in my xml document with php.I know how to loop all through and get all the nodes. And also getting all of one specific nodes.But i cant seem to just get the value of one specific note within the document.Anyone knows how to do that?

Link to comment
Share on other sites

I've used both the SimpleXML extension and a simple regular expression to parse XML. SimpleXML works for a lot of complex data, if you only need one specific thing it will probably be faster to read the XML data as a string and use a regular expression to get what you're looking for.

Link to comment
Share on other sites

The best, least error prone and easiest way I've found is by using DOMXPath, like so:

$xml = new DOMDocument;$xml->load('file.xml');//Replace with your XML file of course$xpath = new DOMXPath($xml);echo $xpath->query('//r[1]/@d')->item(0)->nodeValue;//Selects the "d" attribute from the first "r" element in the whole document.

See the XPath tutorial for details on what to put in the "query" function.Note that this particular snippet only echoes the value of the first node that the XPath expression matches. You could instead echo the values of all nodes, or do something more than plain echo-ing of values.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...