Important: Please read the Qt Code of Conduct -

XML Parser giving error if xml filename is only numbers.

  • I am using the following function for parsing an xml file.
    @QDomElement readallxml::parseXML(QString fileName)
    QDomElement rootTag;
    QDomDocument docum;
    parseOK = false;
    QFile *file = new QFile(fileName);
    if(!(file->open(QIODevice::ReadOnly | QIODevice::Text)))
    return rootTag;
    QString errorStr;
    int errorLine;
    int errorColumn;
    if (!docum.setContent(file, false, &errorStr, &errorLine,
    &errorColumn)) {
    qDebug() << "Error: Parse error at line " << errorLine << ", "
    << "column " << errorColumn << ": "<< qPrintable(errorStr);
    return rootTag;
    //Get the root element
    rootTag = docum.documentElement();
    return rootTag;

    If the name of my xml file is say abc.xml then this routine works great. However, if my filename is 123.xml then setContent gives an error...Error: Parse error at line 1 , column 11 : letter is expected (as printed by qdebug)

    I tried to find out if there is any rule that xml filename should start with an alphabet, but could not locate any. If I change the filename and the corresponding document element names from 123.xml to a123.xml, then I have no error.
    Is there a better way to ensure that the numerical name file be parsed properly?

  • Qt Champions 2017

    There is no rule specified as file should start with 123.xml. Filename is coming from OperatingSystem. It is failing at setContent. It means that is parsing issue at the file. Can you paste the sample file which you are using ?

  • Thanks Dheerendra for your reply.

    The xml file contents are as below. The name of this xml file is 520.xml
    <!DOCTYPE 520>

    Now here, if I change manually from 520 to a520 this gets parsed without any problem. But the 520 does not get parsed.

  • Qt Champions 2017

    Please look at the second rule. It is because of name starting with numbers.

    XML Naming Rules

    XML elements must follow these naming rules:

    Names can contain letters, numbers, and other characters
    Names cannot start with a number or punctuation character
    Names cannot start with the letters xml (or XML, or Xml, etc)
    Names cannot contain spaces

  • OH. Thanks Dheerendra
    I was using the XML format to save my design files in the software. So in that case, I might have to switch over to some other format. Cannot lay rules to a user not to use numbers as the starting character for the design files.

    If you dont mind, any suggestions for any alternate format for saving files?

  • Qt Champions 2017

    File name can be saved in number format. XML element name should not start with numbers. In your file you have 520 as XML tag. According to the rules tag name starting with number is not allowed. Hope it is clear.

  • OK I got it. Thanks Dheerendra.

Log in to reply