简体   繁体   English

使用具有多个名称空间的PHP解析XML

[英]Parse XML using PHP with multiple namespaces

I have an xml file which i am having a hard time to correctly parse: I need to retrieve the Value from the xml file 我有一个很难正确解析的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>

Below is the code that I am running: 下面是我正在运行的代码:

// 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";
}

Below is the empty object I am getting: 以下是我得到的空对象:

object (SimpleXMLElement)[3]
SimpleXMLElement Object ( )

Pls help. 请帮助。

That actually is not an empty object, and has methods against it. 那实际上不是一个空对象,并且有针对它的方法。 This is expected. 这是预期的。 If you did 如果你做了

echo $output->asXML(); 

You will see your XML string returned. 您将看到返回的XML字符串。

Now perform methods against this to iterate the data set and retrieve your specific values. 现在针对此执行方法以迭代数据集并检索您的特定值。

Also, if you're dealing with debugging simplexml frequently, consider this function 另外,如果您经常要调试simplexml,请考虑使用此功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM