Results 1 to 3 of 3

Thread: Problem using QDomDocument

  1. #1
    Join Date
    Jul 2013
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Problem using QDomDocument

    Hi guys, I'm newbie at everything, so please be patient.

    I'm trying to save Users data (like password and login) in a XML, reading and appending in this file whenever I need to.
    I started to use only QXMLStream to write, but then with a little research I found out that using QDomDocument would be more simple and easy.
    So, my intent is to create a XML File with data of users, and have the possibility to read, add and remove users from the xml.
    I already tried many things, the last one was write and read with QDomDocument, but I keep getting a error of ""unexpected end of file" 1 1 " when I try "doc.setContent" function. Well, I could solve that by pre-creating a XML File with just the root element, using a xmlWriter (QXmlWriteStream), but when I do that I got a wrong Xml File, like:

    (when I just initialize the xml to don't fall in to the "unexpected end of file" 1 1 " error)

    <?xml version="1.0" encoding="UTF-8"?><Users/>

    until here it's ok, I have a xml with the empty root element Users.

    When I start to add a user, and use the QDomDocument functions:

    <?xml version="1.0" encoding="UTF-8"?><Users/>
    <?xml version='1.0' encoding='UTF-8'?>
    <Users>
    <user/>
    </Users>

    As you can see, he doubles the document, dont know why, probably when I save the changes with doc->save...but I don't know other way to save changes in a file with QdomDocument

    the code:

    QDomElement root = doc->documentElement();
    qDebug()<< root.tagName();
    QDomElement user = doc->createElement("user");
    root.appendChild(user);

    QTextStream out(&database);

    doc->save(out, Indent);

    database.close();


    So that's it, I already tried many things, but none of them gave me a right xml file, even something close to that (a xml with multiple root elements, but with right data and formatting.
    Sorry for my bad english, I'm brazilian and they don't teach it very well here ;p....And if I did something wrong making this post, please warn me, I'll certainly do it right next time.

    Regards.
    Last edited by Dangelo; 30th July 2013 at 00:07.

  2. #2
    Join Date
    Jul 2013
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Problem using QDomDocument

    Hi again, I'm doing some attempts, and in the last one I think I got closer

    First exec, I look if there is already any data on the xml file, if not, I create the first user
    otherwise I insert the new user on the QMap and I read all the document, inserting the old data in the same QMap,
    then I use the QMap to rewrite a new document with all the data.

    That's work until I try to add a third user, if I try to add a third user he's not read, with the attributes blank, take a look on the exits:

    - First exec (blank xml file): <entry: asd / 123>

    <?xml version="1.0" encoding="UTF-8"?>
    <Users>
    <user>
    <conta Login="asd" Senha="123"/>
    </user>
    </Users>

    qDebug exit:
    teste if do doc "unexpected end of file" 2 1
    happytest

    - Second exec: <entry: qwe / 456>
    <?xml version="1.0" encoding="UTF-8"?>
    <Users>
    <user>
    <conta asd="123"/>
    </user>
    <user>
    <conta qwe="456"/>
    </user>
    </Users>

    qDebug exit:
    "Users"
    "user"
    "user"
    "user"
    test "conta" "asd"

    - Third exec (where the problem starts): <entry: zxc / 789>

    <?xml version="1.0" encoding="UTF-8"?>
    <Users>
    <user>
    <conta =""/>
    </user>
    <user>
    <conta zxc="789"/>
    </user>
    </Users>



    qDebug exit:
    "Users"
    "user"
    "user"
    "user"
    test "conta" ""
    "user"
    test "conta" ""


    The code:


    QString XmlModel2::addUser(User user){

    QMap<QString,QString> reader;
    QString b = "teste2";
    const int Indent = 4;
    int securityCont = 15;

    QString errorStr;
    int errorLine,errorColumn;


    QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();
    xmlWriter->setAutoFormatting(true);


    QFile database("c://Qt//Qt5.0.1//Tools//QtCreator//bin//ScracthEncrypt//xml//securitydata.xml");

    xmlWriter->setDevice(&database);


    if (!database.open(QIODevice::ReadWrite))
    {
    /* show wrror message if not able to open database */
    QMessageBox::warning(0, "Write and Ready", "O banco de senhas não abriu corretamente");
    }
    else{

    QDomDocument *doc = new QDomDocument();

    if(!doc->setContent(&database,true
    ,&errorStr,&errorLine,&errorColumn))
    {
    qDebug() << "teste if do doc" << errorStr << errorLine << errorColumn;
    }

    QDomElement root = doc->documentElement();



    if(root.isNull()){
    qDebug() << "happytest";
    xmlWriter->writeStartDocument();
    xmlWriter->writeStartElement("Users");
    xmlWriter->writeStartElement("user");
    // xmlWriter->writeAttribute("id","1");
    xmlWriter->writeStartElement("conta");
    xmlWriter->writeAttribute("Login",user.getLogin());
    xmlWriter->writeAttribute("Senha",user.getSenha());
    xmlWriter->writeEndElement();
    xmlWriter->writeEndElement();
    xmlWriter->writeEndElement();
    xmlWriter->writeEndDocument();
    reader.insert(user.getLogin(),user.getSenha());
    database.close();
    }
    else{
    reader.insert(user.getLogin(),user.getSenha());

    qDebug()<<root.tagName();
    QDomNode n = root.lastChild();

    QDomElement e ;
    e = n.toElement();
    qDebug()<<e.tagName();
    QDomElement c;

    qDebug()<<e.tagName();
    while(!n.isNull()){
    qDebug()<<e.tagName();
    c = e.firstChildElement();
    qDebug() << "test" << c.tagName() << c.attribute("Login");
    reader.insert(c.attribute("Login"),c.attribute("Se nha"));
    n = n.previousSibling();
    e = n.toElement();
    securityCont--;
    if(securityCont<0){
    qDebug()<<"Security Loop Break";
    break;
    }
    }

    QMapIterator<QString, QString> i(reader);
    database.close();
    database.remove();

    QFile databaseact("c://Qt//Qt5.0.1//Tools//QtCreator//bin//ScracthEncrypt//xml//securitydata.xml");

    if (!databaseact.open(QIODevice::ReadWrite))
    {
    /* show wrror message if not able to open database */
    QMessageBox::warning(0, "Write and Ready", "O banco de senhas não abriu corretamente");
    }
    xmlWriter->setDevice(&databaseact);
    xmlWriter->writeStartDocument();
    xmlWriter->writeStartElement("Users");
    while (i.hasNext())
    {
    i.next();
    /* create tag person */
    xmlWriter->writeStartElement("user");
    /*add one attribute and its value*/
    xmlWriter->writeStartElement("conta");
    xmlWriter->writeAttribute(i.key(), i.value());
    xmlWriter->writeEndElement();
    xmlWriter->writeEndElement();
    /*close tag person */

    }
    xmlWriter->writeEndElement();
    xmlWriter->writeEndDocument();
    databaseact.close();

    }


    }
    return b;
    }

  3. #3
    Join Date
    Jul 2013
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Problem using QDomDocument

    Quote Originally Posted by Dangelo View Post
    Hi guys, I'm newbie at everything, so please be patient.

    I'm trying to save Users data (like password and login) in a XML, reading and appending in this file whenever I need to.
    I started to use only QXMLStream to write, but then with a little research I found out that using QDomDocument would be more simple and easy.
    So, my intent is to create a XML File with data of users, and have the possibility to read, add and remove users from the xml.
    I already tried many things, the last one was write and read with QDomDocument, but I keep getting a error of ""unexpected end of file" 1 1 " when I try "doc.setContent" function. Well, I could solve that by pre-creating a XML File with just the root element, using a xmlWriter (QXmlWriteStream), but when I do that I got a wrong Xml File, like:

    (when I just initialize the xml to don't fall in to the "unexpected end of file" 1 1 " error)

    <?xml version="1.0" encoding="UTF-8"?><Users/>

    until here it's ok, I have a xml with the empty root element Users.

    When I start to add a user, and use the QDomDocument functions:

    <?xml version="1.0" encoding="UTF-8"?><Users/>
    <?xml version='1.0' encoding='UTF-8'?>
    <Users>
    <user/>
    </Users>

    As you can see, he doubles the document, dont know why, probably when I save the changes with doc->save...but I don't know other way to save changes in a file with QdomDocument

    the code:

    QDomElement root = doc->documentElement();
    qDebug()<< root.tagName();
    QDomElement user = doc->createElement("user");
    root.appendChild(user);

    QTextStream out(&database);

    doc->save(out, Indent);

    database.close();


    So that's it, I already tried many things, but none of them gave me a right xml file, even something close to that (a xml with multiple root elements, but with right data and formatting.
    Sorry for my bad english, I'm brazilian and they don't teach it very well here ;p....And if I did something wrong making this post, please warn me, I'll certainly do it right next time.

    Regards.

    I managed to solve the problem, was a silly mistake at the time of writing the keys. The adm can close the post if he wants to
    (still would be nice to know how to read and write using just QDomDocument functions)

Similar Threads

  1. Replies: 12
    Last Post: 22nd August 2012, 15:45
  2. Problem with QDomDocument
    By justin123 in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2011, 08:40
  3. Split QDomDocument to new QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 10:59
  4. QDomDocument inside other QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 16:27
  5. QDomDocument problem !!!
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2006, 23:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.