PDA

View Full Version : Writing a XML file



Djony
5th February 2007, 12:40
Hi!
I would like to create and write into XML file. The file should look like this:


<tcdescription>
<platform type=something>
<id>something</id>
<owner>something</owner>
<requirement>something</requirement>
<type id=something></type>
<style type=something></style>
<module>something</module>
<level id=something></level>
<effort unit=something>something</effort>
<startertimeout>something</startertimeout>
<description>something </description>
</platform>

<platform type=something os=something>
<activated status=something></activated>
<timeout>something</timeout>
<trace use_automatic=something></trace>
<debug use_automatic=something></debug>

<compilation type=something" default=something>
<executable>something</executable>
</compilation>
<compilation type=something>
<executable>something</executable>
</compilation>
</platform>

<platform type=something os=something>
<activated status=something></activated>
<timeout>something</timeout>
<trace use_automatic=something></trace>
<debug use_automatic=something></debug>

<compilation type=something default=something>
<executable>something</executable>
</compilation>
<compilation type="release">
<executable>something</executable>
</compilation>
</platform>
</tcdescription>

Now, I have successfully created XML file and wrote tcdescription tag with this code:

QDomDocument doc("XML");
QDomElement root=doc.createElement("tcdescription");
doc.appendChild(root); and wrote it to QTextStream and to qfile. But I don't know how to create "platform" tag. I know that I need to do with QDomelement, but I am kind of stuck, and couldn't find the solution in documentation. Any ideas how to create "platform" tag, as an element child to "tcdescription" tag?

wysota
5th February 2007, 13:10
QDomDocument doc("xml");
QDomElement root = doc.createElement("tcdescription");
QDomElement platform = doc.createElement("platform"); // the same for next levels
root.appendChild(platform); // here you attach a node to a parent
doc.appendChild(root);

mcosta
5th February 2007, 13:11
You can use the function QDomNode::appendChild.
In example


QDomElement platform;
platform.setTagName("platform");
platform.setAttribute("type", "something");
root.appendChild(platform);

Djony
5th February 2007, 15:45
Thank you for your answers. Now I have different problem. When I run this code:

style = doc.createElement("style");//doc is QDocDocument
style.setAttribute("type", "something");
commonPlatform.appendChild(style); //commonPlatform is QDomEllement


I get this output in my xml file:
<style type="something" /> And I would like to have "/style" closing of tag. How can I do that?

wysota
5th February 2007, 16:01
I don't think you can do it directly using DOM.

Djony
5th February 2007, 16:05
What is the alternative then?

jpn
5th February 2007, 16:09
What difference does it make? :) A separate ending tag is only needed when there are child elements. And you'll get one in case there are.

wysota
5th February 2007, 16:23
What is the alternative then?

For example you can insert ending tags using regular expressions. But I don't see why would anyone want to do it...