15

我有这个丑陋的 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,NULL,"http://schemas.xmlsoap.org/soap/envelope/");
?>

但 $xml 对象将仅包含以下内容

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

    [Body] => SimpleXMLElement Object
        (
        )

)
4

5 回答 5

35

我认为您需要使用 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');

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

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

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

希望这可以帮助。

于 2009-04-11T21:18:52.530 回答
12

1)不要print_r和朋友一起看什么是“在”一个SimpleXML对象。有关解释和替代方案,请参阅https://github.com/IMSoP/simplexml_debug 。

2) SimpleXML 中的命名空间支持由->children()and->attributes()方法提供。

例如,您可以像这样获取 From 节点的 PartyId:

$from_party = (string)$xml->children('soap-env', true)->Header->children('eb', true)->MessageHeader->From->PartyId;
于 2012-11-12T19:32:40.680 回答
0

那是一个肥皂信封。您可能想使用一个肥皂客户端来抽象所有的 xml 解析。PHP带有一个相当不错的soap 客户端,默认包含在内。

于 2009-04-11T21:53:09.513 回答
0

对于遇到此问题的其他人,我挠头试图返回正确的数据,尽管最佳答案非常接近,但我仍然需要一段时间才能找到答案。最终使用这个页面来帮助:https ://www.w3schools.com/php/func_simplexml_registerxpathnamespace.asp

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

foreach($xml->xpath('//eb:CPAId') as $header)
{
    echo $header; // Should output 'something'.
}
于 2017-05-08T06:37:22.317 回答
-2

试试这个

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

   var_dump($soap_client->__getFunctions());

有关更多详细信息,请阅读此处

于 2013-10-21T06:56:46.723 回答