SimpleXMLの使い方
SimpleXMLの使い方メモ。
CDATAなんてのはパースできないので諦める。
対象、自分のはてぶ
<?php
$xml = @simplexml_load_file('http://b.hatena.ne.jp/cockok/atomfeed');
すべてのタイトルを表示する
foreach ($xml->entry as $entry) {
var_dump($entry->title);
}
すべてのタイトルを表示する(XPath)
$xml->registerXPathNamespace('atom', 'http://purl.org/atom/ns#');
foreach($xml->xpath('//atom:title') as $title) {
var_dump($title);
}
すべてのタグを表示する
foreach ($xml->entry as $entry) {
foreach ($entry->children('http://purl.org/dc/elements/1.1/') as $subject) {
var_dump($subject);
}
}
すべてのタグを表示する(XPath)
foreach($xml->xpath('//dc:subject') as $subject) {
var_dump($subject);
}
すべてのlink=alternateを表示(XPath)
$xml->registerXPathNamespace('atom', 'http://purl.org/atom/ns#');
foreach($xml->xpath('//atom:link[@rel="alternate"]') as $link) {
var_dump($link);
}
エントリーごとにtitleとlink=alternateのhrefを表示
foreach ($xml->entry as $entry) {
$entry->registerXPathNamespace('atom', 'http://purl.org/atom/ns#');
var_dump($entry->title);
foreach ($entry->xpath('atom:link[@rel="alternate"]') as $link) {
var_dump($link['href']);
}
}
カテゴリー: 日記