2014-11-25 71 views
0

我有我有一个很难正确解析的XML文件: 我需要从XML文件解析XML使用PHP与多个命名空间

<?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 () 

请他LP。

回答

0

这实际上不是一个空对象,并且有反对它的方法。这是预料之中的。如果你没有

echo $output->asXML(); 

你会看到你的XML字符串返回。

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

另外,如果你正在处理调试的SimpleXML频繁,consider this function

+0

感谢更新机能的研究simplexml_tree()看起来ffod。我会探索它。我使用过“asXML”,它返回整个XML的值。我正在使用echo $ output-> Body-> PerfmonCollectCounterDataResponse-> ArrayOfCounterInfo [1] - > item-> Value ....并且出现错误尝试获取C:\ wamp \ www \ axl中非对象的属性.php。请让我知道这里的错误似乎是什么。我是XML解析的新手 – RAVI 2014-11-25 12:09:08

+0

@RAVI SOAP要求将自己的NS传递给' - > children()'方法 – Ohgodwhy 2014-11-25 16:14:04

+0

我一直在叮叮咚咚,但仍无法遍历它。要么我得到一个空值或类似的东西“警告:SimpleXMLElement :: __ toString():节点不再存在于”。有什么建议么?? – RAVI 2014-12-01 15:55:46