我刚开始学习用PHP SOAPclient编程。我有 以下xml文件,我想提取/获取值 RS-1304338811289-11595:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:deliverMDRecordsResponse xmlns:ns2="http://mdstore.data.dnetlib.eu/" xmlns:ns3="http://www.w3.org/2005/08/addressing">
<return>
<ns3:Address>http://129.70.212.20:8282/dnet-mdstore/service/MDStoreResultSet</ns3:Address>
<ns3:ReferenceParameters>
<ResourceIdentifier:ResourceIdentifierxmlns:ResourceIdentifier="http://www.driver.org" xmlns:wsa="http://www.w3.org/2005/08/addressing">rs-1304338811289-11595</ResourceIdentifier:ResourceIdentifier>
</ns3:ReferenceParameters>
-......
</return>
</ns2:deliverMDRecordsResponse>
</soap:Body>
</soap:Envelope>
<?php
$source = 'resourceIdentifier.xml';
$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('identifier', 'ns3');
foreach ($xml->xpath('//identifier:ResourceIdentifier') as $item)
{
print_r($item);
}
?>
答案 0 :(得分:0)
您的simplexml命名空间注册语句已损坏:
试试这个:
<?
$source=<<<END
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:deliverMDRecordsResponse xmlns:ns2="http://mdstore.data.dnetlib.eu/" xmlns:ns3="http://www.w3.org/2005/08/addressing">
<return>
<ns3:Address>http://129.70.212.20:8282/dnet-mdstore/service/MDStoreResultSet</ns3:Address>
<ns3:ReferenceParameters>
<ResourceIdentifier:ResourceIdentifier xmlns:ResourceIdentifier="http://www.driver.org" xmlns:wsa="http://www.w3.org/2005/08/addressing">r\
s-1304338811289-11595</ResourceIdentifier:ResourceIdentifier>
</ns3:ReferenceParameters>
</return>
</ns2:deliverMDRecordsResponse>
</soap:Body>
</soap:Envelope>
END;
$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('t', 'http://www.driver.org');
foreach ($xml->xpath('//t:ResourceIdentifier') as $item)
{
// print_r($item);
echo $item->asXML();
}
?>