我从API请求中获取xml,并想知道如何在PHP中将<soap:Body>
到</soap:Body>
部分转换为数组。在我的搜索中,我没有看到任何简单或合乎逻辑的方法。
任何可以指向正确方向的信息都会很棒!
提前致谢。
示例XML:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<wsa:Action>http://www.urlhere.com/</wsa:Action>
<wsa:MessageID>urn:uuid:00000000-0000-0000-0000-000000000000</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:00000000-0000-0000-0000-000000000000</wsa:RelatesTo>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsse:Security>
<wsu:Timestamp wsu:Id="Timestamp-4cf61b60-957a-454a-a828-90bd373301af">
<wsu:Created>2001-01-01T00:00:00Z</wsu:Created>
<wsu:Expires>2001-01-01T00:00:00Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soap:Header>
<soap:Body>
<blah xmlns="http://www.urlhere.com/schema">
<blahblah>
<blahblahblah>0</blahblahblah>
</blahblah>
</blah>
</soap:Body>
</soap:Envelope>
我希望这样做
array('body'=>array('blah'=>array('blahblah'=>array('blahblahblah'=>0))))
答案 0 :(得分:1)
如果您要使用SimpleXML http://www.php.net/manual/en/book.simplexml.php
您可以在命名空间元素上创建一个匹配的xpath。
$simple = simplexml_load_string($xml);
$result = $simple->xpath('//soap:Body');
echo $result[0]->blah->blahblah->blahblahblah;
我不会出现&#34;数组&#34;作为复杂XML结构的解决方案需要不同类型的处理/迭代,所有这些都由simpleXML文档覆盖。
添加:添加名称空间消耗的更好示例
$simple = simplexml_load_string($xml);
$simple->registerXPathNamespace('foo','http://www.urlhere.com/schema');
$result = $simple->xpath('//foo:blah');
echo $result[0]->blahblah->blahblahblah;