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. Writing Subchild with DOM
Forum Updated to NodeBB v4.3 + New Features

Writing Subchild with DOM

Scheduled Pinned Locked Moved Solved General and Desktop
parsexml parsing
12 Posts 2 Posters 3.6k 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.
  • A Offline
    A Offline
    Aashu10
    wrote on 9 Mar 2016, 06:45 last edited by Aashu10 3 Sept 2016, 08:32
    #1

    I already have an XML file, I need to write under a Header, Below i have show a my XML file.

    <MAIN_HEADER>
      
      <HEADER1>
      //it's child
      </HEADER1>
    
      <HEADER2>
      //it's child>
      </HEADER2>
    
      <HEADER3>
      //EMPTY
      </HEADER3>
      
      <HEADER4>
      //it's child
      </HEADER4>
    
    </MAIN_HEADER>
      
    

    I need to write child and subchild inside HEADER3 . Below is my code.

    QFile file(output);
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug()<<"file doesnt exists";
        }
        QDomDocument domDocument;
        if( !domDocument.setContent( &file ) )
        {
            qDebug()<<"Error";
    
            return;
        }
    
        QDomElement topElement = domDocument.documentElement();
        QDomNode domNode = topElement.firstChild();
        while(!domNode.isNull())
        {
                        QDomElement domElement = domNode.toElement();
                        if(!domElement.isNull())
                        {
    
            if(domElement.tagName() == "HEADER3")
            {
                for(int i=0; i<List.count();i++)
                {
                    QDomElement node = domDocument.createElement("CHILD");
                    node.setAttribute("Name", List[i].Filename);
                    node.setAttribute("Value", List[i].value);
                    domElement.appendChild(node);
                    if(List[i].subChild.size() != 0)
                    {
                        QDomElement subNode =   domDocument.createElement("SubChild");
                        subNode.setAttribute("Name", List[i].SubChild);
                        node.appendChild(subNode);
                    }
                }
            }
    
    
                    }
    domNode = domNode.nextSibling();
                }
        file.close();
    
        QFile outfile( output);
        if( !outfile.open( QIODevice::WriteOnly | QIODevice::Text ) )
        {
            qDebug()<<"Error";
        }
    
        QTextStream stream1( &outfile );
        stream1 << dom.toString();
        outfile.close();
    

    This code gets stuck inside while loop, I'm not so sure about the hierarchy in this code. I just want to write the contents in <HEADER3>

    J 1 Reply Last reply 9 Mar 2016, 07:16
    0
    • A Aashu10
      9 Mar 2016, 06:45

      I already have an XML file, I need to write under a Header, Below i have show a my XML file.

      <MAIN_HEADER>
        
        <HEADER1>
        //it's child
        </HEADER1>
      
        <HEADER2>
        //it's child>
        </HEADER2>
      
        <HEADER3>
        //EMPTY
        </HEADER3>
        
        <HEADER4>
        //it's child
        </HEADER4>
      
      </MAIN_HEADER>
        
      

      I need to write child and subchild inside HEADER3 . Below is my code.

      QFile file(output);
          if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
          {
              qDebug()<<"file doesnt exists";
          }
          QDomDocument domDocument;
          if( !domDocument.setContent( &file ) )
          {
              qDebug()<<"Error";
      
              return;
          }
      
          QDomElement topElement = domDocument.documentElement();
          QDomNode domNode = topElement.firstChild();
          while(!domNode.isNull())
          {
                          QDomElement domElement = domNode.toElement();
                          if(!domElement.isNull())
                          {
      
              if(domElement.tagName() == "HEADER3")
              {
                  for(int i=0; i<List.count();i++)
                  {
                      QDomElement node = domDocument.createElement("CHILD");
                      node.setAttribute("Name", List[i].Filename);
                      node.setAttribute("Value", List[i].value);
                      domElement.appendChild(node);
                      if(List[i].subChild.size() != 0)
                      {
                          QDomElement subNode =   domDocument.createElement("SubChild");
                          subNode.setAttribute("Name", List[i].SubChild);
                          node.appendChild(subNode);
                      }
                  }
              }
      
      
                      }
      domNode = domNode.nextSibling();
                  }
          file.close();
      
          QFile outfile( output);
          if( !outfile.open( QIODevice::WriteOnly | QIODevice::Text ) )
          {
              qDebug()<<"Error";
          }
      
          QTextStream stream1( &outfile );
          stream1 << dom.toString();
          outfile.close();
      

      This code gets stuck inside while loop, I'm not so sure about the hierarchy in this code. I just want to write the contents in <HEADER3>

      J Offline
      J Offline
      JohanSolo
      wrote on 9 Mar 2016, 07:16 last edited by
      #2

      @Aashu10 said:

      My code get's stuck when i compile it. I am very new to DOM Parser.

      Please elaborate what you mean? Getting stuck when compiling? What is the error?

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Aashu10
        wrote on 9 Mar 2016, 07:22 last edited by
        #3

        No error, It's just stuck, When i debuged it. I found it's stuck in the while loop.

        J 1 Reply Last reply 9 Mar 2016, 07:24
        0
        • A Aashu10
          9 Mar 2016, 07:22

          No error, It's just stuck, When i debuged it. I found it's stuck in the while loop.

          J Offline
          J Offline
          JohanSolo
          wrote on 9 Mar 2016, 07:24 last edited by
          #4

          @Aashu10 said:

          No error, It's just stuck, When i debuged it. I found it's stuck in the while loop.

          OK, so the code compiles.

          I think you never go to the next node... it just reads forever and ever the first node of you document.

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Aashu10
            wrote on 9 Mar 2016, 07:28 last edited by
            #5

            Any suggestion how to bypass this?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JohanSolo
              wrote on 9 Mar 2016, 07:46 last edited by
              #6

              Well, at some point you must read you document, i.e. go from one node to the other. At the end of the loop you should do something like domNode = domeNode.nextSibling(). If you don't do so, you're endlessly reading the first node of your document, as I've told you before.

              `They did not know it was impossible, so they did it.'
              -- Mark Twain

              1 Reply Last reply
              1
              • A Offline
                A Offline
                Aashu10
                wrote on 9 Mar 2016, 08:02 last edited by Aashu10 3 Sept 2016, 08:17
                #7

                Ah! Thank you! @JohanSolo Now it goes through the loop properly, but the contents of the header is still empty. I just debugged, found out it never goes into

                if(domElement.text() == "HEADER3")
                

                I am not sure about hierarchy in the code. but the hierarchy in my XML file is as shown in this XML file

                @JohanSolo I have edited the code accordingly

                J 1 Reply Last reply 9 Mar 2016, 08:17
                0
                • A Aashu10
                  9 Mar 2016, 08:02

                  Ah! Thank you! @JohanSolo Now it goes through the loop properly, but the contents of the header is still empty. I just debugged, found out it never goes into

                  if(domElement.text() == "HEADER3")
                  

                  I am not sure about hierarchy in the code. but the hierarchy in my XML file is as shown in this XML file

                  @JohanSolo I have edited the code accordingly

                  J Offline
                  J Offline
                  JohanSolo
                  wrote on 9 Mar 2016, 08:17 last edited by JohanSolo 3 Sept 2016, 08:18
                  #8

                  @Aashu10 said:

                  if(domElement.text() == "HEADER3")
                  

                  I am not sure about hierarchy in the code. but the hierarchy in my XML file is as shown in this XML file

                  Use tagName(), text() returns the text contained in a tag.

                  `They did not know it was impossible, so they did it.'
                  -- Mark Twain

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    Aashu10
                    wrote on 9 Mar 2016, 08:32 last edited by Aashu10 3 Sept 2016, 08:32
                    #9

                    Yes! @JohanSolo Now it goes inside the loop, but does not write the contents inside HEADER3. This time i have no idea why. I have updated the code. Please do take a look.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JohanSolo
                      wrote on 9 Mar 2016, 09:46 last edited by JohanSolo 3 Sept 2016, 09:51
                      #10

                      I don't see any obvious mistake. Note that I've never used QDomDocument to write a file. It's such a waste of memory according to me...

                      Edit: you should get a look at the returned value of appendChild to check that everything went fine.

                      `They did not know it was impossible, so they did it.'
                      -- Mark Twain

                      1 Reply Last reply
                      1
                      • A Offline
                        A Offline
                        Aashu10
                        wrote on 9 Mar 2016, 10:09 last edited by
                        #11

                        Thanks for replying, How do i check the return value of that. Sorry not so proficient with DomDocument.

                        J 1 Reply Last reply 9 Mar 2016, 11:11
                        0
                        • A Aashu10
                          9 Mar 2016, 10:09

                          Thanks for replying, How do i check the return value of that. Sorry not so proficient with DomDocument.

                          J Offline
                          J Offline
                          JohanSolo
                          wrote on 9 Mar 2016, 11:11 last edited by
                          #12

                          @Aashu10 said:

                          Thanks for replying, How do i check the return value of that. Sorry not so proficient with DomDocument.

                          Have a look at the doc

                          `They did not know it was impossible, so they did it.'
                          -- Mark Twain

                          1 Reply Last reply
                          1

                          5/12

                          9 Mar 2016, 07:28

                          • Login

                          • Login or register to search.
                          5 out of 12
                          • First post
                            5/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved