DOMXpath
TEXTや属性値の取得
// //c:name[text()="下関地方気象台"] //
// //c:MeteorologicalInfos[@type="区域予報"] //
ノード(node)
//-------------- // 時系列 雨 function get_jikeiretsu_rain($arg_xpath,$t) { $tag1 = $arg_xpath->query('//c:MeteorologicalInfos[@type="区域予報"]')->item(1); $tag2 = $arg_xpath->query(".//c:Item", $tag1)->item(1); $elements = $arg_xpath->query(".//c:WeatherPart/d:Weather", $tag2); return $elements->item($t)->nodeValue; }
ノード表示(属性値)
// //------------------------------------------- // 任意の項目のみ表示 //------------------------------------------- //echo "
"; //echo $elements->item(0)->nodeValue; //echo "
"; //echo $elements->item(7)->nodeValue; //echo $elements->item(0)->getAttribute('href'); //echo "
"; //echo $elements->getAttribute('href'); //echo "
"; //
// //------------------------------------------- // 全表示 //------------------------------------------- /* echo "
"; foreach ($elements as $node) { //echo $node; echo $node->nodeValue; //print_r($node->nodeValue); echo "
"; // 属性表示 idの中身を表示 動作 OK!! //echo $node->getAttribute('description'); //echo $node->getAttribute('condition'); //echo $node->getAttribute('type'); //echo $node->getAttribute('href'); } */ //
length
echo $xpath->query("//tag2")->length; //output 2 -> correct echo $xpath->query("//tag2", $tag1)->length; //output 2 -> wrong, the query is not relative echo $xpath->query(".//tag2", $tag1)->length; //output 1 -> correct (note the dot in front
// // 時刻フォーマット変換 return date( "n/j/H", strtotime($elements->item($t)->nodeValue)); //
参考リンク(nodeName、childnode、エトセトラ)
php DOMDocument nodeName property returning ‘#text’ with the nodeName
How get first level of dom elements by Domdocument PHP?
How do I do an XPath query on a DOMNode?
Traversing child nodes with PHP DOMXpath?
Get tag name and value of a given node using XMLReader, DOM, Xpath
hp domdocument get node value where attribute value is