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. Regarding QDomDocument

Regarding QDomDocument

Scheduled Pinned Locked Moved General and Desktop
30 Posts 5 Posters 18.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.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #9

    Hi lukas

    What I am trying to do is i have an xml file,I am performing following steps

    1.)Open in ReadWrite mode
    2.)Read that file using QDomDocument
    3.)Get all the child nodes

    4.)Now as i have read all tha nodes and are in memory i want to change the value of particular chaild node and update the file with these changes.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #10

      Yes. Find the node using elementsByTagName() and set the data using setData() - as DanSiddiqui and Andre said.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Indrajeet
        wrote on last edited by
        #11

        Hi

        Below is my code i tried by not yet success please help

        @
        QFile file("ABC.xml");
        if (!file.open(QIODevice::ReadWrite))
        {
        qDebug() << "Not able to open the file";
        return 1;
        }

        QString errMsg;
        int errLine;
        int errCol;
        
        QDomDocument doc;
        if (!doc.setContent(&file,false,&errMsg,&errLine,&errCol&#41;)
        {
            qDebug() << "Error in XML File format" <<endl;
            return 1;
        }
        
        QDomElement root = doc.documentElement();
        if (root.tagName() != "TAG")
        {
            qDebug() << "Error in TAG" << endl;
        }
        
        QDomNodeList list = root.childNodes();
        QDomAttr attr;
        int iSize = list.size();
        QDomNodeList list1 = doc.elementsByTagName("Rajveer");
        for (int i=0; i < iSize;i++)
        {
            QDomElement elem = list.item(i).toElement();
            attr = elem.attributeNode("name");
            if(!attr.isNull())
            {
               if(attr.value() == "Rajveer")
              {
                   QDomText text2 = doc.createTextNode("ITPL-Park2");
                   elem.appendChild(text2);* 
              }
           }
        

        }
        file.close();
        @

        My file looks something like this
        @
        <root>
        <TAG>
        <FIELD name= "Rajveer">value</FIELD>
        .
        .
        .
        </TAG>
        </root>
        @
        With above code I am still not able to update the value of required tag in my file please help.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #12

          As long as you are not willing to put the effort to properly format your code (as I, I seem to remember, told you before), I am not willing to put in the effort to read it.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Indrajeet
            wrote on last edited by
            #13

            Andre i have formated the code.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #14

              Did you try to debug the code? Does it find your node? I doubt it, as you do not have INDEX elements in your document. That would mean that line 28 returns an empty list, thus your loop (including the code to set a value) is not executed.

              Please, run your code through a debugger, and step through it. It is really enlightening. If you can not figure out how to do that, at least insert qDebug() << LINE; statements at every code junction. That will give you insight in how your code is actually executed.

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Indrajeet
                wrote on last edited by
                #15

                Hi Andre

                I have updated my code please have a check in debug also even if i am getting the pointer I am not ale to update the value in the file.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #16

                  No, you will need to do the debugging of your code. If you want me to do it, reference the second line in my signature.

                  I would suggest you read up on your XML knowledge of what an element is, and thus, what an element name is. Hint: it is not "Rajveer".

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #17

                    If you are interested in a real advice: Stop botching code together and learn how to develop software instead. This includes a basic understanding of how computers work, a basic understanding of software design, programming language and toolkit comprehension and the ability to read, understand and adopt documentation and examples given. This will take time but every hour spent will save another ten later on.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #18

                      Your XML:

                      @
                      <FIELD name = “Rajveer”>Value</FIELD>
                      @

                      Your code:

                      @
                      QDomNodeList list1 = doc.elementsByTagName("Rajveer");
                      @

                      How should it ever find a tag named "Rajveer", if you only have tag named "FIELD".

                      Are you sure, you did understand the minimal basics of XML?

                      Did you ever try to debug the code yourself? Either with debug output like qDebug() or with stepping through the code with a debuggger? You would immediately have spottet that you never get into the for loop.

                      Pleas do yourself and everyone else a favor and learn the basic principles of programming.

                      Also, make yourself comfortable with the most basic XML stuff, like the differences between tags, attribute names and attribute values.

                      Else you will fail miserably with your project.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        Indrajeet
                        wrote on last edited by
                        #19

                        Hi All

                        I have a fixed TAG template which i have saved in Template.xml

                        Template.xml
                        @
                        <TAG>
                        <ITEM name = "A"></ITEM>
                        <ITEM name = "B"></ITEM>
                        <ITEM name = "C"></ITEM>
                        <ITEM name = "D"></ITEM>
                        <ITEM name = "E"></ITEM>
                        </TAG>
                        @
                        I read the contents of above file & set the values.
                        Below is the code for that
                        @
                        QDomDocument doc;
                        QFile file(strFilename);
                        if (!file.open(QIODevice::ReadOnly))
                        {
                        qDebug() << "Not able to open the file";
                        return doc;
                        }

                                  QString errMsg;
                                  int errLine;
                                  int errCol;
                        
                                  if (!doc.setContent(&file,false,&errMsg,&errLine,&errCol))
                                  {
                                            qDebug() << "Error in XML File format" <<endl;
                                            return doc;
                                  }
                                  file.close();
                                  QDomElement root = doc.documentElement();
                                  if (root.tagName() != "TAG")
                                  {
                                           qDebug() << "Error in TAG" << endl;
                                  }
                        
                                  QDomNodeList list = root.childNodes();
                                  QDomAttr attr;
                                  int iSize = list.size();
                        
                                  for (int i=0; i < iSize;i++)
                                  {
                                       QDomElement elem = list.item(i).toElement();
                                       attr = elem.attributeNode("name");
                                       if(!attr.isNull())
                                      {
                                             if(attr.value() == "A")
                                            {
                                                 qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl;
                                                 QDomText text2 = doc.createTextNode("ONE");
                                                 elem.appendChild(text2);
                                            }
                                            else if(attr.value() == "B")
                                            {
                                                 qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl;
                                                 QDomText text2 = doc.createTextNode("Two");
                                                 elem.appendChild(text2);
                                            }
                                            else if(attr.value() == "C")
                                            {
                                                 qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl;
                                                 QDomText text2 = doc.createTextNode("Three");
                                                 elem.appendChild(text2);
                                            }
                                            else if(attr.value() == "D")
                                            {
                                                qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl;
                                                QDomText text2 = doc.createTextNode("Four");
                                                elem.appendChild(text2);
                                           }
                                           else if(attr.value() == "E")
                                           {
                                               qDebug() << " Tagname = " << elem.tagName() << " Text Value " << elem.text() << endl;
                                               QDomText text2 = doc.createTextNode("Five");
                                                elem.appendChild(text2);
                                           }
                            }
                        

                        }
                        @

                        After updating values now I am saving this document in other file.
                        Below is the code for that
                        @
                        QFile generatedFile("Main.xml");
                        if (!generatedFile.open(QIODevice::WriteOnly))
                        {
                        qDebug() << "Not able to open the file";
                        return 1;
                        }
                        QTextStream textStream(&generatedFile);
                        doc.save(textStream,4);
                        generatedFile.close();
                        @

                        Now I want to add header & root element to this Main.xml file & also i want to clone or append as many Tags as above in this file how to do that?

                        My Main.xml should look like this
                        @
                        <!DOCTYPE xml>
                        <Root>
                        <TAG>
                        <ITEM name = "A">1</ITEM>
                        <ITEM name = "B">2</ITEM>
                        <ITEM name = "C">3</ITEM>
                        <ITEM name = "D">5</ITEM>
                        <ITEM name = "E">4</ITEM>
                        </TAG>
                        <TAG>
                        <ITEM name = "A">11</ITEM>
                        <ITEM name = "B">12</ITEM>
                        <ITEM name = "C">11</ITEM>
                        <ITEM name = "D">41</ITEM>
                        <ITEM name = "E">44</ITEM>
                        </TAG>
                        .
                        .
                        .

                        </Root>
                        @

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #20

                          With that badly formatted code there is no wonder that you do not find your errors. Do you really expect us to play the "where is the corresponding brace game" in that big mess?

                          Please format the code nicely, first of all indent the blocks in a reasonable way, and edit your original post to update the code sections. We might come back and look at it then. Please leave a short comment in the comment box to notify of the update, so that the post shows up again in the listings.

                          You might want to read http://www.catb.org/~esr/faqs/smart-questions.html before you continue.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • I Offline
                            I Offline
                            Indrajeet
                            wrote on last edited by
                            #21

                            HI volker

                            I edit window the code is appearing as nicely indented but when i see the preview it shows messed up indentation.

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #22

                              Why do you not construct the whole XML using QDom methods but open an XML template and try to modify and duplicate this? This is quite strange at best, as you have the structure of the XML hardcoded in your method anyways (by means of the huge if/else blocks).

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • I Offline
                                I Offline
                                Indrajeet
                                wrote on last edited by
                                #23

                                Hi volker

                                I know it is redundant way,but is it possible to get the result withthe same.

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  goetz
                                  wrote on last edited by
                                  #24

                                  "QDomNode::cloneNode() ":http://doc.qt.nokia.com/4.7/qdomnode.html#cloneNode should be your friend.

                                  http://www.catb.org/~esr/faqs/smart-questions.html

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    andre
                                    wrote on last edited by
                                    #25

                                    Rajveer, why do you keep opening topics on the same issue?
                                    http://developer.qt.nokia.com/forums/viewthread/9240/
                                    http://developer.qt.nokia.com/forums/viewthread/8947/
                                    http://developer.qt.nokia.com/forums/viewthread/7905/
                                    http://developer.qt.nokia.com/forums/viewthread/7704/

                                    You don't seem to be making much progress, to be honest.

                                    1 Reply Last reply
                                    0
                                    • I Offline
                                      I Offline
                                      Indrajeet
                                      wrote on last edited by
                                      #26

                                      Hi All

                                      My XML tag format is as shown below
                                      @
                                      <INFO>
                                      <FIELD name = "ONE"></FIELD>
                                      .
                                      .
                                      .
                                      600
                                      </INFO>

                                      My final XML file should be like this

                                      <?xml version="1.0"?>
                                      <root>
                                      <INFO>....</INFO>
                                      <INFO>....</INFO>
                                      <INFO>....</INFO>
                                      <INFO>....</INFO>
                                      .
                                      .
                                      .
                                      50000
                                      </root>
                                      @
                                      I want to create a xml file with 50000 tags as shown above.

                                      Below is my code
                                      @
                                      QDomDocument mainDoc;
                                      QDomProcessingInstruction instr = mainDoc.createProcessingInstruction("xml","version="1.0"");
                                      mainDoc.appendChild(instr);
                                      QDomElement root = mainDoc.createElement("root");
                                      mainDoc.appendChild(root);

                                      QDomDocument doc;
                                      QString errMsg;
                                      int errLine;
                                      int errCol;
                                      

                                      QString strItemTemplateData;//In this i have my Tag INFO stored
                                      if (!doc.setContent(strItemTemplateData,false,&errMsg,&errLine,&errCol))
                                      {
                                      qDebug() << "Error in XML File format" <<endl;
                                      }
                                      for(int i=0;i<50000;i++)
                                      {
                                      QDomNode clonedNode = doc.cloneNode(true);
                                      root.appendChild(clonedNode);
                                      }
                                      QFile generatedFile(strXMLFileName);
                                      if (!generatedFile.open(QIODevice::WriteOnly))
                                      {
                                      qDebug() << "Error in Filet" <<endl;
                                      return;
                                      }
                                      QTextStream textStream(&generatedFile);
                                      mainDoc.save(textStream,4);
                                      generatedFile.close();
                                      @

                                      My code above is performing very slow i.e it is taking lot of time & also some times it is crashing.
                                      Can anybody tell me how to create such a big file with good performance.
                                      And While creating the TAGS i have to set values for each TAG items for which i have to check each attribute value and than set it.
                                      eg:
                                      @
                                      <FIELD name="ONE">USA</FIELD>
                                      @

                                      [EDIT: three dots are enough, Volker]

                                      1 Reply Last reply
                                      0
                                      • L Offline
                                        L Offline
                                        lgeyer
                                        wrote on last edited by
                                        #27

                                        QDomDocument is not the way how to handle big XML documents, but you have been told numerous times:

                                        http://developer.qt.nokia.com/forums/viewthread/9483/
                                        http://developer.qt.nokia.com/forums/viewthread/9240/
                                        http://developer.qt.nokia.com/forums/viewthread/8947/
                                        http://developer.qt.nokia.com/forums/viewthread/7905/
                                        http://developer.qt.nokia.com/forums/viewthread/7704/

                                        So - please - stop creating topics on the same issue over and over again and stick to the ones you've already created.

                                        1 Reply Last reply
                                        0
                                        • I Offline
                                          I Offline
                                          Indrajeet
                                          wrote on last edited by
                                          #28

                                          Hi Lukas

                                          Not in anyone of the links u pasted has mentioned that
                                          QDomDocument is not the way how to handle big XML documents
                                          So please see my question and purpose of my question.

                                          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