私はこの simplexml 結果オブジェクトを持っています:
object(SimpleXMLElement)#207 (2) {
["@attributes"]=>
array(1) {
["version"]=>
string(1) "1"
}
["weather"]=>
object(SimpleXMLElement)#206 (2) {
["@attributes"]=>
array(1) {
["section"]=>
string(1) "0"
}
["problem_cause"]=>
object(SimpleXMLElement)#94 (1) {
["@attributes"]=>
array(1) {
["data"]=>
string(0) ""
}
}
}
}
ノード「problem_cause」が存在するかどうかを確認する必要があります。空でも結果はエラーになります。php マニュアルで、必要に応じて変更したこの php コードを見つけました。
function xml_child_exists($xml, $childpath)
{
$result = $xml->xpath($childpath);
if (count($result)) {
return true;
} else {
return false;
}
}
if(xml_child_exists($xml, 'THE_PATH')) //error
{
return false;
}
return $xml;
ノードが存在するかどうかを確認するために、xpath クエリ 'THE_PATH' の代わりに何を配置すればよいかわかりません。それとも、simplexml オブジェクトを dom に変換した方がよいでしょうか?