2013-06-03 72 views
1

xml屬性在XML輸入反應低於訪問使用PHP

<response> 
    <lst name="responseHeader"> 
    <int name="status">0</int> 
    <int name="QTime">1</int> 
    </lst> 
    <lst name="initArgs"> 
    <lst name="defaults"> 
     <str name="config">../conf/dataconfig.xml</str> 
    </lst> 
    </lst> 
    <str name="status">busy</str> 
    <str name="importResponse">A command is still running...</str> 
    <lst name="statusMessages"> 
    <str name="Time Elapsed">0:0:7.942</str> 
    <str name="Total Requests made to DataSource">1</str> 
    <str name="Total Rows Fetched">8831</str> 
    <str name="Total Documents Processed">8830</str> 
    <str name="Total Documents Skipped">0</str> 
    <str name="Full Dump Started">2013-06-03 22:15:11</str> 
    </lst> 
    <str name="WARNING"> 
    This response format is experimental. It is likely to change in the future. 
    </str> 
</response> 

我需要訪問兩個值:下面

<str name="status">busy</str> -- busy 
<str name="Total Rows Fetched">8831</str> -- 8831 

使用代碼:

$xml = simplexml_load_string($string); 
echo $xml->str[0]; 

我能夠訪問值「忙」但我不知道如何訪問「8831」

回答

0

使用XPath:

$xml = simplexml_load_string($x); // assuming XML in $x 
echo $xml->xpath("//str[@name='status']")[0]; 

,並做相同的其他價值。

+0

需要解決? –

+0

是的,它的SimpleXML,看到我的編輯。 – michi

+0

真棒解決方案....頭離 –

0

使用var_dump($ xml);來理解xml對象。閱讀示例#1解釋XML字符串例如http://php.net/manual/en/simplexmlelement.attributes.php 它將會像,

foreach($xml->foo[0]->attributes() as $a => $b) { 
     if($a=="Total Rows Fetched"){ 
     echo $b;  
     }  
    } 
+0

其發出指令仍在運行... [ –

+0

不能夠做到這一點:-( 請使用simplexml_load_string幫助 –