1

I use the following function to validate XML coming from a Web API before trying to parse it:

function isValidXML($xml) {
    $doc = @simplexml_load_string($xml);
    if ($doc) {
        return true;
    } else {
        return false;
    }
}

For some reason, it fails on the following XML. While it's a bit light in content, it looks valid to me.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><connection-response-list xmlns="http://www.ca.com/spectrum/restful/schema/response" />

Why would this fail? I tried another method of validate the XML that used DOMDocument and libxml_get_errors(), but it was actually more fickle.

EDIT: I should mention that I'm using PHP 5.3.8.

8
  • Remove the @ and see what the parser says. Commented Aug 26, 2014 at 20:05
  • What message do you get when you are not suppressing the error? I am not 100% certain, but it might be the self closing tag. Every XML document I've worked with in SimpleXML I have always had a root element with opened/closed tag. Commented Aug 26, 2014 at 20:06
  • Hey @bart-friederichs, I tried it and the results were the same. To be clear, the validation function works brilliantly on larger chunks of XML and has done so 10s of 1000s of times. It's just this short chunk of XML that makes it unhappy. Commented Aug 26, 2014 at 20:09
  • @Crackertastic, It just outputs a blank line. Not very helpful. Commented Aug 26, 2014 at 20:13
  • @JonA Is error reporting enabled? If $doc is evaluating to false due to simplexml_load_string($xml), then there has to be some kind of reason for that which should be getting kicked out by the parser. Commented Aug 26, 2014 at 20:14

2 Answers 2

3

I think your interpretation is just wrong here – var_dump($doc) should give you

object(SimpleXMLElement)#1 (0) {
}

– but since it is an “empty” SimpleXMLElement, if($doc) considers it to be false-y due to PHP’s loose type comparison rules.

You should be using

if ($doc !== false)

here – a type-safe comparison.

(Had simplexml_load_string actually failed, it would have returned false – but it didn’t, see var_dump output I have shown above, that was tested with exactly the XML string you’ve given.)

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

Comments

0

SimpleXML wants some kind of "root" element. A self-closing tag at the root won't cut it.

See the following code when a root element is added:

<?php

function isValidXML($xml)
{
    $doc = @simplexml_load_string($xml);
    if ($doc) {
        return true;
    } else {
        return false;
    }
}

var_dump(isValidXML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root><connection-response-list xmlns="http://www.ca.com/spectrum/restful/schema/response" /></root>'));

// returns true

print_r(isValidXML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root><connection-response-list xmlns="http://www.ca.com/spectrum/restful/schema/response" /></root>'));

// returns 1

?>

Hope that helps.

5 Comments

Well, except I don't get to generate the XML. I'm pretty sure a self-closing root tag is valid XML. Ideas?
@JonA Depends on how you want to interpret the W3C specification, but for me, no I would say a self-closed root tag doesn't do it. Might be software that will parse it, but doesn't mean it would be considered "well formed".
It is well-formed, and that the root element is self-closing is not the issue here – see my answer please.
@CBroe +1 to you. Good call on PHP's truthy/falsy system. That hadn't occured to me right away. Although, on a side note, I still remember SimpleXML crying at me once for loading a document with no root element (had a series of self-closed tags). That is what prompted my answer. I thought it was the lack of a parent root element.
Yes, every XML document needs a root element, and there can be only one element at root level – otherwise it would not be a well-formed XML document, and therefor SimpleXML would be right in “crying” at you ;-)

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.