Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-26-2005, 01:48 PM   PM User | #1
NinjaTurtle
Regular Coder

 
Join Date: Jun 2002
Posts: 358
Thanks: 2
Thanked 0 Times in 0 Posts
NinjaTurtle is an unknown quantity at this point
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...
__________________
Thanks.
=====================================================
From NinjaTurtle
++http://ohmygoh.blogspot.com|http://technology.ohmygoh.com++

Last edited by NinjaTurtle; 07-26-2005 at 01:55 PM..
NinjaTurtle is offline   Reply With Quote
Old 07-26-2005, 03:51 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 07-26-2005, 04:36 PM   PM User | #3
NinjaTurtle
Regular Coder

 
Join Date: Jun 2002
Posts: 358
Thanks: 2
Thanked 0 Times in 0 Posts
NinjaTurtle is an unknown quantity at this point
i am using version 4.x
__________________
Thanks.
=====================================================
From NinjaTurtle
++http://ohmygoh.blogspot.com|http://technology.ohmygoh.com++
NinjaTurtle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:44 AM.


Advertisement
Log in to turn off these ads.