Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDomDocument::setContent reead only 1 chield but in file is more chield
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 4.1k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Pablik2005
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      Is "chields" in Welsh?

      (Z(:^

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pablik2005
        wrote on last edited by
        #3

        i tryed this, result its the same

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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);
          @

          (Z(:^

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pablik2005
            wrote on last edited by
            #5

            [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"

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              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);
              @

              (Z(:^

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Pablik2005
                wrote on last edited by
                #7

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

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

                1 Reply Last reply
                0
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #8

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

                  (Z(:^

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Pablik2005
                    wrote on last edited by
                    #9

                    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]

                    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