Regarding creating XML file
-
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.
- <bookmark name="AAA"></bookmark>
-
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.