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条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏