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
    Jun 2002
    Posts
    358
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to get array from XML with attribute?

    Dear,

    The following is my example of XML soure:
    Code:
    <standing>
         <team id="12" name="Renault F1" total="86">
              <gp id="4">32</gp>
              <gp id="8">10</gp>
              <gp id="10">10</gp>
         </team>
         <team id="15" name="Toyota" total="76">
               <gp id="4">0</gp>
              <gp id="8">24</gp>
              <gp id="10">26</gp>
         </team>
    </standing>
    Code:
     function fetch_xml($xml,$root_level){
    
        // Read XML File
        if(!($fp = fopen($xml, "r"))){
            die("IO Error $xml_file");
        }
        while($chunk = fread($fp,4096)){
            $xml_data .= $chunk;
        }
        
        // Do the PHP Crap
        $parser = xml_parser_create();
        xml_parse_into_struct($parser, $xml_data, &$assoc_arr, &$idx_arr);
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
        
        // Setup Some VARs
        $root_tag = $assoc_arr[$root_level]['tag'];                // Used to define starting node
        $base_level = $root_level+1;
        $base_tag = strtolower($assoc_arr[$base_level]['tag']); // Used to define base tag
        $i = 0;
        $prefix = "";
        print_r($assoc_arr);
    
        foreach($assoc_arr as $element){
            if($element['tag'] != $root_tag){
                if(!preg_match('/^s+$/', $element['value'])){
                    $tag = strtolower($element['tag']);
                    if($element['type'] == 'open' && $element['level'] > ($base_level+1)){
                        // Add Prefix's for new Tags Under Base
                        $tag_prefix[$element['level']] = $tag;
                    } elseif($element['type'] == 'close' && $element['level'] > ($base_level+1)){
                        // Remove Prefix's when tags closed
                        $tag_prefix[$element['level']] = "";
                    } elseif($element['type'] == 'complete') {
                        // Generate Prefix	
    					print_r($tag_prefix);			
                        if(count($tag_prefix) > 0){
                            $str_prefix = "";
                            foreach($tag_prefix as $prefix){
                                if(!empty($prefix))
                                $str_prefix .= $prefix."_";
                            }
                        } // End Prefix
                        // Add Our Data to the Items Array
                        $extra = "";
    					$counter1=0;
    					
    					
    						while(isset($items[$i][$str_prefix.$tag.$extra])){
    							if(empty($extra))
    								$extra = 1;
    								
    							$extra++;
    							//$counter1++;
    						} // end while					
    						$items[$i][$str_prefix.$tag.$extra] = $element['value'];
    					
                    } // end if, $element['type']
                }
                if($tag == $base_tag){
                    // Add Our Atttributes
                    if(isset($element['attributes']) && $element['type'] == 'open'){
                           $items[$i] = $element['attributes'];
                      }
                    // Start New Record
                    if($element['type'] == 'close'){
                        $i++;
                    }
                }
            }
        }
    
        return $items;
    }
    $items = fetch_xml($path,2);
    My result:
    Code:
        [0] => Array
            (
                [ID] => 12
                [NAME] => Renault F1
                [TOTAL] => 86
                [gp] => 32
                [gp2] => 10
                [gp3] => 10
            )
    
        [1] => Array
            (
                [ID] => 15
                [NAME] => Toyota
                [TOTAL] => 76
                [gp] => 0
                [gp2] => 24
                [gp3] => 26
              )
    What i want is :
    Code:
    [0] => Array
            (
                [ID] => 12
                [NAME] => Renault F1
                [TOTAL] => 86
                [gp] => Array
                     (
                       [4] => 3   // 4 is the gp's id in XML
                       [8] => 10 // 8 is the gp's id in XML
                       [10] => 10 // 10 is the gp's id in XML
                     )
    
            )
    
        [1] => Array
            (
                [ID] => 15
                [NAME] => Toyota
                [TOTAL] => 76
                [gp] => Array
                     (
                       [4] => 0
                       [8] => 24
                       [10] => 26
                     )
    
           )
    How to make it works????
    I works for this about whlole day out...
    Last edited by NinjaTurtle; 07-26-2005 at 01:55 PM.
    Thanks.
    =====================================================
    From NinjaTurtle
    ++http://ohmygoh.blogspot.com|http://technology.ohmygoh.com++

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    My recommendation for what you are doing is to either use DOM or DOMXML (depending on your php version, DOM is incorperated within the 5.x, and DOMXML is included within 4.x with libxml.dll configured).
    If you let me know of the version, I can show you how to do this a lot faster with the dom/domxml features (or, you can wait until I'm finished with my custom DOM->DOMXML class script).

    BTW, I have an exact structure for this using the DOM, so that will make it really easy if you have 5.x.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    358
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i am using version 4.x
    Thanks.
    =====================================================
    From NinjaTurtle
    ++http://ohmygoh.blogspot.com|http://technology.ohmygoh.com++


  •  

    Posting Permissions

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