2014-11-25 100 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