【问题标题】:PHP Parse XML with colon in tag name using childrenPHP使用子项在标签名称中使用冒号解析XML
【发布时间】:2013-09-14 00:11:54
【问题描述】:

我有以下 php 代码:

$xml=simplexml_load_file("http://openclipart.org/api/search/?query=dog&page=1");
echo "<ul>";
foreach ($xml->channel->item as $clipinfo):
  $title=$clipinfo->title;
  $full=$clipinfo->enclosure['url'];
  $thumb=$clipartinfo->children('media', true)->thumbnail['url'];

  echo "<div style=\"width:160px;height:120px;float:left;margin:5px;margin-top:10px;\">    
  <a href=\"{$full}\" target='_blank' class=\"thumbnail\">
  <img alt=\"{$title}\" style=\"width: 160px; height: 120px;\" src=\"{$thumb}\">
  </a>
  </div>";
endforeach;

这是由以下 XML 文件提供的:

<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:cc="http://web.resource.org/cc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenClipart.org</title>
    <atom:link rel="self" href="http://openclipart.org/api/search/?query=dog&amp;page=1" type="application/rss+xml"/>
    <link>http://openclipart.org</link>
    <description>Download, Sample, Cut-up, Share.</description>
    <language>en-US</language>
    <image>
    <url>http://openclipart.org/assets/images/images/logo-no-text.jpg</url>
    <link>http://openclipart.org</link>
    <title>OpenClipart.org</title>
    <height>93</height>
    <width>114</width>
    </image>
    <item>
      <title>walking dog</title>
      <link>http://openclipart.org/detail/720/walking-dog-by-johnny_automatic</link>
      <pubDate>Tue, 17 Oct 2006 21:23:37 -0400</pubDate>
      <dc:creator>johnny_automatic</dc:creator>
      <description>cartoon of a girl walking a dog from  http://www.usda.gov/cnpp/KidsPyra/National Agricultural Library, Agricultural Research Service, U. S. Department of Agriculture</description>
      <enclosure url="http://openclipart.org/people/johnny_automatic/johnny_automatic_walking_dog.svg" type="image/svg+xml" length="99841"/>
      <guid>http://openclipart.org/detail/720/walking-dog-by-johnny_automatic</guid>
      <cc:license>http://creativecommons.org/licenses/publicdomain</cc:license>
      <media:thumbnail url="http://openclipart.org/image/90px/svg_to_png/720/johnny_automatic_walking_dog.png"/>
    </item>

为什么我的 foreach 语句无法获得 &lt;media:thumbnail&gt; url 属性? (我已经阅读了PHP library for parsing XML with a colons in tag names?)这是一个不同的问题。

【问题讨论】:

    标签: php xml


    【解决方案1】:

    首先,您在foreach 循环中使用了一个未定义的变量。您已经定义了$clipinfo,但您尝试在代码中使用$clipartinfo

    其次,你错误地访问了属性:

    <media:thumbnail url="http://openclipart.org/image/foo.png"/>
    

    您正在尝试访问 URL 属性。这需要使用attributes() 方法来完成。

    变化:

    $thumb=$clipartinfo->children('media', true)->thumbnail['url'];
    

    到:

    $thumb = $clipinfo->children('media', true)->thumbnail->attributes()->url;
    

    希望这会有所帮助!

    【讨论】:

    • 这解决了整个 foreach 语句不起作用的问题。但是,变量 $thumb 仍然返回 null。
    • 漂亮!太感谢了!你是第一个也是唯一一个不粗鲁地把我送到另一个答案的人!
    • 明确地说,一般来说,['url'] 是访问属性的正确方式;当涉及命名空间时,问题就来了。 (指向的另一个答案是解释“标签名称中的冒号”实际上是一个“XML 名称空间”;一旦使用正确的术语,将更容易找到帮助。)
    【解决方案2】:

    属性不会自动与它们出现的元素在同一个命名空间中。如果 XML 包含 &lt;media:thumbnail media:url="...." /&gt;,则该属性将位于当前分配前缀 media 的命名空间中,与 thumbnail 元素相同的命名空间,并且您的代码可以正常工作。

    然而,XML 命名空间规范的一个怪癖是“无前缀属性名称的命名空间名称始终没有值”。 - 也就是说,没有命名空间前缀的属性明确不在任何命名空间中,无论文档中定义了什么。 (另见XML Namespaces and Unprefixed Attributes

    在 SimpleXML 中,这意味着如果您使用 -&gt;children 切换到特定命名空间,则必须使用 the -&gt;attributes() method 显式切换回“null”命名空间以访问该属性。这应该有效:

     $thumb = $clipinfo->children('media', true)->thumbnail->attributes(null)->url;
    

    【讨论】:

      猜你喜欢
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多