我从外部网站获取以下XML代码:
<source>
<url>http://externalsite.com</url>
<widget id="1">
<title>Title1</title>
</widget>
<widget id="2">
<title>Title2</title>
</widget>
<widget id="3">
<title>Title3</title>
</widget>
<widget id="4">
<title>Title4</title>
</widget>
<widget id="5">
<title>Title5</title>
</widget>
</source>
如何获取PHP中每个小部件的id
和title
?
答案 0 :(得分:3)
$xml = simplexml_load_string( $xml);
foreach( $xml->xpath( '//widget') as $el) {
$attributes = $el->attributes();
$children = $el->children(); // OR: $el->xpath('title'); if children vary
echo $attributes['id'] . ' ' . $children[0] . "\n";
}
答案 1 :(得分:1)
答案 2 :(得分:0)