simple xml how to read node without a namespace?
I have an xml string in php that I want to put into an array. How do I get ttt:tree and ttt:car using simple xml?
<?xmlversion="1.0"encoding="utf-8"?>
<soap:Envelope xmlns:soap="htp://schemas.xmlsoap.org/soap/envelope/"xmlns:xsi="htp://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="htp://www.w3.org/2001/XMLSchema">
<soap:Body>
<a xmlns="www.ttt.com">
<b xmlns:ttt="www.ttt.com">
<ttt:tree>big</ttt:tree>
<ttt:car>small</ttt:car>
</b>
</a>
</soap:Bo开发者_JS百科dy>
</soap:Envelope>
$simpleXML = new SimpleXMLElement($x);
//the hard way
foreach($simpleXML->children('soap',true)
->children('www.ttt.com')
->children('www.ttt.com')
->children('www.ttt.com') as $nodename => $nodevalue){
echo $nodename.':'.$nodevalue.PHP_EOL;
}
//or direct:
echo $simpleXML->children('soap',true)
->children('www.ttt.com')
->children('www.ttt.com')
->children('www.ttt.com')->tree;
//the less hard way
$simpleXML->registerXPathNamespace('wantthisone','www.ttt.com');
$trees = $simpleXML->xpath('//wantthisone:tree');
echo $trees[0];
$cars = $simpleXML->xpath('//wantthisone:car');
echo $cars[0];
精彩评论