Generating an XML file
-
I am trying to generate an XML file using the xml streaing classes in Qt. I running into a problem. Qt's XML writer's documentation is too technical for me. I can write a XML file by brute force but I rather not.
So my question, anyone know where I can get a pragmatic tutorial on XML? I did some searching around and have not found anything useable yet.
Thanks,
Arukas[fixed typo in title, Eddy]
-
Let me add some more to that. I have read some more tutorials and I'm finding that I am thinking I am having more problems with the XML Streaming than the actual XML.
I found some XML streaming examples, and they don't do what I am looking for. It's putting everything on the same line. I'd like the tags and comments to be on their own line. I like to add roots, but I can't figure that out.
About all I can do is start an XML document and add an element. I can't really figure anything else out. The XML file I am working on is pretty simple, but I can't figure out how to make XML streaming work for me.
-Arukas
-
Here is a sample XML
@<?xml verision="1.0"?>
<!-- Space for Rent -->
<Forest>
<Number_of_Trees>200</Trees>
<Tree_1 bark="mossy" leaves="none" nest="lots"/>
<Animals>
<!-- Legally distinguable from Bambi -->
<Rabbit>Thumperette</Rabbit>
<Skunk>Rose</Skunk>
</Animals>
</Forest>@Here is how I started with the code:
@QXmlStreamWriter writer(&file);
writer.setAutoFormatting(true);
writer.writeStartDocument("1.0");
writer.writeComment("Space for Rent);@That is as far as I have gotten. I can't figure out how to make the <Forest> and <Animals> parts. I don't know what function covers that. I played around with the functions and still haven't figured out how to make it what I want to do.
When I open it up in a text editior, everything is on the same line, which will ultimately make it hard to read for humans. And I haven't messed with the other stuff much because if I can't do the roots and sub roots, I'll have to use another method.
-Arukas
-
Well, you could continue like this:
@
writer.writeStartElement("Forest");
writer.writeTextElement("Number_of_Trees", QString::number(200));
writer.writeStartElement("Tree_1");
writer.writeAttribute("bark", "mossy");
writer.writeAttribute("leaves", "none");
writer.writeAttribute("nest", "lots");
writer.writeEndElement(); //Tree_1
//...
writer.writeEndElement(); //Forest;
writer.writeEndDocument(); // End of document
@Note: code not tested, just typed into the forum reply...
-
I appreciate the help. That got me started on what I need to do, and works great. However, I still have one problem.
This line
@writer.setAutoFormatting(true);@The description says it suppose to add line breaks to add to the readability. However, it does not add any line breaks. All I can figure out is how to adjust the number of spaces.
Thanks,
Arukas -
I've been using notepad to open files. I tried wordpad and it works fine.
Now that I'm thinking, I had a problem like this before. My understanding, in the past you needed two things to get to the next line. You needed a carriage return to get back to the beginning of the line and a line feed to start a new line. Although, I'm not sure how to correct it. Only reason I think I know this is I was doing something in Linux and tried it in Windows and wondered why it didn't work.
I think it, is how I am opening the file. This is the only way I know how to open files in Qt. Is a different way to correct the problem.
@ QString filename="F.xml";
QFile file;
file.setFileName(filename);@ -
There are three kinds of marking a new line:
- LF (\n)
a single newline character
the usual way on Unix, Linux and Mac OS X - CRLF (\r\n)
a carriage return followed by a newline
that's commonly used on Windows - CR (\r)
a single carriage return
that was used by Mac OS up to version 9
See wikipedia on "Newline":http://en.wikipedia.org/wiki/Newline for some more details.
Notepad is known to work only with the traditional Windows CRLF line endings, while Wordpad is able to handle pure LF too. I personally avoid Notepad completely for that purpose.
Regarding your QFile questions:
You can combine the first two lines together into one:
@
QFile file("F.xml");
@A possible solution to the line ending problem could be to open the file in text mode:
@
QFile file("F.xml");
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
// handle the error here
}
@bq. *The QIODevice::Text docs state:"
When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.Make sure that the software/classes reading your XML can handle the CRLF too.
I personally avoid the QIODevice::Text flag, though. It's rarely needed nowadays.
- LF (\n)
-
[quote author="Arukas" date="1323216964"]I've been using notepad to open files. I tried wordpad and it works fine.[/quote]
Might I suggest you get yourself a decent text editor, like "Notepad++":http://notepad-plus-plus.org/ perhaps?