Important: Please read the Qt Code of Conduct -

QDomDocument add setAttribute in especific order

  • I'm having troubles with setAttribute from the QDomDocuments, I can't add the attributes in the order I'd like to. I can't figure out how to do it right

    @void XMLParse::AddSingleProduct(QString mes, QString pro, QString value){
    QDomDocument tmp("tmp");
    QDomNodeList Item = MonthNode(); // this place me into the <Meses>
    // int i = GetMonthIndexByName(mes); //
    // QDomNode q =; //

    QDomNode q =;
    QDomElement aaaa = tmp.createElement("Item");


    aaaa.setAttribute("Nombre", pro);
    aaaa.setAttribute("Costo", value);

    this->Save(); // this save the xml file


    here is the xml in which I'm working on:

    @<?xml version='1.0'?>
    <Temp Year="2006">

    no matter how or where I do the .setAttibute, it always add them in a differente order, I need to add this attributes in a especific order, please help

  • Moderators

    AFAIK you can't do that with QDomDocument. The XML spec says nothing about order of the attributes (or rather says it's "not significant"), so it's an implementation detail in QDomDocument.
    You might have better luck with the QXmlStreamWriter if that's an option for you.

  • should I use just QXmlStream or both(QXmlStream* & QDomDocument), to read, edit add delete, bla bla, .... from an xml file?

    I'm very new with Qt and I don't know wehere I'm.

  • Moderators

    There are basically two ways to parse or write XMLs.

    One is DOM - this basically creates a tree of xml nodes and allows you to manipulate them - this one is handled by QDomDocument.

    The other one is SAX - it's an incremental parser. It reads(or writes) tag by tag without knowing surrounding nodes. This is handled by QXmlStreamReader/Writer.

    They don't mix. Choose one or the other.
    Here's an example for the writer: "Detailed Description":

Log in to reply