QDomDocument::setContent reead only 1 chield but in file is more chield



  • Hi i make QDomDocument and add 2 root chields (Accouts and Tests) next i save to file. Now if i want read this form file, QDomDocument read only 1 root chield (Accounts), Why ??

    //Code
    [code]
    #include <QtCore>
    #include <QtXml>

    void save()
    {
    QDomDocument XMLDocument;

      QDomElement Accounts = XMLDocument.createElement("Accounts");
      XMLDocument.appendChild(Accounts);
    
     QDomElement Account1;
         Account1 = XMLDocument.createElement("Account");
         Account1.setAttribute("Name", "Account1");
         Accounts.appendChild(Account1);
    
         QDomElement Account2;
         Account2 = XMLDocument.createElement("Account");
         Account2.setAttribute("Name", "Account2");
         Accounts.appendChild(Account2);
    
    
      QDomElement Tests = XMLDocument.createElement("Tests");
      XMLDocument.appendChild(Tests);
    
         QDomElement Test1;
         Test1 = XMLDocument.createElement("Test");
         Test1.setAttribute("Name", "Test1");
         Tests.appendChild(Test1);
    
         QDomElement Test2;
         Test2 = XMLDocument.createElement("Test");
         Test2.setAttribute("Name", "Test2");
         Tests.appendChild(Test2);
    
    
    QFile XMLFile&#40;"d:/test.xml"&#41;;
    
    if( XMLFile.open(QIODevice::ReadWrite | QIODevice::Text) )
    {
        QTextStream tStream(&XMLFile);
        tStream <&lt; XMLDocument;
    }
    
    XMLFile.flush();
    XMLFile.close();
    

    }

    void load()
    {
    QFile XMLFile("d:/test.xml");
    QDomDocument XMLDocument;

    if(XMLFile.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        XMLDocument.setContent(&XMLFile);
        XMLFile.close();
    }
    
    qDebug() &lt;&lt; XMLDocument.childNodes().count();
    

    }

    int main()
    {
    save();
    load();

    }[/code]

    //XMLFile
    [code]
    <Accounts>
    <Account Name="Account1"/>
    <Account Name="Account2"/>
    </Accounts>
    <Tests>
    <Test Name="Test1"/>
    <Test Name="Test2"/>
    </Tests>
    [/code]


  • Moderators

    perhaps you need to do this:
    @
    XMLDocument.setContent(XMLFile.readAll());
    @

    Is "chields" in Welsh?



  • i tryed this, result its the same


  • Moderators

    OK, let's try a different thing. Sometimes, QDomDocument needs to be initialised before it can be worked upon. Try doing this:
    @
    // This will initialise the document.
    QDomElement whatever = XMLDocument.createElement("anything");
    // Now it should get the whole content
    XMLDocument.setContent(&XMLFile);
    @



  • [quote author="sierdzio" date="1383134843"]OK, let's try a different thing. Sometimes, QDomDocument needs to be initialised before it can be worked upon. Try doing this:
    @
    // This will initialise the document.
    QDomElement whatever = XMLDocument.createElement("anything");
    // Now it should get the whole content
    XMLDocument.setContent(&XMLFile);
    @[/quote]

    i tryed that but setContent overrite XMLDocument and still is 1 chields "Accouts"


  • Moderators

    Another try and I give up. Maybe the structure is a bit different and there is only one child of the doc. Use another debug line to make sure:
    @
    qDebug() << XMLDocument.toString(2);
    @



  • [code]
    qDebug() << XMLDocument.toString(2);
    [/code]

    Result:
    [code]
    <Accounts>
    <Account Name="Account1"/>
    <Account Name="Account2"/>
    </Accounts>
    [/code]


  • Moderators

    Weird, I do not know why this happens. An obvious workaround would be to add some global root item, enclosing both Accounts and Tests.



  • i do someting like this, its work on 2 chields but is suck, some one have better idea ??

    [code]
    void load()
    {
    QFile XMLFile("d:/test.xml");
    QDomDocument XMLDocument;

    if(XMLFile.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QString tContent = XMLFile.readAll();
        QDomDocument tChield;
    
        XMLDocument.setContent(tContent);
        QString tTagName = "</" + XMLDocument.childNodes().at(0).toElement().tagName() + ">";
        tContent.remove(0, tContent.indexOf(tTagName) + tTagName.length());
    
        tChield.setContent(tContent);
    
        XMLDocument.appendChild( tChield.childNodes().at(0) );
    
        qDebug() <&lt; XMLDocument.toString();
        XMLFile.close();
    }
    
    qDebug() &lt;&lt; XMLDocument.childNodes().count();
    

    }
    [/code]


Log in to reply
 

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