PHP XML Traversing gone awry

I'm traversing an xml file as below:

<?xml version="1.0" encoding="ISO-8859-1"?> <wrapper> <site base="" name="example 1"> <page>page1/</page> </site> <site base="" name="example 2"> <page>page2/</page> </site> </wrapper>

And my php is this:

xml = simplexml_load_file("siteList.xml"); foreach ($xml->site as $site => $child) { foreach ($xml->$site->page as $a => $page) { $endUrl = ($child["base"] . "" . $page); print_r($endUrl . "<br />"); } }

This technically works, but returns the following:

Instead of:

It's using the page from the previous loop somehow, I cannot work it out :'(

Thanks in advance!

-------------Problems Reply------------

You should be iterating over $child for the inner loop, but you are not. To fix it, change this

foreach ($xml->$site->page as $a => $page) {

to this

foreach ($child as $page) { // you don't even need the $a

You could also simplify the outer loop to foreach ($xml->site as $child) as you don't care about the key; and if you do this, renaming $child to $site would be a logical next step to improve readability.

It is using the page from the previous loop. Perhaps this change will help:

xml = simplexml_load_file("siteList.xml");
foreach ($xml->site as $site => $child) {
foreach ($site->page as $a => $page) {
$endUrl = ($child["base"] . "" . $page);
print_r($endUrl . "<br />");

Category:php Views:0 Time:2012-03-19
Tags: php xml

Related post

  • Xml traversal: Alternative to recursive patterns? 2011-03-29

    I have an XML document which needs to be traversed efficiently (recursively visit all nodes). I am not sure if I am comfortable with using recursive patterns so I thought about other patterns. Am I being too paranoid, should I just use recursive patt

  • Delphi XML traversing 2011-05-11

    I am new to Delphi. I wanted to find all the authors under each subject. Here is my XML <?xml version="1.0"?> <catalog> <subject id="computer"> <book id="bk101"> <author>Gambardella, Matthew</author> <title>X

  • XML Traversing, Copy Elements and Nodes using LINQ to XML 2009-11-15

    My XML Document is as below - <?xml version="1.0" encoding="utf-8"?> <Parents> <Parent id="A" description="A is a parent"> <Children> <ChildName name = "Son1ofA" /> <ChildName name = "Son2ofA" /> </Children>

  • jQuery XML Traversing 2010-11-12

    I have the following XML format and would like to get the list of markers for each category using jQuery. <categories> <category id="1"> <marker /> <marker /> <marker /> </category> <category id="2"> <mark

  • php DOM xml parsing geon awry 2011-10-30

    I can't seem to parse my xml today. What have I done wrong? I'm hoping someone can point me to a better/working solution. I want to parse a bus route xml file that looks like this: <buses> <bus> <num>12</num> <stime>05:0

  • Jquery XML traversing filter date range 2015-01-07

    I am trying to find multiple nodes in an XML file which have a node attribute ex. like position[validFromDate=2015-01-05] and where the date is within the range of two dates (30 day windows). In addition I need to have a logic operand OR as in "selec

  • Abap - Traversing the entire nodes of an xml document? 2011-10-04

    I want to traverse through the entire nodes of an if_ixml_document. which is the best way to do this? Please find the sample document. <text> <id> <guid auto="false"> 432543254543 </guid> </id> <title> <short_ti

  • importing an XML document 2009-08-25

    What I'm working on is a navigation bar populated by an XML document. I'm working from an existing navigation bar while I learn, and the existing bar has a menu, and when clicked each button has a sub menu. I'm trying to remove the submenu so that wh

  • Child XML tag collision jQuery 2009-09-09

    I've been using jQuery .find() to do xml traversal, and I'm finding that occasionally I have a child tag in a tree that will collide with a tag somewhere further up the tree. For instance... <?xml version="1.0" encoding='UTF-8'?> <userInfo

  • reading root element in xml not the child 2010-06-01

    Hi I am working on an application which reads XML file. Here I am trying to read "name" node under the "a"node not under the node "b". When I tried to read the root "name" node I get both nodes. Please help me to solve the problem. Bellow is the samp

  • Getting the text portion of a node using php Simple XML 2011-02-27

    Given the php code: $xml = <<<EOF <articles> <article> This is a link <link>Title</link> with some text following it. </article> </articles> EOF; function traverse($xml) { $result = ""; foreach($xml->ch

  • How to remove most namespaces from input XML in XSLT (nokogiri example) 2011-03-02

    I have this piece of nokogiri code, thats runs slower then I like on large input. How would you redo this in XSLT? Any other ideas to make it run faster? # remove namespaces (other then soapenv) from input xml, and move # them to type attribute. # xm

  • How to read data from a XML file and display it in delphi 2011-12-27

    Possible Duplicate: How to read data from xml file and display it over the text box in delphi language I am new to delphi ,and here i have an xml file called test.xml that looks like this <data> <vehicle> <type>Car</type> <

  • how to update xml file from another xml file dynamically? 2012-03-27

    I would like to update an xml file from another xml file.I have used an xml file as shown bellow: one.xml <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="" android:layout_width="fil

  • No PHP for large projects? Why not? 2008-12-21

    I've read a few posts where people have stated (not suggested, not discussed, not offered) that PHP should not be used for large projects. Being a primarily PHP developer, I ask two questions: What defines a "large project"? Why not? What are the pit

  • Fastest/shortest way to build unique tree in Ruby? 2009-10-19

    What is the fastest/shortest/one-liner (not possible :p) way to build a unique tree of elements from a tree where many of the elements are duplicated/missing in some nodes, given the tree has a defined set of nodes (which we'd use this algorithm to f

  • python search from tag 2010-06-25

    hi i need help with python programming: i need a command which can search all the words between tags from a text file. for example in the text file has <concept> food </concept>. i need to search all the words between <concept> and

  • Create unordered list dynamically using jQuery 2010-08-18

    How can I create an unordered list dynamically using jQuery? I read the image file path (href and src) from an XML file. <ul> <li><a href="images/test1.png"><img id="imageSlide" src="images/test1.png" alt="" /></a></l

  • Accessing a node-set in a JavaScript XPath query 2010-10-28

    I have a real simple question that I can't seem to find an answer to. I want to compress two XPath statements (that are getting attribute values). I learned about the | operator, hearing how it returns node sets. var getdata = xmldoc.evaluate ( '/foo

Copyright (C), All Rights Reserved.

processed in 0.216 (s). 11 q(s)