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 creating XML file
Forum Updated to NodeBB v4.3 + New Features

Regarding creating XML file

Scheduled Pinned Locked Moved General and Desktop
29 Posts 6 Posters 14.9k 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
    #1

    Hi All

    I want to create a simple XML file in Qt.
    Below is my tag content which is to be added to XML file.

    <Item>
    <child1="Name">ABC</child1>
    <child1="Age">12</child1>
    <child1="DOB">121212</child1>
    <child1="PLACE">XYZ</child1>
    <child1="SCHOOL">AAA</child1>
    <child1="FATHER">DDD</child1>
    <child1="MOTHER">SSS</child1>
    <child1="SISTER">WWW</child1>
    <child1="FRIENDS">QQQ</child1>
    </Item>

    I want to add 100 items shown above and generate XML file.

    Can anyone show me how to create a XML file with above content.

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

      Take a look at: "QXmlStreamWriter":http://developer.qt.nokia.com/doc/qt-4.7/qxmlstreamwriter.html

      (Z(:^

      1 Reply Last reply
      0
      • JohanSoloJ Offline
        JohanSoloJ Offline
        JohanSolo
        wrote on last edited by JohanSolo
        #3

        I'm not even sure that the structure you're showing us is XML-valid. The dom element name cannot have an attribute-like associated value. Why don't you first have a look at simple XML examples, like this?

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

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          I'm sorry, but I'm with previous poster, Johan Solo, that's no valid XML. A somewhat more correct versions would be:

          <Item>
          <child1 Name=“Name”>ABC</child1>
          ...
          </Item>

          or

          <Item>
          <child1 Name=“Name” Text="ABC" /> <!--not really sure about this one...-->
          ...
          </Item>

          More examples and validation utilities can be found here http://www.w3schools.com/xml/xml_examples.asp

          Ah! Forgot to mention, this is the data part of the XML... it also lacks a proper header! A first line like this one will do fine in most cases: <?xml version="1.0" encoding="ISO-8859-1"?>

          --

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

            [quote author="iytenorio" date="1313733811"]I'm sorry, but I'm with previous poster, Johan Solo, that's no valid XML.
            ...
            A first line like this one will do fine in most cases: <?xml version="1.0" encoding="ISO-8859-1"?>

            --[/quote]

            True, that. Although I would personally go for UTF-8 in terms of char encoding.
            Another nice way to represent the data described would be:
            @
            <Item>
            <Child>
            <Id>1</Id>
            <Name>AAA</Name>
            ...
            </Child>
            </Item>
            @

            As far as I know, your proposal of @ <child1 Name=“Name” Text=“ABC” /> @ is correct.

            (Z(:^

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

              Hi All

              You all are correct.
              Below is my xml file which i want to create
              <?xml version="1.0" ?>

              • <root>
                -<Item>
                <child1 name=“Name”>ABC</child1>
                <child1 age=“Age”>12</child1>
                <child1 dob=“DOB”>121212</child1>
                <child1 place=“PLACE”>XYZ</child1>
                <child1 school=“SCHOOL”>AAA</child1>
                <child1 father=“FATHER”>DDD</child1>
                <child1 mother=“MOTHER”>SSS</child1>
                <child1 sister=“SISTER”>WWW</child1>
                <child1 friends=“FRIENDS”>QQQ</child1>
                -</Item>
              • </root>
              1 Reply Last reply
              0
              • O Offline
                O Offline
                octal
                wrote on last edited by
                #7

                As said before, take a loot at "QXmlStreamWriter":http://doc.qt.nokia.com/latest/qxmlstreamwriter.html

                You will find an example "here":http://doc.qt.nokia.com/latest/xml-streambookmarks-xbelwriter-cpp.html

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

                  [quote author="Rajveer" date="1313734813"]Hi All

                  You all are correct.
                  Below is my xml file which i want to create
                  <?xml version="1.0" ?>

                  • <root>
                    -<Item>
                    <child1 name=“Name”>ABC</child1>
                    <child1 age=“Age”>12</child1>
                    <child1 dob=“DOB”>121212</child1>
                    <child1 place=“PLACE”>XYZ</child1>
                    <child1 school=“SCHOOL”>AAA</child1>
                    <child1 father=“FATHER”>DDD</child1>
                    <child1 mother=“MOTHER”>SSS</child1>
                    <child1 sister=“SISTER”>WWW</child1>
                    <child1 friends=“FRIENDS”>QQQ</child1>
                    -</Item>
                  • </root>[/quote]

                  Still looks like very silly XML to me. What's with all the attributes that have values equal to the attribute name?

                  Shouldn't it just be something like this (ommitting the header):
                  @
                  <root>
                  <item>
                  <name>ABC</name>
                  <age>12</age>
                  <dob>121212</dob>
                  ...
                  </item>
                  </root>
                  @

                  Or possibly:
                  @
                  <root>
                  <item>
                  <datapiece type="name">ABC</datapiece>
                  <datapiece type="age">12</datapiece>
                  <datapiece type="dob">121212</datapiece>
                  ...
                  </item>
                  </root>
                  @

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

                    Hi Andre

                    My XML file has the same pattern which u shown
                    @
                    <root>
                    <item>
                    <datapiece type="name">ABC</datapiece>
                    <datapiece type="age">12</datapiece>
                    <datapiece type="dob">121212</datapiece>
                    ...
                    </item>

                    <item>
                    <datapiece type="name">ABC</datapiece>
                    <datapiece type="age">12</datapiece>
                    <datapiece type="dob">121212</datapiece>
                    ...
                    </item>

                    <item>
                    <datapiece type="name">ABC</datapiece>
                    <datapiece type="age">12</datapiece>
                    <datapiece type="dob">121212</datapiece>
                    ...
                    </item>

                    <item>
                    <datapiece type="name">ABC</datapiece>
                    <datapiece type="age">12</datapiece>
                    <datapiece type="dob">121212</datapiece>
                    ...
                    </item>

                    </root>
                    @
                    Can you show me how to generate such XML file.

                    Edit: formatted XML as code section for readability; Andre

                    1 Reply Last reply
                    0
                    • JohanSoloJ Offline
                      JohanSoloJ Offline
                      JohanSolo
                      wrote on last edited by JohanSolo
                      #10

                      Why don't you write something like this :

                      <root>
                       <item>
                         <name> ABC </name>
                         <age> 12 </age>
                         <!-- ... ->
                        </item>
                      </root>
                      

                      To answer your question, I think you were already given twice the answer ;-)

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

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

                        As you were told before: [[doc:QXmlStreamWriter]] is going to be your friend. It is really simple to use. Look at the examples, try it out, and if you can't make it work, come back here with what you have tried so we point you in the right direction. We are not going to do your work for you.

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

                          Hi All

                          I have tried the following code
                          @ QXmlStreamWriter stream(&output);
                          stream.setAutoFormatting(true);
                          stream.writeStartDocument();
                          stream.writeStartElement("FIELD");
                          stream.writeAttribute("name", "AAA");
                          stream.writeEndElement(); // bookmark
                          stream.writeEndDocument();
                          output.close();
                          @
                          It is producing the following output
                          @
                          <?xml version="1.0" encoding="UTF-8" ?>

                          • <bookmark name="AAA"></bookmark>
                            @

                          I want to get output like this
                          @
                          <?xml version="1.0" encoding="UTF-8" ?>
                          -<root>

                          • <bookmark name="AAA">111</bookmark>
                            -</root>
                            @

                          What changes i have to do to get the following output.

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

                            Actually write a root document using writeStartElement("root") might help (and also a matching writeEndElement();, of course).

                            Note: I formatted your XML using @ tags again, because otherwise it is not readable. Please do that yourself next time, and do not include characters (like the - character) in your sample that are not in the actualy generated or outputted text.

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

                              Hi Andre

                              I am able to produce the following output

                              @ <?xml version="1.0" encoding="UTF-8" ?>

                              • <root>
                              • <ITEM>
                                <FIELD name="AAA" />
                                <FIELD surname="BBB" />
                                <FIELD designation="CCC" />
                                </ITEM>
                              • <root>
                                @

                              code for above is
                              @
                              QXmlStreamWriter stream(&output);
                              stream.setAutoFormatting(true);

                              stream.writeStartDocument();

                              stream.writeStartElement("root");

                              stream.writeStartElement("ITEM");
                              stream.writeStartElement("FIELD");
                              stream.writeAttribute("name", "AAA");
                              stream.writeEndElement();
                              stream.writeStartElement("FIELD");
                              stream.writeAttribute("surname", "BBB");
                              stream.writeEndElement();
                              stream.writeStartElement("FIELD");
                              stream.writeAttribute("designation", "CCC");
                              stream.writeEndElement();
                              stream.writeEndElement();
                              stream.writeEndElement();
                              stream.writeEndDocument();
                              output.close();
                              @

                              How to add the closing tag for FIELD & also value
                              eg:<FIELD name="AAA">value</FIELD>

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

                                bq. "QXmlStreamWriter":http://developer.qt.nokia.com/doc/qt-4.7/qxmlstreamwriter.html Element content consists of either characters, entity references or nested elements. It is written with writeCharacters(), which also takes care of escaping all forbidden characters and character sequences, writeEntityReference(), or subsequent calls to writeStartElement(). A convenience method writeTextElement() can be used for writing terminal elements that contain nothing but text.

                                As you do not add any values to your elements they are properly shortened.
                                @
                                <FIELD name="AAA" />
                                @
                                is basically the same as
                                @
                                <FIELD name="AAA"></FIELD>
                                @

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

                                  Thanks Lukas!!!!

                                  But if the items are more say 500 so the code will be redundant right.
                                  so is there any way to reduce this.

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

                                    Redundant? In what way?
                                    If you mean that XML is a bit verbose, then you are absolutely right. It is. But that is nothing that Qt can fix, that is just the way the standard works.

                                    If you think the output is too big, either compress the resulting xml file, or use another format entirely. Note that by choosing the structure you did (with FIELD elements instead of elements that actualy describe the contents) you made the format even more verbose. Compare the two equivant pieces of XML that I posted before:

                                    @
                                    <root>
                                    <item>
                                    <name>ABC</name>
                                    <age>12</age>
                                    <dob>121212</dob>
                                    ...
                                    </item>
                                    </root>
                                    @

                                    Is more efficient than:
                                    @
                                    <root>
                                    <item>
                                    <datapiece type="name">ABC</datapiece>
                                    <datapiece type="age">12</datapiece>
                                    <datapiece type="dob">121212</datapiece>
                                    ...
                                    </item>
                                    </root>
                                    @

                                    You are using the second structure.

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

                                      And if you mean that the C++ code you have to provide is redundant just merge the code for creating a single FIELD to its own method and loop over your data.

                                      @
                                      void writeField(const QXmlStreamWriter& stream, const QString& name, const QString& value)
                                      {
                                      stream.writeStartElement("FIELD");
                                      stream.writeAttribute("name", name);
                                      stream.writeCharacters(value);
                                      stream.writeEndElement();
                                      }
                                      ...
                                      stream.writeStartDocument();
                                      stream.writeStartElement("root");
                                      foreach(const Entity entity, entityList)
                                      {
                                      stream.writeStartElement("ITEM");
                                      writeField(stream, "name", entity.name);
                                      writeField(stream, "surname", entity.surname);
                                      ...
                                      stream.writeEndElement();
                                      }
                                      stream.writeEndDocument();
                                      @
                                      Brain to terminal. Not tested.

                                      Yes, XML means bloat, code-wise and data-wise. XML is completely overhyped and misused in most of its use. If you just want to serialize ("save") data use "QDataStream":http://doc.qt.nokia.com/latest/qdatastream.html instead.

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

                                        Hi All
                                        Thanks for reply.

                                        I have one more query reading the XML file
                                        My xml file looks like this
                                        @
                                        <item>
                                        <datapiece type="name"></datapiece>
                                        <datapiece type="age"></datapiece>
                                        <datapiece type="dob"></datapiece>
                                        ...
                                        </item>
                                        @

                                        I have written a following code to read this file
                                        @
                                        QFile file(fileName);
                                        if (!file.open(QIODevice::ReadOnly))
                                        {
                                        qDebug() << "Not able to open the file";
                                        return;
                                        }

                                        QString errMsg;
                                        int errLine;
                                        int errCol;
                                        
                                        QDomDocument doc;
                                        if (!doc.setContent(&file,false,&errMsg,&errLine,&errCol)) 
                                        

                                        {
                                        qDebug() << "Error in XML File format" <<endl;
                                        return;
                                        }

                                        QDomElement root = doc.documentElement();
                                        if (root.tagName() != "item") 
                                        

                                        {
                                        qDebug() << "Error in ITEM" << endl;
                                        }
                                        QDomNodeList list = root.childNodes();
                                        @

                                        Now I have read all the tags i want to set the value for these tags[value I mean to say is ABC in below eg]
                                        for eg:
                                        <datapiece type="name">ABC</datapiece>

                                        How to do this using DOM?

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

                                          Why would you want to use dom to read this? I would recommend you use Qt's QXmlStreamReader. It is much lighter in terms of memory usage and overhead, and is easier to use for a lot of use cases.

                                          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