XmlDocument parser question

I have 2 documents, one is an invalid one (or at least i think it is invalid), the other just fine, for both cases the XML parser does not report any problems and generates a weird result for the "invalid" document, the code to test is simple and very "spaghetti like" (as Jules once said about my example code), it is done on purpose for this post:

    File goodFile     = File ("c:\\devel\\valid.xml");
    File badFile    = File ("c:\\devel\\invalid.xml");

    XmlDocument goodDocument (goodFile);
    XmlDocument badDocument (badFile);

    XmlElement *goodRootElement = goodDocument.getDocumentElement();
    XmlElement *badRootElement  = badDocument.getDocumentElement();

    if (goodRootElement != nullptr)
    {
        Logger::writeToLog ("goodRootElement is valid, has ["+String(goodRootElement->getNumChildElements())+"] children");
        Logger::writeToLog (goodRootElement->createDocument(String::empty));
        Logger::writeToLog ("parser errors: ["+goodDocument.getLastParseError()+"]");
    }
    else
    {
        Logger::writeToLog ("goodRootElement is invalid");
        Logger::writeToLog ("parser errors: ["+goodDocument.getLastParseError()+"]");
    }

    if (badRootElement != nullptr)
    {
        Logger::writeToLog ("badRootElement is valid, has ["+String(badRootElement->getNumChildElements())+"] children");
        Logger::writeToLog (badRootElement->createDocument(String::empty));
        Logger::writeToLog ("parser errors: ["+badDocument.getLastParseError()+"]");
    }
    else
    {
        Logger::writeToLog ("badRootElement is invalid");
        Logger::writeToLog ("parser errors: ["+badDocument.getLastParseError()+"]");
    }

 

Now the invalid and valid XML documents are:

<?xml version="1.0" encoding="UTF-8"?>

<root>
    <child1 property1="value" />
    <child2 -- None="invalid property">
        <subChild property="value" />
        <subChild property="value" />
    </child2>
    <child3 property3="value" />
    <child4 property4="value" />
</root>

-

<?xml version="1.0" encoding="UTF-8"?>

<root>
    <child1 property1="value" />
    <child2 property2="value" />
    <child3 property3="value" />
    <child4 property4="value" />
</root>

output from the piece of code i posted:

[debug]goodRootElement is valid, has [4] children
[debug]<?xml version="1.0" encoding="UTF-8"?>
[debug]<root>
[debug]  <child1 property1="value"/>
[debug]  <child2 property2="value"/>
[debug]  <child3 property3="value"/>
[debug]  <child4 property4="value"/>
[debug]</root>
[debug]parser errors: []
[debug]badRootElement is valid, has [5] children
[debug]<?xml version="1.0" encoding="UTF-8"?>
[debug]<root>
[debug]  <child1 property1="value"/>
[debug]  <child2/>one=&quot;invalid property&quot;&gt;
[debug]&#9;&#9;<subChild property="value"/>
[debug]  <subChild property="value"/>
[debug]</root>
[debug]parser errors: []

output from firefox for the invalid xml only:

XML Parsing Error: not well-formed
Location: file:///C:/devel/invalid.xml
Line Number 5, Column 10:    <child2 -- None="invalid property">
----------------^

output for xmlvalidator.com

5: 10 Element type "child2" must be followed by either attribute specifications, ">" or "/>"

 

My problem is that JUCE is not saying anything, it's just dropping the children of the guilty child element, so my document does not open and does not report any errors that something went wrong. I know that i should never have a property like that but i allowed Lua to interface with my program and people do weird stuff and this happens (very very rarely).

Any suggestions?

Also shouldn't there be an exception or assertion when a property with an invalid name is beeing set ? Or maybe there is one but Lua is throwing it away ?? (i know that exceptions and Lua don't go well together)

Thanks! Yes, it should have caught that, it was mainly confused by the fact that "--" is technically a valid XML identifier! I've checked in a new version now that should spot the error.

 Or maybe there is one but Lua is throwing it away ?? (i know that exceptions and Lua don't go well together)

If you've compiled Lua without exception support (I.E. as default ANSI C), and you let an unhandled exception bubble up past lua_call() it'll behave just like any other unhandled exception.  Eventually it'll reach the last chance exception handler, so you'll get to hear about it one way or another.

 

Nothing good can possibly come of allowing exceptions to escape a lua_call() though, as would be the case for passing an exception through C code in general.  With Lua, in addition to everything that leaked, you'd have the extra fun of a messed up Lua stack.

 

(though personally I fear the longjmp more than I fear the exception)