PHP XML data missing custom attribute data -
i have custom xml of following format:
<xml> <data> <node index='0.04, 0.17, 0.30, 0.43, 1.48, 2.01, 4.23, 4.36'> node data#1 </node> <node index='0.07, 0.20, 0.33, 0.46, 1.51, 2.04, 4.27, 4.40'> node data#2 </node> <node index='0.11, 0.24, 0.37, 0.50, 1.55, 2.08, 4.30, 4.43'> node data#3 </node> </data> </xml>
i'm using following simple php code read xml file:
$file = 'test1.xml'; if(file_exists($file) ){ print "xml file found!"; $xml = file_get_contents($file) or die('<br>error loading xml file'); $xml = simplexml_load_string($xml) or die('<br>error loading xml file'); }else{ print "no xml file found!"; } if($xml){ print "<pre>"; print_r($xml); print "</pre>"; }
the output code shows simplexmlelement object xml output expected except index attributes data source xml missing. output looks following:
[data] => simplexmlelement object ( [node] => array ( [0] => node data#1 [1] => node data#2 [2] => node data#3 ) )
it's missing node index attributes data.
no, it's not inside print_r()
can access them, not missing:
$xml_string = <<<xml <xml> <data> <node index='0.04, 0.17, 0.30, 0.43, 1.48, 2.01, 4.23, 4.36'> node data#1 </node> <node index='0.07, 0.20, 0.33, 0.46, 1.51, 2.04, 4.27, 4.40'> node data#2 </node> <node index='0.11, 0.24, 0.37, 0.50, 1.55, 2.08, 4.30, 4.43'> node data#3 </node> </data> </xml> xml; $xml = simplexml_load_string($xml_string); $first_node = $xml->data->node[0]; $attrs = (string) $first_node->attributes()->index; echo $attrs;
or of course of want loop each node:
$xml = simplexml_load_string($xml_string); $nodes = $xml->data->node; foreach($nodes $node) { echo (string) $node . '<br/>'; $attrs = (string) $node->attributes()->index; echo $attrs . '<br/>'; $numbers = explode(', ', $attrs); print_r($numbers); echo '<hr/>'; }
Comments
Post a Comment