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. Editing XML

Editing XML

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 10.0k Views
  • 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.
  • H Offline
    H Offline
    haney
    wrote on last edited by
    #1

    Hi,

    I have a XML like this:
    @
    <rootTag>
    <device desc="customDevice1" type="1" id="21"/>
    <device desc="customDevice2" type="2" id="22"/>
    <device desc="customDevice3" type="3" id="23"/>
    <function id="1">Some content function1</function>
    <function id="2">Some content function2</function>
    <function id="3">Some content function3</function>
    </rootTag>
    @

    I would like to modify the existing values of "function id" or any attributes of device. After modifying I need to save the same file.

    Please suggest any ideas.

    Thanks,
    Haney.

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

      Did you take a look at the tree sets of XML classes that Qt has?

      • DOM-based: [[doc:QXmlDomDocument]] and friends
      • SAX-based: [[doc:QXmlReader]] and friends
      • stream-based: [[doc:QXmlStreamReader]] and [[doc:QXmlStreamWriter]]
      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on last edited by
        #3

        [quote author="Andre" date="1342523969"]

        • DOM-based: [[doc:QXmlDomDocument]] and friends
          [/quote]

        haney, please have a look at "this example of DOM-based parsing XML":http://www.developer.nokia.com/Community/Wiki/Using_QDomDocument_to_parse_XML using "QDomDocument":http://qt-project.org/doc/qt-4.8/qdomdocument.html.

        http://anavi.org/

        1 Reply Last reply
        0
        • H Offline
          H Offline
          haney
          wrote on last edited by
          #4

          Thanks for your replies.

          I am able to use DOM based parsing XML and successfully reading the values. I want help in writing/editing in the same xml.

          Eg: I just want to edit/ rewrite function id =1 CONTENT.

          I want to add new device say <device desc="customDevice4" type="4" id="24"/>

          DOM based parsing is really nice for reading. Please suggest me how to rewrite/modify values.

          Code for Reading:
          @
          int main(int argc, char *argv[])
          {
          QApplication app(argc, argv);

          QFile xmlFile&#40;":/sample.xml"&#41;;
          xmlFile.open(QIODevice::ReadOnly);
          
          QDomDocument doc;
          doc.setContent(xmlFile.readAll());
          
          QDomElement root = doc.documentElement();
          QDomElement nodeTag = root.firstChildElement("device");
          while(!nodeTag.isNull())
          {
              qDebug()<<"attributes of device"<<nodeTag.attribute("desc")<<"type"    <<nodeTag.attribute("type") <<"id"<<nodeTag.attribute("id");
              nodeTag = nodeTag.nextSiblingElement("device");
          }
          

          QDomNodeList nodeList = root.elementsByTagName("function");

          for(int i =0;i<nodeList.count();i++)
          {
          QDomElement el = nodeList.at(i).toElement();

          qDebug()<<nodeList.at(i).toElement().text(); // prints content
          qDebug()<<el.attribute("id"); // prints id value
          

          }

          xmlFile.close();
          return app.exec();
          

          }
          @

          Now that reading is fine.. I need to replace old content with new content. Also I need to modify desc of device, type, id values.

          Appreciate your help in this regard.

          Thanks,
          Haney.

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

            What have you tried already with regards to modifying values or creating new ones? Did you notice methods like setNodeValue(), appendChild() and setAttribute()?

            1 Reply Last reply
            0
            • H Offline
              H Offline
              haney
              wrote on last edited by
              #6

              @
              el.setAttribute("id", 500);
              nodeList.at(i).toElement().setNodeValue("new content");
              @

              I just tried these two lines of code in after line num 26 in above post. I made xmlFile.open(QIODevice::WriteOnly);

              but I did not see any new content in my xml file. Do I need to save it in a different way. Please share any working example of editing xml.

              Thanks for the favour.

              Thanks,
              Haney.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on last edited by
                #7

                Of course, you just changed the content in the DomDocument. To save the changes to the file you need to write the content of the DomDocument back to the file (overwrite the file).

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  panosk
                  wrote on last edited by
                  #8

                  KA510 is right. You can use something like this to overwrite the file:

                  @
                  xmlfile.resize(0);
                  QTextStream stream;
                  stream.setDevice(&xmlfile);
                  doc.save(stream,0);
                  xmlfile.close();
                  @

                  I'm new to Qt too, so maybe there's a better way to do it.

                  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