【问题标题】:how to get a specific field from an xml tag?如何从 xml 标签中获取特定字段?
【发布时间】:2014-09-09 08:43:03
【问题描述】:

我正在处理的 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>

我只想从“描述”标签中提取“img src”字段。我该如何在 php 中做到这一点?

【问题讨论】:

    标签: php html xml json


    【解决方案1】:

    您可以对 XML 使用 SimpleXML 的组合,并通过 DOMDocument 解析 HTML。示例:

    $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
    

    【讨论】:

    • ghost 的答案适用于 xml_load_string 命令..但不适用于 xml_load_file。
    • @user3863908 等等,你确定这是你的 xml 文件吗?那是完整的xml吗?还是只是其中的一部分?
    • 对不起鬼..,我的错误。你的代码工作正常。谢谢兄弟!完全有效!
    • @user3863908 兄弟很高兴它有帮助!
    • @user3863908 如果这确实帮助您勾选左侧的检查以接受答案。谢谢
    【解决方案2】:
    $xml = simplexml_load_string($xmlstring);
    $imgData=$xml->getElementsByTagName("description")[0];
    $imgString=$imgData->nodeValue;
    $explodedFirst=explode("href=", $imgString);
    $firstSplit=$explodedFirst[1];
    $explodedLast=explode("Expires", $firstSplit);
    $finalURL=$explodedLast[0];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 2020-06-13
      • 2020-07-22
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多