Regarding creating XML file
-
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.