使用PHP导入和解析XML


Import and parse XML with PHP

我试图通过API将一个XML文件导入到我的php脚本中,然后该脚本将解析所述XML文件并提取一个字符串。我在网上搜索了一下答案,尽管我找到了大量的资源,但我仍然无法让这个脚本发挥作用。

我正在加载的XML文件将类似于以下

<api version="2">
  <currentTime>2012-07-28</currentTime>
    <result>
      <rowset name="accounts" key="accountID" columns="accountID,accountKey,balance">
        <row accountID="555555555" accountKey="6666" balance="7777777777.23"/>
      </rowset>
    </result>
  <cachedUntil>2012-07-28</cachedUntil>
</api>

我正在尝试让我的php脚本获取属性balance的值。这是我迄今为止整理的代码:

<?php
$apiurl = "api.some-arbitrary-api-site.com;
$xml = simplexml_load_file($apiurl);
print_r($xml);
$balance = $xml->balance;
print_r($balance);    
?>

返回:

SimpleXMLElement Object ( ) 

也遵循一些其他的网络教程,我已经尝试了这个改变

$balance = $xml->row->attributes()->balance;
print_r($balance);

它吐出

Warning: main() [function.main]: Node no longer exists in C:'xampp'htdocs'EVE'progress'import.php on line 22
Warning: main() [function.main]: Node no longer exists in C:'xampp'htdocs'EVE'progress'import.php on line 22

我做错了什么?最终的结果是页面加载这个API,每两天获取一次余额,并将数据存储在使用highchart呈现的图表中。

任何帮助都将不胜感激!感谢

Musa回答了问题

我不知道simplexml,但它不应该是$xml->result->rowset->row->attributes()->balance–Musa

我是个白痴,在输入路径时没有再次检查xml。

感谢