Php 使用DOM检索嵌套div标记中的所有元素
我是php和DOM新手,非常感谢您在这段代码中提供的帮助。这就是我目前所拥有的Php 使用DOM检索嵌套div标记中的所有元素,php,xml,class,dom,Php,Xml,Class,Dom,我是php和DOM新手,非常感谢您在这段代码中提供的帮助。这就是我目前所拥有的 Title : Thanksgiving Break 2015 Link : http://www.example.com/event/42811211 Description : Happy Holidays. Date : Wednesday, November 25, 2015 (All day), Thursday, November 26, 2015 (All day),
Title : Thanksgiving Break 2015
Link : http://www.example.com/event/42811211
Description : Happy Holidays.
Date : Wednesday, November 25, 2015 (All day),
Thursday, November 26, 2015 (All day),
Friday, November 27, 2015 (All day)
Location : Blacksburg, VA
试试这个:
<?php
$rss = simplexml_load_file('http://www.example.com/feeds/events.xml');
$html = "";
$dom = new DOMDocument(); // the HTML parser used for descriptions' HTML
foreach ($rss->channel->item as $item) {
$title = $item->title;
$link = $item->link;
$description = $item->description;
foreach ($description as $desc)
{
$dom->loadHTML($desc);
$html = simplexml_import_dom($dom)->body;
// ?????
}
$html .= "Title : $title <br /> Link : $link <br /> Description : $description <br /> Date : <br /> Location : <hr>";
}
echo $html;
?>
或
使用simplexml
,DOMDocument
和XPath
$document = new DOMDocument();
$document->load($xmlfile);
// this will also output doctype and comments at top level
foreach($document->childNodes as $node)
$result .= $document->saveXML($node)."\n";
echo $result;
}
你好谢谢你的回复。我不知道这段代码将如何实现最终结果。这里的代码不考虑嵌套的div标签,这是这里的主要问题。你介意分享一下你的逻辑吗?那就行了!非常感谢你。
$xmlfile='http://www.example.com/feeds/events.xml';
$xml = simplexml_load_file($xmlfile) or die("Error: Cannot create object");
foreach($xml->children() as $item)
{
$title = $item->title;
$link = $item->link;
$description = $item->description;
$description = $item->description;
foreach ($description as $desc)
{
$title1 = $desc->title;
$link 1 = $desc->link;
$description1 = $desc->description;
}
}
$document = new DOMDocument();
$document->load($xmlfile);
// this will also output doctype and comments at top level
foreach($document->childNodes as $node)
$result .= $document->saveXML($node)."\n";
echo $result;
}
$rss = simplexml_load_file('http://www.example.com/feeds/events.xml');
foreach($rss->channel->item as $item){
print 'Title: ' . $item->title . PHP_EOL;
print 'Link: ' . $item->link . PHP_EOL;
$dom = new DOMDocument();
$dom->loadHTML($item->description);
$xpath = new DOMXpath($dom);
$description = $xpath->query("//div[contains(@class, 'field-name-body')]");
print 'Description: ' . $description->item(0)->nodeValue . PHP_EOL;
$date = $xpath->query("//div[contains(@class, 'field-name-field-date')]");
print 'Date: ' . $date->item(0)->nodeValue . PHP_EOL;
$location = $xpath->query("//div[contains(@class, 'field-name-field-location')]/div/div");
print 'Location: ' . $location->item(0)->nodeValue . PHP_EOL;
}
/*
will output
Title: Thanksgiving Break 2015
Link: http://www.example.com/event/42811211
Description: Happy Holidays.
Date: Date: Wednesday, November 25, 2015 (All day)Thursday, November 26, 2015 (All day)Friday, November 27, 2015 (All day)
Location: Blacksburg, VA
*/