PDA

View Full Version : XML saving settings



^NyAw^
11th May 2009, 17:56
Hi,

I'm trying to save an XML file that will look like this:



<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE MyApp Project>
<Project>
<Name>
"Project Name"
</Name>
<Programs>
<Program 1>
<value1>
"Value1"
</value1>
<value2>
"Value2"
</value2>
</Program 1>
</Programs>
</Project>


Can anoyone provide me an example to write this XML? I'm reading the docs but I can't find a XML creation example.

Thanks,

Lykurg
11th May 2009, 18:09
Hi, have a look at the description of QDomDocument. There is an example how to create a document with nodes and then simply use toString() and write the return QString to a file.

^NyAw^
11th May 2009, 18:23
Hi,

Thanks,

What is the difference of creating an element to store a value or to set an attribute of an element? I can see that it stores the data on a different manner.

Table that has 2 rows and 3 columns


<table>
<rows>
2
</rows>
<columns>
3
</columns>
or:


<table>
<table Rows="2" Columns="3" />
</table>


PS: I'm able to write the XML file as the second way but I'm not able to write as the first way.

Lykurg
11th May 2009, 19:21
What is the difference of creating an element to store a value or to set an attribute of an element?
Well the difference between them is the way how they are stored:rolleyes: Well, XML activist would say: attributes are worst things ever, but I don't care about it. Both ways are legal XML. But in normally one should preferre tags instead of attributes. But not for simple integer indexes. For that i use attributes. So the XML looks nicer to me. But the decision is yours...


PS: I'm able to write the XML file as the second way but I'm not able to write as the first way.


QDomDocument doc("MyML");
QDomElement root = doc.createElement("table");
doc.appendChild(root);

QDomElement tag = doc.createElement("rows");
root.appendChild(tag);
QDomText t = doc.createTextNode("2");
tag.appendChild(t);

QDomElement tag2 = doc.createElement("colums");
root.appendChild(tag2);
QDomText t2 = doc.createTextNode("3");
tag2.appendChild(t2);

QString xml = doc.toString();

talk2amulya
11th May 2009, 20:08
there is also a QXmlStreamWriter which directly addresses issue of writing xml.

faldzip
11th May 2009, 20:28
Hi,


<table>
<rows>
2
</rows>
<columns>
3
</columns>


In this one the <table>, <rows> and <columns> are QDomElements, while 2, 3 are QDomTexts and they are children for <rows> and <columns> respectively. So you have to make the QDomDocument which represents the whole XML document, and then use QDomDocument::createXXX() where XXX can be Element, TextNode, Attr and many more (look at Lykurg's example code). Then you can build the right XML tree by appending nodes as children to another nodes.
The QXmlStreamWriter is a different approach, where you wirte your XML from beginnig to the end. In your example it would be something like this: start writing <table>, start writing <rows>, write 3, end writing <rows>, start writing <columns>, write 3, end writing <columns>, end writing <table>.
And I represent the Lykurg's way of using attributes :] for the <table> I would use rows and columns as attributes, and for example each item e.g. <item> would have attributes row, column but the content of item would be tags inside <item>.

^NyAw^
12th May 2009, 09:18
Hi,

Thanks to all.
I asked about the second way(using QDomText) because I like the way that Qt stores the translation files(*.ts) that uses this behaviour. But maybe I will mix the two versions depending on the things that I'm going to store.

Thanks again,

^NyAw^
12th May 2009, 11:13
Hi,

When I write the XML file, the elements that have attributes write them into a random order?



QDomElement qProject = qDoc.createElement("Project");
qDoc.appendChild(qProject);
qProjecte.setAttribute(QString("Version"),QString::number(m_fVersion));
qProjecte.setAttribute(QString("Name"),m_qName);
qProjecte.setAttribute(QString("LogEnabled"),m_bLogEnabled ? QString("true") : QString("false"));


The XML file stores it like:


<Project LogEnabled="false" Name="Basler 1" Version="2.5" >


Is there anyway to force the attributes to be stored in the order that I add them?

Thanks,

talk2amulya
12th May 2009, 14:09
as u set attributes, they are added on a stack and then written into xml..so add the attributes in the opposite order as you write them in code.
but QXmlStreamWriter writes in xml just like in code.so if you use that one, you can add in exactly the same order in you require

^NyAw^
12th May 2009, 15:37
Hi,



as u set attributes, they are added on a stack and then written into xml..so add the attributes in the opposite order as you write them in code


The attributes are sotred randomized instead of inverted order as you said.

Thanks,

Lykurg
12th May 2009, 16:14
The attributes are sotred randomized
Yepp, they are stored in a QHash (QDomNamedNodeMapPrivate::map).

^NyAw^
12th May 2009, 17:02
Hi,

So? I'm not getting them sorted, nor as I'm inserted them.

Lykurg
12th May 2009, 17:05
You can't sort them. Unfortunately.