9

I get simple xml content from a request after a certain ID. But sometimes an ID isn't anymore available and the XML is empty.

How can I check with PHP if a xml is empty?

If someone has an idea I would appreciate if he could help me.

Thanks in advance.

Marco

3
  • 1
    What do you mean by saying it's empty? You can compare it to empty string like $xml == "" ? Or use the xml class to check if it has "children". Commented Oct 10, 2012 at 12:21
  • If it's a file you could just check the filesize Commented Oct 10, 2012 at 12:21
  • @Chevi As I understood it, a XML file could have contents, but the XML document is still empty; for example when only having a <?xml version="1.0"?> or an empty root-element. Thus the question from Ofir Baruch: how does the OP define "empty" in case of an XML document? Commented Oct 10, 2012 at 12:30

5 Answers 5

13

If you can use SimpleXML extention:

$xmlObject = new SimpleXMLElement($xmlText);
if ($xmlObject->count() == 0) {
    //it's empty
}
else {
    //XML object has children
}

Also, SimpleXML is a very handy XML-reader/editor.

Details: https://www.php.net/manual/en/book.simplexml.php

Sign up to request clarification or add additional context in comments.

1 Comment

If you want to know if a child node is empty this solution is not fit. For example <a>123</a> and <b/> both has count 0.
8

I guess it depends on what you mean by empty, if the entire document is empty, you can check if the string version of the document is empty with:

if (empty($xmlString)) 
{
    // is empty 
}

But if you're expecting a root node, you might need to check if there are any children:

$xml = simplexml_load_file("test.xml");

if (empty($xml->getName()) && count($xml->children()) == 0) { 
    // is empty
}

Hope that helps.

1 Comment

If the xml returned is blank, this will still throw an error. This is a much better solution: stackoverflow.com/a/11633904/1312282
2

just use (string) before your id and it will work. example:- your code is like this $id = $xml->mail->id;

then make it just like this $id = (string)$xml->mail->id;

it will work

Comments

2

Just to add my two cents to an old question, but the top rated answer seems to suggest using simplexml_load_file("test.xml"); and then testing that a node you expect is not empty. As this would thrown a warning level error if the file is empty, I would disagree and instead wrap the call with a test for filesize.

$xmlInfo = new SplFileInfo('test.xml');
if((int) $xmlInfo->getSize() > 0)
{
   $xml = simplexml_load_file($xmlInfo->getPath());  
}

Of course OP has asked How can I check with PHP if a xml is empty, which doesn't specify if he means node or file, but for completeness there it is.

Comments

1

Try This:

$xml_str = '<?xml version="1.0" standalone="yes"?><Name/>';
$xml = new SimpleXMLElement($xml_str);

if(empty($xml->Name[0])){
    // ITS EMPTY !! DO SOMETHING
    echo "ITS EMPTY ASJK";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.