【问题标题】:Parse XML using PHP with multiple namespaces使用具有多个命名空间的 PHP 解析 XML
【发布时间】:2014-11-25 08:34:56
【问题描述】:

我有一个很难正确解析的 xml 文件: 我需要从 xml 文件中检索值

<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soapenv:Body>
            <ns1:PerfmonCollectCounterDataResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://schemas.cisco.com/ast/soap/">
                <ArrayOfCounterInfo soapenc:arrayType="ns1:CounterInfoType[2]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
                    <item xsi:type="ns1:CounterInfoType">
                        <Name xsi:type="ns1:CounterNameType">\\172.16.106.18\Number of Replicates Created and State of Replication(ReplicateCount)\Number of Replicates Created</Name>
                        <Value xsi:type="xsd:long">603</Value>
                        <CStatus xsi:type="xsd:unsignedInt">1</CStatus>
                    </item>
                    <item xsi:type="ns1:CounterInfoType">
                        <Name xsi:type="ns1:CounterNameType">\\172.16.106.18\Number of Replicates Created and State of Replication(ReplicateCount)\Replicate_State</Name>
                        <Value xsi:type="xsd:long">2</Value>
                        <CStatus xsi:type="xsd:unsignedInt">1</CStatus>
                    </item>
                </ArrayOfCounterInfo>
            </ns1:PerfmonCollectCounterDataResponse>
        </soapenv:Body>
    </soapenv:Envelope>

下面是我正在运行的代码:

// fclose($fopen);
// if (file_exists($filename)) {    
    // $output = simplexml_load_file($filename,null,null,"http://www.w3.org/2001/XMLSchema-instance",true);  // I have also tried without the namespaces
    ;
    //$output = simplexml_load_string($xmldata);

    //var_dump($output);
    //print_r($output);     
} else {
    echo "File not found";
}

下面是我得到的空对象:

object (SimpleXMLElement)[3]
SimpleXMLElement Object ( )

请帮忙。

【问题讨论】:

    标签: php xml soap cisco-axl


    【解决方案1】:

    这实际上不是一个空对象,并且有针对它的方法。这是意料之中的。如果你这样做了

    echo $output->asXML(); 
    

    您将看到返回的 XML 字符串。

    现在对此执行方法以迭代数据集并检索您的特定值。

    另外,如果你经常调试 simplexml,consider this function

    【讨论】:

    • 感谢更新功能 simplexml_tree() 看起来 ffod。我会探索它。我使用了“asXML”,它返回整个 XML 的值。我正在使用 echo $output->Body->PerfmonCollectCounterDataResponse->ArrayOfCounterInfo[1]->item->Value .... 我收到错误 Trying to get property of non-object in C:\wamp\www\axl .php请让我知道这里似乎有什么错误。我是 XML 解析的新手
    • @RAVI SOAP 要求将它自己的 NS 传递给 -&gt;children() 方法
    • 我一直在努力解决这个问题,但仍然无法遍历它。我得到一个空值或类似“警告:SimpleXMLElement::__toString():节点不再存在”之类的东西。有什么建议吗??
    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 2014-06-14
    • 2016-09-22
    • 2012-08-15
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多