gpt4 book ai didi

php - 简单的xml如何在没有命名空间的情况下读取节点?

转载 作者:行者123 更新时间:2023-12-04 17:04:48 25 4
gpt4 key购买 nike

我在 php 中有一个 xml 字符串,我想将它放入一个数组中。
如何使用简单的 xml 获取 ttt:tree 和 ttt:car?

<?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:Body>
</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];

关于php - 简单的xml如何在没有命名空间的情况下读取节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6865540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com