<i id='yrc2M'><tr id='yrc2M'><dt id='yrc2M'><q id='yrc2M'><span id='yrc2M'><b id='yrc2M'><form id='yrc2M'><ins id='yrc2M'></ins><ul id='yrc2M'></ul><sub id='yrc2M'></sub></form><legend id='yrc2M'></legend><bdo id='yrc2M'><pre id='yrc2M'><center id='yrc2M'></center></pre></bdo></b><th id='yrc2M'></th></span></q></dt></tr></i><div id='yrc2M'><tfoot id='yrc2M'></tfoot><dl id='yrc2M'><fieldset id='yrc2M'></fieldset></dl></div>

    <small id='yrc2M'></small><noframes id='yrc2M'>

  1. <tfoot id='yrc2M'></tfoot>

      <bdo id='yrc2M'></bdo><ul id='yrc2M'></ul>
    <legend id='yrc2M'><style id='yrc2M'><dir id='yrc2M'><q id='yrc2M'></q></dir></style></legend>

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

      时间:2023-05-22

          <small id='yfQVG'></small><noframes id='yfQVG'>

        • <tfoot id='yfQVG'></tfoot>
            <legend id='yfQVG'><style id='yfQVG'><dir id='yfQVG'><q id='yfQVG'></q></dir></style></legend>
              <bdo id='yfQVG'></bdo><ul id='yfQVG'></ul>
              <i id='yfQVG'><tr id='yfQVG'><dt id='yfQVG'><q id='yfQVG'><span id='yfQVG'><b id='yfQVG'><form id='yfQVG'><ins id='yfQVG'></ins><ul id='yfQVG'></ul><sub id='yfQVG'></sub></form><legend id='yfQVG'></legend><bdo id='yfQVG'><pre id='yfQVG'><center id='yfQVG'></center></pre></bdo></b><th id='yfQVG'></th></span></q></dt></tr></i><div id='yfQVG'><tfoot id='yfQVG'></tfoot><dl id='yfQVG'><fieldset id='yfQVG'></fieldset></dl></div>

                  <tbody id='yfQVG'></tbody>
              1. 本文介绍了使用具有多个命名空间的 SimpleXML 解析 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我有这个丑陋的 XML,它上面有很多命名空间,当我尝试用 simpleXML 加载它时,如果我指出第一个命名空间,我会得到一个 xml 对象,但是跟随带有其他命名空间的标签不会进入目的.

                I have this ugly XML which has alot of namespaces on it, when I try to load it with simpleXML if i indicate the first namespace I'd get an xml object ,but following tags with other namespaces would not make it to the object.

                如何解析这个 XML?

                How can I parse this 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>
                

                我正在尝试使用以下代码解析它

                im trying to parse it with the following code

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

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

                but the $xml object would only contain the following

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

                推荐答案

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

                I think you need to register the namespacing and access with XPath. Something like the following should get you going (I haven't the facility to test this).

                $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');
                

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

                Then you can do something like:

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

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

                You may not need to register the namespacing, thinking about it, as they are alredy present in the XML. Not sure on this though, would need to test.

                希望这会有所帮助.

                这篇关于使用具有多个命名空间的 SimpleXML 解析 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:SOAP-ERROR:解析 WSDL:无法加载 - 但适用于 WAMP 下一篇:致命错误:找不到“SoapClient"类

                相关文章

                最新文章

                • <bdo id='ADd94'></bdo><ul id='ADd94'></ul>

                1. <tfoot id='ADd94'></tfoot>
                2. <i id='ADd94'><tr id='ADd94'><dt id='ADd94'><q id='ADd94'><span id='ADd94'><b id='ADd94'><form id='ADd94'><ins id='ADd94'></ins><ul id='ADd94'></ul><sub id='ADd94'></sub></form><legend id='ADd94'></legend><bdo id='ADd94'><pre id='ADd94'><center id='ADd94'></center></pre></bdo></b><th id='ADd94'></th></span></q></dt></tr></i><div id='ADd94'><tfoot id='ADd94'></tfoot><dl id='ADd94'><fieldset id='ADd94'></fieldset></dl></div>

                    <small id='ADd94'></small><noframes id='ADd94'>

                  1. <legend id='ADd94'><style id='ADd94'><dir id='ADd94'><q id='ADd94'></q></dir></style></legend>