通过php从XML获取值

时间:2017-08-21 08:30:47

标签: php xml

所以我的XML看起来像这样

<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="ERROR">
   <Errors>
      <Error Number="1011102">
      API Key is invalid or API access has not been enabled
      </Error>
   </Errors>
   <Warnings/>
   <RequestedCommand/>
   <Server>PHX01SBAPI02</Server>
   <GMTTimeDifference>--7:00</GMTTimeDifference>
   <ExecutionTime>0</ExecutionTime>
</ApiResponse>

如何访问错误数字文字 Api Key is invalid..

我试过了:

$xml = new SimpleXMLElement($xml_string);
var_dump($xml['Errors']);
var_dump($xml->Errors->Error);

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

更新:试试这个..

$xmlArray = simplexml_load_string($xml);
echo $xmlArray->Errors->Error->attributes()->Number; //Number
echo $xmlArray->Errors->Error[0];                    //Text

答案 1 :(得分:0)

您可以像访问数组的元素一样访问属性,因此使用XML和

$xml = new SimpleXMLElement($xml_string);
echo "Number:".$xml->Errors->Error['Number'].PHP_EOL;
echo "Text:".(string)$xml->Errors->Error.PHP_EOL;

...给出

Number:1011102
Text:
      API Key is invalid or API access has not been enabled