0

我有一个很难正确解析的 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 ( )

请帮忙。

4

1 回答 1

0

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

echo $output->asXML(); 

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

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

此外,如果您经常处理调试 simplexml,请考虑使用此功能

于 2014-11-25T08:43:32.847 回答