【问题标题】:PHP SimpleXML + Get AttributePHP SimpleXML + 获取属性
【发布时间】:2012-05-19 05:47:39
【问题描述】:

我正在阅读的 XML 如下所示:

<show id="8511">

    <name>The Big Bang Theory</name>
    <link>http://www.tvrage.com/The_Big_Bang_Theory</link>
    <started>2007-09-24</started>
    <country>USA</country>

    <latestepisode>
        <number>05x23</number>
        <title>The Launch Acceleration</title>
    </latestepisode>

</show>

要获得(例如)最新剧集的编号,我会这样做:

$ep = $xml->latestepisode[0]->number;

这很好用。但是我该怎么做才能从&lt;show id="8511"&gt; 获取 ID?

我尝试过类似的方法:

$id = $xml->show;
$id = $xml->show[0];

但没有任何效果。

更新

我的代码sn-p:

$url    = "http://services.tvrage.com/feeds/episodeinfo.php?show=".$showName;
$result = file_get_contents($url);
$xml = new SimpleXMLElement($result);

//still doesnt work
$id = $xml->show->attributes()->id;

$ep = $xml->latestepisode[0]->number;

echo ($id);

奥利。 XML:

http://services.tvrage.com/feeds/episodeinfo.php?show=The.Big.Bang.Theory

【问题讨论】:

标签: php xml simplexml


【解决方案1】:

这应该可行。

$id = $xml["id"];

您的 XML 根成为 SimpleXML 对象的根;您的代码正在调用名为“show”的 chid 根,但该名称不存在。

您也可以使用此链接获取一些教程:http://php.net/manual/en/simplexml.examples-basic.php

【讨论】:

  • 这似乎可以解决问题!非常感谢! (下次我可能应该花更多时间看示例:()
【解决方案2】:

你需要使用attributes

我相信这应该可行

$id = $xml->show->attributes()->id;

【讨论】:

  • 这行不通...show 是默认根...他需要正确地扭曲xml
  • 不幸的是,我不断收到Warning: main() [function.main]: Node no longer exists in....
【解决方案3】:

这应该可以。 您需要使用带类型的属性(如果 sting 值使用(字符串))

$id = (string) $xml->show->attributes()->id;
var_dump($id);

或者这个:

$id = strip_tags($xml->show->attributes()->id);
var_dump($id);

【讨论】:

    【解决方案4】:

    你需要使用attributes()来获取属性。

    $id = $xml->show->attributes()->id;
    

    您也可以这样做:

    $attr = $xml->show->attributes();
    $id = $attr['id'];
    

    或者你可以试试这个:

    $id = $xml->show['id'];
    

    查看对您问题的编辑(&lt;show&gt; 是您的根元素),试试这个:

    $id = $xml->attributes()->id;
    

    $attr = $xml->attributes();
    $id = $attr['id'];
    

    $id = $xml['id'];
    

    【讨论】:

    • 第一个和第二个例子不幸输出Warning: main() [function.main]: Node no longer exists in....,而最后一个根本没有显示任何东西。
    • 它有效,如果您也编辑了@Sam 的答案,请告诉我,因为我会更改正确的答案并现在选择您的。
    • @Andrej:不,Sam 的回答从一开始就是这样:-P 我刚刚编辑了格式。
    【解决方案5】:

    试试这个

    $id = (int)$xml->show->attributes()->id;
    

    【讨论】:

      【解决方案6】:

      您需要正确格式化您的XML 并让它具有例如使用&lt;root&gt;&lt;/root&gt;&lt;document&gt;&lt;/document&gt; 任何东西.. 请参阅http://php.net/manual/en/function.simplexml-load-string.php 的XML 规范和示例

      $xml = '<?xml version="1.0" ?> 
      <root>
      <show id="8511">
          <name>The Big Bang Theory</name>
          <link>http://www.tvrage.com/The_Big_Bang_Theory</link>
          <started>2007-09-24</started>
          <country>USA</country>
      
          <latestepisode>
              <number>05x23</number>
              <title>The Launch Acceleration</title>
          </latestepisode>
      
      </show>
      </root>';
      
      $xml = simplexml_load_string ( $xml );
      var_dump ($xml->show->attributes ()->id);
      

      【讨论】:

      • 不幸的是,我对 xml 的格式没有任何影响!
      • 我在帖子中添加了原始 XML 源代码!
      【解决方案7】:

      使用 SimpleXML objecto 正确加载 xml 文件后,您可以执行 print_r($xml_variable) 并轻松找到可以访问的属性。正如其他用户所说,$xml['id'] 也为我工作。

      【讨论】:

        猜你喜欢
        • 2018-08-04
        • 2017-03-14
        • 2012-10-03
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        • 1970-01-01
        相关资源
        最近更新 更多