Php 使用具有多个名称空间的SimpleXML解析XML

Php 使用具有多个名称空间的SimpleXML解析XML,php,soap,namespaces,simplexml,Php,Soap,Namespaces,Simplexml,我有一个丑陋的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

我有一个丑陋的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>

我认为您需要使用XPath注册名称空间和访问。像下面这样的东西应该可以让你开始(我没有测试这个的工具)

然后,您可以执行以下操作:

foreach($xml->xpath('//eb:MessageHeader') as $header)
{
    var_export($header->xpath('//eb:CPAId')); // Should output 'something'.
}
考虑一下,您可能不需要注册名称空间,因为它们已经存在于XML中。但对此不确定,需要进行测试


希望这有帮助。

那是一个肥皂信封。您可能希望使用soap客户端来抽象所有xml解析。PHP默认包含。

1)不要使用
print\r
和friends查看SimpleXML对象中的内容。有关解释和备选方案,请参见

2) SimpleXML中的命名空间支持由和方法提供

例如,您可以从节点获取PartyId,如下所示:

$from_party = (string)$xml->children('soap-env', true)->Header->children('eb', true)->MessageHeader->From->PartyId;
试试这个

   $soap_url = 'http://path/wsdl/somefile.wsdl';
   $soap_client = new SoapClient($soap_url);

   var_dump($soap_client->__getFunctions());

关于更多细节

对于其他遇到此问题的人,我挠头试图返回正确的数据,尽管最上面的答案非常接近,但我还是花了一段时间才找到答案。最终使用此页面帮助:

我相信for循环可以直接访问您需要的内容。i、 e

foreach($xml->xpath('//eb:CPAId') as $header)
{
    echo $header; // Should output 'something'.
}

简单地说,brillian您不仅帮助我解决了问题,还阐明了xpath的工作原理:)谢谢!没有必要仅仅因为您使用的是名称空间就切换到XPath。如果您使用的是XPath,那么您确实需要在上面的代码中显式注册名称空间,否则会发生故障。我使用的正是您建议的对上述输入的精确复制,但我仍然得到
数组(0=>SimpleXMLElement::__设置_状态(数组()),)
这种结果。我不明白为什么。你能解释一下吗?你帮了我很多忙,谢谢!这是对我最有用的评论,很好,你指出了打印等不起作用,并提供了指向simplexml调试的链接,道具!这些调试功能对新手非常有用。
$from_party = (string)$xml->children('soap-env', true)->Header->children('eb', true)->MessageHeader->From->PartyId;
   $soap_url = 'http://path/wsdl/somefile.wsdl';
   $soap_client = new SoapClient($soap_url);

   var_dump($soap_client->__getFunctions());
foreach($xml->xpath('//eb:CPAId') as $header)
{
    echo $header; // Should output 'something'.
}