簡體   English   中英

PHP使用子代標記名稱中的冒號解析XML

[英]PHP Parse XML with colon in tag name using children

我有以下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語句無法獲取<media:thumbnail> url屬性? (我已經讀過PHP庫,用於使用標記名稱中的冒號來解析XML。 )這是一個不同的問題。

首先,您在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;

希望這可以幫助!

屬性不會自動與其出現的元素位於相同的名稱空間中。 如果XML包含<media:thumbnail media:url="...." /> ,則該屬性將位於當前分配了前綴media的名稱空間中,該名稱空間與thumbnail元素相同,並且您的代碼可以正常工作。

但是,XML名稱空間規范的一個怪癖是“無前綴屬性名稱的名稱空間名稱始終沒有值”。 -也就是說,沒有命名空間前綴的屬性將明確不在任何命名空間中,無論文檔中定義了什么。 (另請參見XML命名空間和非前綴屬性

在SimpleXML中,這意味着如果您已使用->children切換到特定的名稱空間,則必須使用->attributes()方法顯式切換回“ null”名稱空間以訪問屬性。 這應該工作:

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

如何解析

[英]How to parse <a name and <image src= inside <li tag using php?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM