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...