2014-09-09 2 views
1

내가 같은 외모에 일하고 있어요 XML :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> 

내가 PHP에서 그렇게 할 tag.how '설명'에서 바로 'IMG의 SRC'필드를 추출합니다.?

답변

0

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 
+0

유령에 의해 대답은 xml_load_file의 xml_load_string 아니 command..but 노력하고 있습니다. –

+0

@ user3863908 기다려주세요. XML 파일입니까? 그게 완벽한 XML이야? 아니면 그 중 일부일까요? – Ghost

+0

죄송합니다 유령 .. 내 실수. 코드가 잘 작동합니다. 감사합니다. 완전히 작동합니다! –

0
$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]; 
관련 문제