开发者

How to check if TAG exists inside XML with PHP?

How do i check if a TAGname exists inside an XML file.

so if i want to get

$dom->getElementsByTagName('error')

and it doesn't exist, it prints out something like an error message.

开发者_Go百科EDIT:

I'm working from an API. So what happens is when a user enters an incorrect username, another XML files is loaded that contains the tag <error>.

However, if they enter the correct username, the XML file doesn't contain the <error> tag so I'm looking for a way to check if the error tag exists inside an XML.


$dom = new DOMDocument(); 
$dom->load( 'some.xml' ); 

$errorNodes = $dom->getElementsByTagName('error'); 

if($errorNodes->length == 0)
{
  message('no error nodes'); // user defined
}


$error=false;
$AllElements = $xmlDoc->getElementsByTagName('*');
    foreach ($AllElements as $OneElement) {
    if (($OneElement->tagName)=='error') { $error = true; }
    }
if $error {
    message('error'); // user defined
}
else
{
    message('no error nodes'); // user defined
}


$errors = $dom->getElementsByTagName('error');
$exists = $errors->length > 0;

if (!$exists ) {
    echo 'Error message..';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜