Jump to content

Php And Xml


Guest Signing
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...