1

外部サイトから取得した次の 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 で各ウィジェットのidandを取得するにはどうすればよいですか?title

4

3 に答える 3

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";
}
于 2012-07-10T12:56:05.277 に答える
1

SimpleXMLxpath(). _

于 2012-07-10T12:54:17.343 に答える
0

詳細については、 http://www.php.net/manual/en/simplexml.examples-basic.phpを確認してください。

于 2012-07-10T12:53:22.727 に答える