php - 如何使用 PHP 解析 XML?

标签 php xml

我试图用此处显示的 PHP 代码解析下面的 XML,但在运行时我最终得到一个空白屏幕。我一直在 stackoverflow 上寻找类似的问题和其他类似的回复,但我仍然无法让它工作。

    <?php
    $url = 'xml-file.xml';
    $xml = simplexml_load_file($url);

    // get first book title
    $title=$xml->entry[0]->name;
    // show title
    echo $title;
    echo '<br/>';
    // get second book title
    $title=$xml->entry[1]->name;
    // show title
    echo $title;
    echo '<br/>';
    ?>

这是 XML:

   <?xml version="1.0" encoding="UTF-8"?>
    <feed gd:kind="shopping#products" gd:etag="&quot;lm_25heFT8yiumci9EH1kItJBpg/bXZf0ylJ3aJceGcE1I8C1fJbzgo&quot;" xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:s="http://www.google.com/shopping/api/schemas/2010">
     <id>tag:google.com,2010:shopping/products</id>
     <updated>2012-01-27T13:41:20.177Z</updated>
     <title>Shopping Products</title>
     <generator version="v1" uri="https://www.googleapis.com/shopping/search/">Search API for Shopping</generator>
     <link rel="alternate" type="text/html" href="https://www.googleapis.com/shopping/search/"/>
     <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.googleapis.com/shopping/search/v1/public/products?alt=atom"/>
     <link rel="self" type="application/atom+xml" href="https://www.googleapis.com/shopping/search/v1/public/products?country=US&amp;q=digital+camera&amp;alt=atom&amp;startIndex=1&amp;maxResults=25"/>
     <link rel="next" type="application/atom+xml" href="https://www.googleapis.com/shopping/search/v1/public/products?country=US&amp;q=digital+camera&amp;alt=atom&amp;startIndex=26&amp;maxResults=25"/>
     <link rel="previous" type="application/atom+xml"/>
     <openSearch:totalResults>6446790</openSearch:totalResults>
     <openSearch:startIndex>1</openSearch:startIndex>
     <openSearch:itemsPerPage>25</openSearch:itemsPerPage>


    <entry gd:kind="shopping#product">>
      <id>tag:google.com,2010:shopping/products/2860562/9400569674928563633</id>
      <author>
       <name>Amazon.com</name>
      </author>
      <published>2010-08-19T05:50:21.000Z</published>
      <updated>2012-01-26T23:54:26.000Z</updated>
      <title>Canon powershot s95 10 mp digital camera with 3.8x wide angle optical image         stabilized zoom and 3.0-inch lcd</title>
      <content type="text">desc</content>
      <link rel="alternate" type="text/html" href="https://rads.stackoverflow.com/amzn/click/com/B003ZSHNGS" rel="nofollow noreferrer"/>
      <link rel="self" type="application/atom+xml" href="https://www.googleapis.com/shopping/search/v1/public/products/2860562/gid/9400569674928563633?alt=atom"/>
      <s:product>
       <s:googleId>9400569674928563633</s:googleId>
       <s:author>
        <s:name>Amazon.com</s:name>
        <s:accountId>2860562</s:accountId>
       </s:author>
       <s:creationTime>2010-08-19T05:50:21.000Z</s:creationTime>
       <s:modificationTime>2012-01-26T23:54:26.000Z</s:modificationTime>
       <s:country>US</s:country>
       <s:language>en</s:language>
       <s:title>Canon powershot s95 10 mp digital camera with 3.8x wide angle optical image stabilized zoom and 3.0-inch lcd</s:title>
       <s:description>desc</s:description>
       <s:link>http://www.amazon.com/Canon-PowerShot-S95-Stabilized-3-0-Inch/dp/B003ZSHNGS</s:link>
       <s:brand>Canon</s:brand>
       <s:condition>new</s:condition>
       <s:gtin>00013803126556</s:gtin>
       <s:gtins>
        <s:gtin>00013803126556</s:gtin>
       </s:gtins>
       <s:inventories>
        <s:inventory channel="online" availability="inStock">
         <s:price shipping="0.0" currency="USD">340.41</s:price>
        </s:inventory>
       </s:inventories>
       <s:images>
        <s:image link="http://ecx.images-amazon.com/images/I/519z3AjKzHL._SL500_AA300_.jpg"/>
       </s:images>
      </s:product>
     </entry>

     <entry gd:kind="shopping#product">
       <id>tag:google.com,2010:shopping/products/2860562/12997829348635087420</id>
       <author>
        <name>Amazon.com</name>
       </author>
       <published>2010-09-14T13:18:12.000Z</published>
       <updated>2012-01-26T23:07:30.000Z</updated>
       <title>Canon g12 10 mp digital camera with 5x optical image stabilized zoom and 2.8 inch vari-angle lcd</title>
       <content type="text">desc</content>
       <link rel="alternate" type="text/html" href="https://rads.stackoverflow.com/amzn/click/com/B0041RSPRS" rel="nofollow noreferrer"/>
       <link rel="self" type="application/atom+xml" href="https://www.googleapis.com/shopping/search/v1/public/products/2860562/gid/12997829348635087420?alt=atom"/>
       <s:product>
        <s:googleId>12997829348635087420</s:googleId>
        <s:author>
         <s:name>Amazon.com</s:name>
         <s:accountId>2860562</s:accountId>
        </s:author>
        <s:creationTime>2010-09-14T13:18:12.000Z</s:creationTime>
        <s:modificationTime>2012-01-26T23:07:30.000Z</s:modificationTime>
        <s:country>US</s:country>
        <s:language>en</s:language>
        <s:title>Canon g12 10 mp digital camera with 5x optical image stabilized zoom and 2.8 inch vari-angle lcd</s:title>
        <s:description>desc</s:description>
        <s:link>http://www.amazon.com/Canon-G12-Digital-Stabilized-Vari-         Angle/dp/B0041RSPRS</s:link>
        <s:brand>Canon</s:brand>
        <s:condition>new</s:condition>
        <s:gtin>00013803126815</s:gtin>
        <s:gtins>
         <s:gtin>00013803126815</s:gtin>
        </s:gtins>
        <s:inventories>
         <s:inventory channel="online" availability="inStock">
          <s:price shipping="0.0" currency="USD">395.95</s:price>
         </s:inventory>
        </s:inventories>
        <s:images>
         <s:image link="http://ecx.images-amazon.com/images/I/516mn5voF0L._SL500_AA300_.jpg"/>
        </s:images>
       </s:product>
      </entry>


     </feed>

最佳答案

我在此位置的 XML 中没有看到名称

$title = $xml->entry[1]->name;

我确实看到了作者的名字:

$title = $xml->entry[1]->author->name;

我确实看到了标题

$title = $xml->entry[1]->title;

这可能就是您正在寻找的。否则,请执行以下操作,以进行调试以了解如何访问所有内容。

echo '<pre>'; 
print_r($xml->entry[1]);
echo '</pre>';

关于php - 如何使用 PHP 解析 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9034692/

相关文章:

php - 在 Magento 中更改措辞 "Special Price"和 "Regular Price"

php - 如何在 PHP 中自动找出特定 MySQL 列的数字索引?

php - Codeigniter 返回变量并检查它的值

java - 如何通过jaxb bindings.xml区分重复元素

java - 使用 Maven 将 Java Web 服务项目添加为另一个 Java 客户端项目的依赖项

asp.net - XPath 不返回节点值 ASP.NET 4.5

php - 如何将两个不同长度的数组相乘?

php - PHP 7.0 FastCGI 进程管理器启动失败

java - 使用 SimpleDateFormat 解析 svn log -xml 日期输出

java - Xerces 中缺少 JAXP 1.5 支持 - 可能的解决方法?