草庐IT

php - 我如何用 PHP 解析 XML?

coder 2024-07-04 原文

<分区>

我正在尝试使用此处显示的 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>

有关php - 我如何用 PHP 解析 XML?的更多相关文章

  1. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  4. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  5. ruby-on-rails - 我更新了 ruby​​ gems,现在到处都收到解析树错误和弃用警告! - 2

    简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und

  6. ruby - 用 YAML.load 解析 json 安全吗? - 2

    我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("

  7. ruby - 如何使用 Nokogiri 解析纯 HTML 表格? - 2

    我想用Nokogiri解析HTML页面。页面的一部分有一个表,它没有使用任何特定的ID。是否可以提取如下内容:Today,3,455,34Today,1,1300,3664Today,10,100000,3444,Yesterday,3454,5656,3Yesterday,3545,1000,10Yesterday,3411,36223,15来自这个HTML:TodayYesterdayQntySizeLengthLengthSizeQnty345534345456563113003664354510001010100000344434113622315

  8. ruby-on-rails - 如何用不同的用户运行nginx主进程 - 2

    A/ctohttp://wiki.nginx.org/CoreModule#usermaster进程曾经以root用户运行,是否可以以不同的用户运行nginxmaster进程? 最佳答案 只需以非root身份运行init脚本(即/etc/init.d/nginxstart),就可以用不同的用户运行nginxmaster进程。如果这真的是你想要做的,你将需要确保日志和pid目录(通常是/var/log/nginx&/var/run/nginx.pid)对该用户是可写的,并且您所有的listen调用都是针对大于1024的端口(因为绑定(

  9. python - 帮我找到合适的 ruby​​/python 解析器生成器 - 2

    我使用的第一个解析器生成器是Parse::RecDescent,它的指南/教程很棒,但它最有用的功能是它的调试工具,特别是tracing功能(通过将$RD_TRACE设置为1来激活)。我正在寻找可以帮助您调试其规则的解析器生成器。问题是,它必须用python或ruby​​编写,并且具有详细模式/跟踪模式或非常有用的调试技术。有人知道这样的解析器生成器吗?编辑:当我说调试时,我并不是指调试python或ruby​​。我指的是调试解析器生成器,查看它在每一步都在做什么,查看它正在读取的每个字符,它试图匹配的规则。希望你明白这一点。赏金编辑:要赢得赏金,请展示一个解析器生成器框架,并说明它的

  10. ruby - 如何用递增的值填充数组 Ruby - 2

    我正在尝试解决http://projecteuler.net/problem=1.我想创建一个方法,它接受一个整数,然后创建一个包含它前面的所有整数的数组,并将整数本身作为数组中的值。以下是我目前所拥有的。代码不起作用。defmake_array(num)numbers=Array.newnumcount=1numbers.eachdo|number|numbers 最佳答案 (1..num).to_a是您在Ruby中需要做的全部。1..num将创建一个Range对象,以1开始并以任意值num结束是。Range对象有to_a方法通过

随机推荐