【问题标题】:simpleXML loop entriessimpleXML 循环条目
【发布时间】:2011-12-19 06:45:34
【问题描述】:

我有下面的xml

    <entry>
        <id></id>
        <title></title>
        <dc:identifier></dc:identifier>
        <dc:identifier></dc:identifier>
        <dc:relation></dc:relation>
        <dc:publisher></dc:publisher>
        <dc:language xsi:type="dcterms:ISO639-2"></dc:language>
        <dcterms:issued></dcterms:issued>
        <updated></updated>
        <rights></rights>
        <author>
            <name></name>
            <uri></uri>
        </author>
        <link rel="alternate" href="" type="text/html"/>
        <link rel="http://opds-spec.org/image" href="" type="image/jpeg"/>
        <link rel="http://opds-spec.org/image/thumbnail" href="" type="image/jpeg"/>
        <link rel="http://opds-spec.org/acquisition/buy" href="" type="application/epub+zip">
            <opds:price currencycode="EUR"></opds:price>
        </link>
        <summary type="text"></summary>
</entry>
<entry>
        <id></id>
        <title></title>
        <dc:identifier></dc:identifier>
        <dc:identifier></dc:identifier>
        <dc:relation></dc:relation>
        <dc:publisher></dc:publisher>
        <dc:language xsi:type="dcterms:ISO639-2"></dc:language>
        <dcterms:issued></dcterms:issued>
        <updated></updated>
        <rights></rights>
        <author>
            <name></name>
            <uri></uri>
        </author>
        <link rel="alternate" href="" type="text/html"/>
        <link rel="http://opds-spec.org/image" href="" type="image/jpeg"/>
        <link rel="http://opds-spec.org/image/thumbnail" href="" type="image/jpeg"/>
        <link rel="http://opds-spec.org/acquisition/buy" href="" type="application/epub+zip">
            <opds:price currencycode="EUR"></opds:price>
        </link>
        <summary type="text"></summary>
</entry>
......

解析所有项目时我可以很好地解析它(simpleXML)

现在我想同时获取一个项目。所以我将一个 var 传递给我的文件,例如

file.php?start=1
file.php?start=2
etc

我试图访问当前项目

$xml->entry[$start] returns NULL

 $xml->entry->{$start} returns NULL

但两者都返回 NULL

我尝试直接访问它

$xml->entry[1]
$xml->entry->{1}

它的工作正常

我错过了什么吗?

【问题讨论】:

    标签: php simplexml


    【解决方案1】:

    $_GET['start'] 可能不是正确的变量类型,请尝试将类型转换为整数。

    【讨论】:

      【解决方案2】:
      <?php
      $xml = simplexml_load_file('simplexml.php');
      
      $id = '1';
      print_r($xml->entry[$id]);
      ?>
      

      结果:

      <?php
      $xml = simplexml_load_file('simplexml.php');
      
      $id = '1';
      $id = (int)$id;
      
      print_r($xml->entry[$id]);
      
      ?>
      

      结果:

      SimpleXMLElement 对象 ( [入口] => SimpleXMLElement 对象 ( [id] => 2 [title] => 另一个标题 ) )

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 2013-03-23
        • 2018-08-04
        • 2013-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多