使用具有多个命名空间的SimpleXMLparsingXML
我有这个丑陋的XML,其上有很多命名空间,当我试图用simpleXML加载时,如果我指出第一个命名空间,我会得到一个XML对象,但下面的标签与其他命名空间不会使其对象。
我怎样才能parsing这个XML?
<?xml version="1.0" encoding="UTF-8"?> <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Header> <eb:MessageHeader xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="1.0" soap-env:mustUnderstand="1"> <eb:From> <eb:PartyId eb:type="URI">wscompany.com</eb:PartyId> </eb:From> <eb:To> <eb:PartyId eb:type="URI">mysite.com</eb:PartyId> </eb:To> <eb:CPAId>something</eb:CPAId> <eb:ConversationId>moredata.com</eb:ConversationId> <eb:Service eb:type="compXML">theservice</eb:Service> <eb:Action>theaction</eb:Action> <eb:MessageData> <eb:MessageId>a certain messageid</eb:MessageId> <eb:Timestamp>2009-04-11T18:43:58</eb:Timestamp> <eb:RefToMessageId>mid:areference</eb:RefToMessageId> </eb:MessageData> </eb:MessageHeader> <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext"> <wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">an impresive binary security toekn</wsse:BinarySecurityToken> </wsse:Security> </soap-env:Header> <soap-env:Body> <SessionCreateRS xmlns="http://www.opentravel.org/OTA/2002/11" version="1" status="Approved"> <ConversationId>the goodbye token</ConversationId> </SessionCreateRS> </soap-env:Body> </soap-env:Envelope> 即时尝试parsing它与下面的代码
 <?php $xml = simplexml_load_string($res,NULL,NULL,"http://schemas.xmlsoap.org/soap/envelope/"); ?> 
但$ xml对象只包含以下内容
 SimpleXMLElement Object ( [Header] => SimpleXMLElement Object ( ) [Body] => SimpleXMLElement Object ( ) ) 
	
我认为你需要用XPath注册命名空间和访问。 像下面的东西应该让你去(我没有设施来testing这个)。
 $xml = simplexml_load_string($res, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/"); $xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/'); $xml->registerXPathNamespace('eb', 'http://www.ebxml.org/namespaces/messageHeader'); $xml->registerXPathNamespace('wsse', 'http://schemas.xmlsoap.org/ws/2002/12/secext'); 
然后你可以做一些事情:
 foreach($xml->xpath('//eb:MessageHeader') as $header) { var_export($header->xpath('//eb:CPAId')); // Should output 'something'. } 
您可能不需要注册名称空间,思考它,因为它们在XML中是存在的。 不知道这个,但是,需要testing。
希望这可以帮助。
  1)不要使用print_r和朋友来查看SimpleXML对象中的“什么”。 请参阅https://github.com/IMSoP/simplexml_debug以获取解释和替代方法。; 
  2)SimpleXML中的名称空间支持由->children()和->attributes()方法提供。 
例如,你可以像这样获得From节点的PartyId:
 $from_party = (string)$xml->children('soap-env', true)->Header->children('eb', true)->MessageHeader->From->PartyId; 
这是一个肥皂信封。 您可能想要使用soap-client将所有xmlparsing分离出来。 PHP 提供了一个相当不错的soap客户端,作为默认包含。
对于遇到这个问题的其他人,我搔着头试图返回正确的数据,虽然最高的答案非常接近,但仍然花了我一段时间才find答案。 最终使用此页面来帮助: https : //www.w3schools.com/php/func_simplexml_registerxpathnamespace.asp
我相信for循环可以直接访问你需要的东西。 即
 foreach($xml->xpath('//eb:CPAId') as $header) { echo $header; // Should output 'something'. } 
尝试这个
  $soap_url = 'http://path/wsdl/somefile.wsdl'; $soap_client = new SoapClient($soap_url); var_dump($soap_client->__getFunctions()); 
更多细节请阅读