If you had an XML document that contained the data show below, how would you extract just the name element? I'm using getElementsByTagName to do it but it's retrieving both the name and the grade as a single string. <student> <name>Robert</name> <grade>A+</grade> </student> Code (markup):
Use simplexml. $xml_string = '<student> <name>Robert</name> <grade>A+</grade> </student>'; $parsed_xml = simplexml_ load_ string($xml_string); $name = (string)$parsed_xml->student->name; PHP: http://us2.php.net/manual/en/function.simplexml-load-string.php
Guys, you need to learn how to use php.net.. Go to php.net/simplexml and look through those functions. You'll see php.net/manual/en/function.simplexml-load-file.php
Okay premiumscripts, I just spent an hour reading through all the simplexml stuff at php.net and worked it out. I needed to use xpath. Simple enough once I learned about it. Thanks to both of you.