php xml help

Discussion in 'PHP' started by Ntech, Aug 6, 2009.

  1. #1
    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):
     
    Ntech, Aug 6, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    jestep, Aug 6, 2009 IP
  3. jazzbass10

    jazzbass10 Peon

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    what if it is on a seperate xml file ?
     
    jazzbass10, Aug 6, 2009 IP
  4. Ntech

    Ntech Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How would I copy a particular object like <student> into the $xml_string from an xml file?
     
    Last edited: Aug 6, 2009
    Ntech, Aug 6, 2009 IP
  5. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    premiumscripts, Aug 6, 2009 IP
  6. Ntech

    Ntech Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    Ntech, Aug 6, 2009 IP