doyrte8419 2013-11-10 10:31
浏览 147
已采纳

如何使用PHP的simplexml_load_string获取XML标记(包含名称空间)的值?

How do I get the value of "TagOne" (i.e. foo) and TagTwo (i.e. bar) from the XML below using simplexml_load_string? I'm stumped by the namespace called "ns" in the tag.

<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Body>

    <ns:ExampleInterface_Output xmlns:ns="http://example.com/interfaces">
        <ns:TagOne>Foo</ns:TagOne>
        <ns:TagTwo>Bar</ns:TagTwo>
    </ns:ExampleInterface_Output>

</SOAP-ENV:Body>

Thanks very much for your kind help!

  • 写回答

2条回答 默认 最新

  • dourunlao1642 2013-11-10 11:26
    关注

    Check this code:

    <?php
    
    $xml = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <SOAP-ENV:Body>
            <ns:ExampleInterface_Output xmlns:ns="http://example.com/interfaces">
                <ns:TagOne>Foo</ns:TagOne>
                <ns:TagTwo>Bar</ns:TagTwo>
            </ns:ExampleInterface_Output>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    XML;
    
    $xse = new SimpleXMLElement($xml);
    $exampleInterface = $xse
        ->children('SOAP-ENV', true)
        ->children('ns', true);
    
    foreach ($exampleInterface->children('ns', true) as $key => $value) {
        //Do your stuff
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?