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

per*_*ter 11 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>
        <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>
Run Code Online (Sandbox Code Playgroud)

我试图用下面的代码解析它

<?php
$xml = simplexml_load_string($res,NULL,NULL,"http://schemas.xmlsoap.org/soap/envelope/");
?>
Run Code Online (Sandbox Code Playgroud)

但是$ xml对象只包含以下内容

SimpleXMLElement Object
(
    [Header] => SimpleXMLElement Object
        (
        )

    [Body] => SimpleXMLElement Object
        (
        )

)
Run Code Online (Sandbox Code Playgroud)

Kie*_*all 32

我认为您需要使用XPath注册命名空间和访问权限.像下面这样的东西应该让你去(我没有设施来测试这个).

$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');
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

foreach($xml->xpath('//eb:MessageHeader') as $header)
{
    var_export($header->xpath('//eb:CPAId')); // Should output 'something'.
}
Run Code Online (Sandbox Code Playgroud)

您可能不需要注册命名空间,考虑它,因为它们在XML中已经存在.虽然不确定,但需要测试.

希望这可以帮助.

  • 我已经准确地使用了你所提出的建议,但是我仍然得到`array(0 => SimpleXMLElement :: __ set_state(array()),)这种结果.我不明白为什么.你能解释一下吗? (3认同)

IMS*_*SoP 8

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;
Run Code Online (Sandbox Code Playgroud)