Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    Reading XML with PHP

    Is there no similar way to JS to select tags? In JavaScript it's "getElementsByTagName"...

    But anyway, main question. Why isn't this displaying the tag names inside the <menu_item> tag. So it should display "name, image, text, color".

    PHP Code:
    //Create navigation HTML from XML
        
    $xml simplexml_load_file("xml/menus.xml");
        
        foreach( 
    $xml -> children() as $child ) {
            if ( 
    $menu $child -> getName() == "menu_item" ) {
                echo 
    $menu -> children();
            }
        } 
    XML:
    PHP Code:
    <?xml version="1.0"?>
    <menu>
        <menu_item>
            <name>reptiles</name>
            <image>images/snake.gif</image>
            <text>Reptiles</text>
            <color>green</color>
        </menu_item>
    </menu>

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,997
    Thanks
    15
    Thanked 234 Times in 234 Posts
    How is this XML file generated? Is it on your server, or from another domain?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    SimpleXML is designed as such. Its flat and simple.
    If you want to manipulate the dom like JS you need to use the DOMDocument object, or write triggers for handles using XMLReader depending on if you want dom, sax or pull parsing.

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fou-Lu View Post
    SimpleXML is designed as such. Its flat and simple.
    If you want to manipulate the dom like JS you need to use the DOMDocument object, or write triggers for handles using XMLReader depending on if you want dom, sax or pull parsing.
    Any idea why my current script doesn't do what I want it to do at the moment?

    Also, the XML doc is written by me manually at the moment.

  • #5
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,269
    Thanks
    10
    Thanked 277 Times in 276 Posts
    Try it like this:
    PHP Code:
        foreach( $xml -> children() as $child ) {
         foreach (
    $child as $value) {
                echo 
    $value->getName()." => ".$value."<br>";
            }
        } 
    Dave

  • Users who have thanked tracknut for this post:

    martynball (08-15-2012)

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Nope, I don't write in SimpleXML. If I had to guess children() would be an array of collected children matching the object its pulled from, so you cannot print it directly (unless the object has overridden the toString functionality and the collection is an object that can traverse).

    If you use dom, you would be able to use getElementsByTagName as well. You can also use xPath to target the node paths.

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,997
    Thanks
    15
    Thanked 234 Times in 234 Posts
    Given all that you're said so far, I would write a php script that generated the XML file and also made whatever code you wanted produced in a usable format in another file. That way you only have to do things once.

  • #8
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by DrDOS View Post
    Given all that you're said so far, I would write a php script that generated the XML file and also made whatever code you wanted produced in a usable format in another file. That way you only have to do things once.
    What? I have hand written the XML file at the moment so I can actually write a script that can read and use it... what use is generating the exact same file at the moment, there is no need until I can actually use the data.

    One step at a time.

    --------


    And okay thanks Fou-Lu, I will look into using DOM.

  • #9
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,269
    Thanks
    10
    Thanked 277 Times in 276 Posts
    Wasn't my suggestion what you wanted to do?

  • #10
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by tracknut View Post
    Wasn't my suggestion what you wanted to do?
    Sorry, I did not see your post. That worked thanks, but I think I will look into using the DOM model instead.

  • #11
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Edit:

    Never mind, I found this on the PHP website and adapted it to work for me:

    PHP Code:
    $doc = new DOMDocument(); 

    if (
    $doc->loadXML($xml)) { 
        
    $items $doc->getElementsByTagName('item'); 
        
    $headlines = array(); 
        
        foreach(
    $items as $item) { 
            
    $headline = array(); 
            
            if(
    $item->childNodes->length) { 
                foreach(
    $item->childNodes as $i) { 
                    
    $headline[$i->nodeName] = $i->nodeValue
                } 
            } 
            
            
    $headlines[] = $headline
        } 
    My version:

    PHP Code:
    //Level 1
        
    $menu_level $xmlDoc->getElementsByTagName('menu_item');
        
        
    //Make variables
        
    $list = array();
        
        foreach (
    $menu_level as $item) {
            
    $x = array();
            
            if(
    $item->childNodes->length) {
                foreach(
    $item->childNodes as $i) {
                    
    $x[$i -> nodeName] = $i->nodeValue;
                }
            }
            
            
    $list[] = $x;
        } 
    Last edited by martynball; 08-15-2012 at 09:30 PM.


  •  

    Posting Permissions

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