Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if TAG exists inside XML with PHP?

Tags:

php

xml

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.

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.

like image 805
Stephen Avatar asked Nov 28 '22 20:11

Stephen


1 Answers

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

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

if($errorNodes->length == 0)
{
  message('no error nodes'); // user defined
}
like image 143
Ewan Todd Avatar answered Dec 10 '22 09:12

Ewan Todd