Regarding creating XML file



  • 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.


  • Moderators



  • 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?



  • 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"?>

    --


  • Moderators

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



  • 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>


  • 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



  • [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>
    @



  • 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



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



  • 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.



  • 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.



  • 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.



  • 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>



  • 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>
    @



  • 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.



  • 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.



  • 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.



  • 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?



  • 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.



  • Hi Andre

    But if i have tags about 700-1000 as below
    @
    <item>
    <datapiece type="name"></datapiece> --------->1st
    <datapiece type="age"></datapiece>
    <datapiece type="dob"></datapiece>
    .
    .
    .
    <datapiece type="dob"></datapiece>---------->1000
    </item>
    @

    using the QXmlStreamReader for above to read and than to set the value
    will result in more lines of code right.



  • Perhaps it will result in more code, but in much less memory usage. However, your exact goal is not very clear to me, so I might be off.



  • So a single item has over thousand unique attributes?



  • yes Lukas



  • Hi Andre

    My goal is
    @
    <item>
    <datapiece type="name"></datapiece> --------->1st
    <datapiece type="age"></datapiece>
    <datapiece type="dob"></datapiece>
    .
    .
    .
    <datapiece type="dob"></datapiece>---------->1000
    </item>
    @
    My item structure is fixed as shown above.

    My goal is user can add any numberof such items and generate an XML file.
    User will read the template of the item set the values as shown in below eg.
    For eg:
    <datapiece type="name">value</datapiece>

    Like wise user will set the values for all the fields in item & put that item in XML file.

    Now can you show me how to do this with XMLStreamReader & XMLStreamWriter.



  • [quote author="Rajveer" date="1314243849"]My goal is user can add any numberof such items and generate an XML file.[/quote]

    Just add the type / value pairs entered by the user to a collection over which you can iterate later on.

    @
    typedef QHash<QString, QString> Item;
    typedef QList<Item> ItemList;

    Item itemA;
    itemA.insert("name", "John Doe");
    itemA.insert("age", "24");

    Item itemB;
    itemB.insert("name", "Joanna Doe");
    itemB.insert("age", "32");

    ItemList itemList;
    itemList.append(itemA);
    itemList.append(itemB);

    QFile xmlFile("data.xml");
    if(xmlFile.open(QIODevice::WriteOnly))
    {
    QXmlStreamWriter xmlStream(&xmlFile);
    xmlStream.writeStartDocument();
    xmlStream.writeStartElement("data");
    for(ItemList::const_iterator itemListIterator = itemList.constBegin(); itemListIterator != itemList.constEnd(); ++itemListIterator)
    {
    xmlStream.writeStartElement("item");
    for(Item::const_iterator itemIterator = (*itemListIterator).constBegin(); itemIterator != (*itemListIterator).constEnd(); ++itemIterator)
    {
    xmlStream.writeStartElement("datapiece");
    xmlStream.writeAttribute("type", itemIterator.key());
    xmlStream.writeCharacters(itemIterator.value());
    xmlStream.writeEndElement();
    }
    xmlStream.writeEndElement();
    }
    xmlStream.writeEndElement();
    xmlStream.writeEndDocument();
    }
    xmlFile.close();
    @
    Brain to terminal. Not tested. Will differ in practice for the sake of good software design.

    There is something I want to add and please don't get me wrong on that. To me the problem has just litte to do with QXmlStreamReader or QXmlStreamWriter - it is based on the lack of basic software design knowledge. Head for a good book or tutorial on programming (with C++) and software design first. This will make your life much easier.



  • Hi Lukas

    Can it be don ein following way?

    I will keep the item structure in one file.
    For eg:
    @
    <item>
    <datapiece type="name"></datapiece> --------->1st
    <datapiece type="age"></datapiece>
    <datapiece type="dob"></datapiece>
    .
    .
    .
    <datapiece type="dob"></datapiece>---------->1000
    </item>
    @

    Say I have this in some temp.xml file.
    Now what i want is we can read this file and set the attribute values & than copy this to other file.



  • Rajveer,

    I still don't understand your ultimate requirements. And don't tell me that your requirement is to write an XML file of format Y, that is just a means to an end. What I don't understand, is

    what kind of operations you need to do on this file

    what the whole talk about "this file" and "the other file" is about

    what kind of sane application would just dump a 1000 attributes on a single object in a file, and expect the user to interact with that.

    To me, it seems like you basically need to store a lot of arbitrary key/value pairs for one or more items, and you need to be able to read that back with the same application, modify it, and write it again. Something like that. If that is the case, then there may be better ways to achieve that than using the XML classes, but untill that is clear, I will refrain from giving further advice.



  • [quote author="Rajveer" date="1314258209"]
    Now what i want is we can read this file and set the attribute values & than copy this to other file.[/quote]

    Take a look at the "QXmlStreamReader":http://doc.qt.nokia.com/latest/qxmlstreamreader.html documentation and "QXmlStream Bookmarks Example":http://doc.qt.nokia.com/latest/xml-streambookmarks.html.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.