php – 使用具有多个命名空间的SimpleXML解析XML
发布时间:2020-12-13 16:36:55 所属栏目:PHP教程 来源:网络整理
导读:我有这个丑陋的 XML,它有很多命名空间,当我尝试加载它与simpleXML如果我指示第一个命名空间我会得到一个xml对象,但跟随其他命名空间的标签不会使它对象. 如何解析这个XML? ?xml version="1.0" encoding="UTF-8"?soap-env:Envelope xmlns:soap-env="http://s
我有这个丑陋的
XML,它有很多命名空间,当我尝试加载它与simpleXML如果我指示第一个命名空间我会得到一个xml对象,但跟随其他命名空间的标签不会使它对象.
如何解析这个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> 我试图解析它与以下代码 <?php $xml = simplexml_load_string($res,NULL,"http://schemas.xmlsoap.org/soap/envelope/"); ?> 但$xml对象只包含以下内容 SimpleXMLElement Object ( [Header] => SimpleXMLElement Object ( ) [Body] => SimpleXMLElement Object ( ) )
我想你需要注册XPath的命名空间和访问.像以下这样的东西应该让你走(我没有能力测试这个).
$xml = simplexml_load_string($res,"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中存在的alredy.不确定,但是,需要测试. 希望这可以帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |