duancan9815 2014-09-09 08:43
浏览 120
已采纳

如何从xml标签获取特定字段?

The xml i'm working on looks like this:

     <item>
     <title>$39.99 and Under Juniors' Swimwear</title>
     <link>http://www.amazon.com/s/ref=xs_gb_rss_A1RFRNENBWTVO4/?rh=n:1036592,n:!2334084011,n:!2334146011,n:8021415011,p_6:ATVPDKIKX0DER&amp;bbn=8021415011&amp;ie=UTF8&amp;qid=1398271335&amp;rnid=15683531&amp;ccmID=380205&amp;tag=bugash-20</link>
     <description>&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;a rel="nofollow" target="_blank" href="http://www.amazon.com/s/ref=xs_gb_rss_A1RFRNENBWTVO4/?rh=n:1036592,n:!2334084011,n:!2334146011,n:8021415011,p_6:ATVPDKIKX0DER&amp;bbn=8021415011&amp;ie=UTF8&amp;qid=1398271335&amp;rnid=15683531&amp;ccmID=380205&amp;tag=rssfeeds-20"&gt;&lt;img src="http://ecx.images-amazon.com/images/I/31kwUz5PiZL._SL160_.jpg" alt="Product Image" style='border:0;'/&gt;&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;tr&gt;&lt;td&gt;$39.99 and Under Juniors' Swimwear&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Expires May 10, 2014&lt;/td&gt;&lt;/tr&gt;&lt;/tr&gt;&lt;/table&gt;</description>
     <guid isPermaLink="false">http://promotions.amazon.com/gp/goldbox/5159412---5opWoFoLiIfWceLGIhXzm2wwCMk=</guid>
     <pubDate>Sat, 26 Apr 2014 07:00:00 +0000</pubDate>
  </item>

i want to extract just the 'img src' field from the 'description' tag.how do i do that in php.?

  • 写回答

2条回答 默认 最新

  • dpda53918 2014-09-09 08:50
    关注

    You can use a combination of SimpleXML for the XML and parse the HTML thru DOMDocument. Example:

    $xml_string = <<<XML
    <item>
         <title>$39.99 and Under Juniors' Swimwear</title>
         <link>http://www.amazon.com/s/ref=xs_gb_rss_A1RFRNENBWTVO4/?rh=n:1036592,n:!2334084011,n:!2334146011,n:8021415011,p_6:ATVPDKIKX0DER&amp;bbn=8021415011&amp;ie=UTF8&amp;qid=1398271335&amp;rnid=15683531&amp;ccmID=380205&amp;tag=bugash-20</link>
         <description>&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;a rel="nofollow" target="_blank" href="http://www.amazon.com/s/ref=xs_gb_rss_A1RFRNENBWTVO4/?rh=n:1036592,n:!2334084011,n:!2334146011,n:8021415011,p_6:ATVPDKIKX0DER&amp;bbn=8021415011&amp;ie=UTF8&amp;qid=1398271335&amp;rnid=15683531&amp;ccmID=380205&amp;tag=rssfeeds-20"&gt;&lt;img src="http://ecx.images-amazon.com/images/I/31kwUz5PiZL._SL160_.jpg" alt="Product Image" style='border:0;'/&gt;&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;tr&gt;&lt;td&gt;$39.99 and Under Juniors' Swimwear&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Expires May 10, 2014&lt;/td&gt;&lt;/tr&gt;&lt;/tr&gt;&lt;/table&gt;</description>
         <guid isPermaLink="false">http://promotions.amazon.com/gp/goldbox/5159412---5opWoFoLiIfWceLGIhXzm2wwCMk=</guid>
         <pubDate>Sat, 26 Apr 2014 07:00:00 +0000</pubDate>
      </item>
    XML;
    
    $xml = simplexml_load_string($xml_string); // or simplexml_load_file('path/to/file.xml');
    $description = (string) $xml->description;
    
    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($description);
    libxml_clear_errors();
    $src = $dom->getElementsByTagName('img')->item(0)->getAttribute('src');
    echo $src; // http://ecx.images-amazon.com/images/I/31kwUz5PiZL._SL160_.jpg
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?