1

I Have a question concerning reading an XML file:

<table>
    <100000 />
    <100001 name="void" type="ref" cat="ref"/>
    <100002 name="noref" type="err" cat="ref"/>
    <100003 name="notype" type="err" cat="ref"/>
    <100004 name="nostring" type="err" cat="ref"/>
    <100005 name="noobj" type="err" cat="ref"/>
 </table>

I want to have QList of all names of the child elements (100000 - 100005), but nit the attribute values.

This is my code so far:

QList<QString> xmlActions::GetXMLID (QFile *XMLIndex)
{
   QList<QString> xList;

   if (XMLIndex->open(QIODevice::ReadOnly))
   {
        QXmlStreamReader reader (XMLIndex->readAll());
        XMLIndex->close();
        while(!reader.atEnd() && !reader.hasError())
            {
            QXmlStreamReader::TokenType token = reader.readNext();
            if(token == QXmlStreamReader::StartElement)
            {
                if(reader.name().toString() == QLatin1String("table"))
                {
                   continue;
                }

                xList << reader.name().toString();
            }
        }
   }
   return xList;
}

I only get an empy list. If I comment out this:

if(reader.name().toString() == QLatin1String("table"))
{
    continue;
}

the only thing that the list contains is 'table' (The start element). I guess this may be quite simple, but I don't get it.

leatherwolf
  • 99
  • 1
  • 6

1 Answers1

2

The file is not valid XML. An XML element's name cannot start with a number.

Quoting the official docs (XML 1.0 fifth edition) section 2.3:

[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5] Name ::= NameStartChar (NameChar)*

Since QXmlStreamReader is designed to reading well-formed XML documents only, what you're getting is technically undefined behaviour. The reader is probably just trying to cope as best as possible.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455