I have just started learning to program with PHP SOAPclient. I have the following xml file and I want to extract/get the value rs-1开发者_运维问答304338811289-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);
}
?>
Your simplexml namespace registration statement is broken:
Try this:
<?
$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();
}
?>
精彩评论