SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recurisve XML parsing

    Hi there,

    Does anyone know of a good script to recursively parse an XML document visiting all nodes. I'm doing this because I'm storing a hierarchical Web site that allows users to create an remove as many pages, sections as they want. I'm using a Flex component to handle building the tree, however I then need to parse the file to create a menu, sub-sections, etc.

    An example of the XML might be:

    Code:
    <sections>
         <section title="Education">
                 <section title="Health">
                           <section title="Physical Health">
                                  <page title="Muscle Health 101" />
                           </section>
                           <section title="Mental Health">
                                  <page title="Wellness" />
                           </section>
                 </section>
                 <section title="Biology">
                          <page title="Bio 101" />
                          <page title="Bio 202" />
                 </section>
         </section>
         
    </sections>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use php's SimpleXML extension to parse the xml.
    http://www.php.net/manual/en/simplex...ples-basic.php

    Or are you asking for help writing a recursive function to traverse it?

  3. #3
    SitePoint Addict scoobasteve1982's Avatar
    Join Date
    Apr 2007
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You can use php's SimpleXML extension to parse the xml.
    http://www.php.net/manual/en/simplex...ples-basic.php

    Or are you asking for help writing a recursive function to traverse it?
    I've used SimpleXML extension before, but I'm asking help for writing a recursive function to traverse it....

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this help?
    PHP Code:
    function traverse($nodes) {
        
    // nodes will be either <page> or <section> elements
        
    foreach ($nodes as $nodetype => $node) {
            
    $num_children count($node);
            echo 
    "$nodetype title='$node[title]' num_chilren=$num_children\n";
            
            if (
    $nodetype == 'section') { // or use if $num_children > 0
                
    traverse($node);
            }
        }
    }

    traverse(new SimpleXMLElement($str)); 
    You could also just blindly call traverse() on $node if you like, because the resulting foreach loop just simply wont loop if theres no children. I added the $nodetype check just to help you get a bearing on the relationship.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •