Php 获取多个名称空间xml中的元素
我试图在名称空间`//ns1:Location//ns1:District中获取元素Php 获取多个名称空间xml中的元素,php,xml,xpath,xml-parsing,xml-namespaces,Php,Xml,Xpath,Xml Parsing,Xml Namespaces,我试图在名称空间`//ns1:Location//ns1:District中获取元素DistrictName,但没有返回任何结果。这是我到目前为止所做的 foreach($xml1->xpath('//ns1:Venue') as $header){ $result = ($header->xpath('//ns1:Venue//ns1:Location//ns1:District//ns1:DistrictName')); // Should output 'somethi
DistrictName
,但没有返回任何结果。这是我到目前为止所做的
foreach($xml1->xpath('//ns1:Venue') as $header){
$result = ($header->xpath('//ns1:Venue//ns1:Location//ns1:District//ns1:DistrictName')); // Should output 'something'.
echo "Local2: " . (string) $result[0]. "</br>";
}
foreach($xml1->xpath('//ns1:vinge')作为$header){
$result=($header->xpath('//ns1:vinge//ns1:Location//ns1:District//ns1:DistrictName');//应该输出'something'。
回显“Local2:”(字符串)$result[0]。“”;
}
soap\u响应\u xml:
...
<ns1:Venue>
<ns1:Name>Rock</ns1:Name>
<ns1:Contact>
<ns1:Name>Rock</ns1:Name>
</ns1:Contact>
<ns1:Location>
<ns1:District>
<ns2:DistrictId>11</ns2:DistrictId>
<ns2:DistrictName>XXXXXXX</ns2:DistrictName>
</ns1:District>
<ns1:Municipaly>
<ns2:MunicipalityId>1111</ns2:MunicipalityId>
<ns2:MunicipalityName>XXXXXXXXX</ns2:MunicipalityName>
</ns1:Municipaly>
</ns1:Location>
</ns1:Venue>
。。。
摇滚乐
摇滚乐
11
XXXXXXX
1111
XXXXXXXXX
我做错了什么?如果XML是字符串,最简单的方法可能是删除名称空间:
$string = str_replace(array('ns1:', 'ns2:'), array('', ''), $string);
$xml = new SimpleXMLElement($string);
foreach($xml->xpath('//Venue') as $header){
$result = ($header->xpath('Location/District/DistrictName')); // Should output 'something'.
echo "Local2: " . (string) $result[0]. "</br>";
}
$string=str_replace(数组('ns1:','ns2:'),数组(''','),$string);
$xml=新的simplexmlement($string);
foreach($xml->xpath('//venture')作为$header){
$result=($header->xpath('Location/District/DistrictName');//应该输出'something'。
回显“Local2:”(字符串)$result[0]。“”;
}
另外:不必要时不要使用/
<代码>/表示“后代”。路径分隔符为/