Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Chiarimento su QDom

Chiarimento su QDom

Scheduled Pinned Locked Moved Solved Italian
3 Posts 2 Posters 783 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PollyP Offline
    PollyP Offline
    Polly
    wrote on last edited by
    #1

    Salve ,
    sto creando un programma per la creazione di un file xml.
    Solo che non capisco perchè rendendo due elementi figli di un QNodeDom io nel file xml non visualizzo i tag.

    Posto il programma

    #include <QCoreApplication>
    #include <QtXml>
    #include <QtDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QDomDocument documento;
        QDomElement radice = documento.createElement("contenitore");
    
    
        documento.appendChild(radice);
    
        QDomElement body=documento.createElement("body");
    
        QDomText testoBody=documento.createTextNode("0");
    
        body.appendChild(testoBody);
    
        QDomText testoHead=documento.createTextNode("1");
        QDomElement head = documento.createElement("head");
        head.appendChild(testoHead);
        QDomNode uno = body.firstChild();
         uno.appendChild(head);
         radice.appendChild(uno);
     
    
        QFile file("C:../FileCreato/index.xml");
        if (!file.open(QIODevice::WriteOnly|QIODevice::Text))
        {
            qDebug() << "Open the file for writing failed";
        }
        else
            {
                      QTextStream stream(&file)  ;
                        stream << documento.toString();
                        file.close();
                        qDebug() << "Writing is done";
        }
    
    
        return a.exec();
    }
    

    e qui il risultato

    <contenitore>0</contenitore>
    

    Grazie mille

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      QDomNode uno = body.firstChild();

      L'unico child di body e' testoBody quindi uno == testoBody.

      uno.appendChild(head); fallisce perche' uno e' un QDomText (puoi controllarlo con QASSERT(!uno.appendChild(head).isNull());)

      radice.appendChild(uno); crea quindi esattamente il risultato che hai.

      Sostituisci

      QDomNode uno = body.firstChild();
           uno.appendChild(head);
      radice.appendChild(uno);
      

      con

      radice.appendChild(head);
      radice.appendChild(body);
      

      P.S.
      Sei sicuro/a che vuoi usare DOM? una cosa come questa e' molto piu' semplice usando QXmlStreamWriter

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • PollyP Offline
        PollyP Offline
        Polly
        wrote on last edited by
        #3

        Ciao,
        spiegazione chiarissima.

        Fatte le modifiche che mi hai suggerito.

        Ho scelto quello solo perchè ho trovato un tutorial con cui provare.
        Purtroppo essendo molto autodidatta è difficile poter valutare la scelta delle librerie ottimali.
        Comunque provo volentieri quanto mi hai suggerito

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved