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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    please help me on Dom XML

    My xml file
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
      <imgList> 
        <list path="images/ads/shop1.jpg" info="">http://www.lemon.com</list>
    	<list path="images/ads/shop2.jpg" info="">http://www.lemon.com</list>
      </imgList>
    Below is my php code to get the xml file values

    Code:
    $xml= new DOMDocument();
    $xml->preserveWhiteSpace=false;
    $xml->load('../imgList.xml');
    print_r(xmlToarray($xml));
    print_r(attToarray($xml));
    function xmlToArray($n)
    {
        $xml_array = array();
        $occurance = array();
    
         foreach($n->childNodes as $nc)
         {
             $occurance[$nc->nodeName]++;
         }
         
        foreach($n->childNodes as $nc){
            if( $nc->hasChildNodes() )
            {
                if($occurance[$nc->nodeName] > 1)
                {
                    $xml_array[$nc->nodeName][] = xmlToArray($nc);
                }
                else
                {
                    $xml_array[$nc->nodeName] = xmlToArray($nc);
                }
            }
            else
            {
                return utf8_decode($nc->nodeValue);
            }
        }
        return $xml_array;
    }
    however this code can only get the values of my xml file, i cannot get the attributes of the xml file which is important, can anybody help me?
    flying dagger

  • #2
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    I don't think I understand, do you want to get this bit:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
      <imgList> 
        <list path="images/ads/shop1.jpg" info="">http://www.lemon.com</list>
        <list path="images/ads/shop2.jpg" info="">http://www.lemon.com</list>
      </imgList>
    All of it, if so...

    Then...

    PHP Code:
    <?php
    file_get_contents
    ('yourfile.xml');
    ?>
    Would work...
    http://uk3.php.net/manual/en/functio...t-contents.php

    If you echo'd it:

    PHP Code:
    <?php
    echo file_get_contents('yourfile.xml');
    ?>
    The output would be:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
      <imgList> 
        <list path="images/ads/shop1.jpg" info="">http://www.lemon.com</list>
        <list path="images/ads/shop2.jpg" info="">http://www.lemon.com</list>
      </imgList>

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    no, the xml file is my original file

    i need to get all attributes and nodevalues into arrays, so i can work on the data
    flying dagger


  •  

    Posting Permissions

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