View Full Version : QDomDocument doesn't accept parentheses when parsing an xml file

3rd August 2014, 05:34
When there is a parenthesis contained in a node of xml file the QDomDocument::setContent gives error "error occurred while parsing element". The error column is right where the parenthesis is. Remove all parentheses and there is no error.


QFile file(QApplication::applicationDirPath()+"/a.xml");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QDomDocument doc;

xml file:


I have googled but found no restriction to parentheses usage in xml file. And I can save nodes with parentheses successfully, with QDomDocument::save. How can I retrieve it?

3rd August 2014, 09:08
QDomDocument is telling you that the input is malformed because parentheses are not permitted in an XML element Name: http://www.w3.org/TR/REC-xml/#sec-starttags

3rd August 2014, 09:49
Well it looks like my manual does not mention that. And it's strange that the QDomDocument allows it when saving the doc to xml files.
So what should I do if the users insist putting parentheses in the element names?

3rd August 2014, 09:59
Convert them to another set of characters before passing the data to QDomDocument.

3rd August 2014, 23:38
Or train your users to correctly specify XML element names. Refuse to permit anything except allowed characters in XML strings and put up an error dialog if the user types a character that isn't allowed by the standard.