records missing when I loop though xml php -
this question has answer here:
- retrieving children xml php 1 answer
apologies if duplicate, see other similar problems, cant mine work.
i have xml:
<?xml version="1.0" encoding="iso-8859-1"?> <quizzes> <quiz> <title>arithmetic quiz</title> <description> <text>seeing how mathematical quiz works</text> </description> <grading> <range start="0" end="49"> <grade>f</grade> <rank/> </range> <range start="50" end="60"> <grade>d</grade> <rank/> </range> <range start="60" end="69"> <grade>c</grade> <rank/> </range> <range start="70" end="79"> <grade>b</grade> <rank/> </range> <range start="80" end="100"> <grade>a</grade> <rank/> </range> </grading> <question type=""> <text>select correct value common difference:2,6,10,14,18,22</text> <option> <text>26</text> <score>5</score> <explanation> <text>correct!</text> </explanation> </option> <option> <text>18</text> <score>0</score> <explanation> <text>incorrect!</text> </explanation> </option> <option> <text>10</text> <score>0</score> <explanation> <text>incorrect!</text> </explanation> </option> </question> </quiz> </quizzes>
i able loop through , 'score' element, not getting other 'quizzes' echo'ed out.
my code looks this:
$xml=simplexml_load_file("maths.xml"); echo $xml->getname() . "<br>"; foreach($xml->children() $child) { echo $child->getname() . ": " . $child . "<br>"; }
can point me in right direction? think not traversing xml tree deep enough dont know how accomplish this.
you can use xpath()
that:
$xml=simplexml_load_file('math.xml'); var_dump($xml->xpath('//score'));
//score
select <score>
elements regardless of position in xml tree. i've google beginners article simple xml you: link
an alternative use domdocument
:
$doc = new domdocument(); $doc->load('math.xml'); foreach($doc->getelementsbytagname('score') $element) { echo $element->nodevalue; }
tutorial: link
Comments
Post a Comment