QXML



  • i code doesn't work if i want to append it.....
    here is my code...

    @
    QDomDocument document;
    QDomElement root = document.createElement("main");
    document.appendChild(root);

    QDomElement book = document.createElement("book");
    book.setAttribute("Name ", ui->lineEdit->text());
    book.setAttribute("ID ",ui->lineEdit1->text());
    root.appendChild(book);
    
    
     QFile file("E:/z.xml");
    if(!file.open(QIODevice::Append | QIODevice::Text ))
    {
        qDebug() <<"Failed to open file  ";
       // return -1;
    }
    else
    {
        QTextStream stream(&file);
        stream << document.toString();
        file.close();
        qDebug() <<"finished";
    } 
    

    @

    [[add code markup, Tobias]]



  • Please wrap your code in code tags (at the beginning of the code place a @ and at the end of the code place a AT).. Otherwise we can't read it good.



  • @#
    QDomDocument document;
    QDomElement root = document.createElement("main");
    document.appendChild(root);

    QDomElement book = document.createElement("book");
    book.setAttribute("Name ", ui->lineEdit->text());
    book.setAttribute("ID ",ui->lineEdit1->text());
    root.appendChild(book);
    
    
     QFile file&#40;"E:/z.xml"&#41;;
    if(!file.open(QIODevice::Append | QIODevice::Text ))
    {
        qDebug() <<"Failed to open file  ";
       // return -1;
    }
    else
    {
        QTextStream stream(&file);
        stream << document.toString();
        file.close();
        qDebug() <<"finished";
    }@

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please enclose your code with coding tags. It wont be easily readable without them.

    What do you want to append to what ?



  • How should the XML file look like at the end (supposing, it would work)?


  • Moderators

    [quote author="moxi" date="1368779763"]i code doesn't work if i want to append it.....
    here is my code...[/quote]
    well... beside you should use code tags you also need to be clearer. What doesn't work and what do you expect. Otherwise we can just guess...



  • @SGaist i just want to append my file
    so after giving input all the data should be in the single <main> tag
    but when ever i give input my app makes a new <main> tag which is wrong..


  • Lifetime Qt Champion

    Well, your function creates a new main tag each time.

    You either have to read your xml back, append your new book to the document and write the whole thing back to the file or modify the end of the file yourself (bad idea).



  • @Sgait
    jux give me the idea how to add all the records in the same <main> tag??


  • Lifetime Qt Champion

    It depends on when you want to add the record. Are you updating your xml file or are you creating a new XML file ?



  • i am actually updating the xml file


  • Lifetime Qt Champion

    Since you're using DOM, read your XML back, search for your main element and append a new "book" child to it. Once done overwrite your file with the new content of the document.



  • Can u plz give me a code example
    or simply edit my code..


  • Lifetime Qt Champion

    • Read the xml file in a QDomDocument -> in the doc of QDomDocument
    • Get "main" element -> in the doc of QDomElement
    • Add a new element -> you already know how
    • Write the document -> overwrite the complete file


  • hmmmm let me try this


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.