1

I need to create XML file in QT, but i dont use QT XML classes to create the XML data, but i create strings manually (which contains XML) and write it into the file, the reason for not using QT XML classes is, i need to preserve the order of attribute but if i use QT XMl classes it writes attribute in random order.

Everything was fine up to now, until i get the html text to be written as tag value in XML. i need to write HTML data as the QT XMl classes writes for e.g "This is <Test data>" should be written as "This is &lt:Test data&gt". here i have replaced the ";" with ":" for understanding purpose.

can anyone help me with any function in QString which can detect the XMl and convert it before writing into the file or while writing into file?

maxchirag
  • 539
  • 1
  • 9
  • 18
  • 1
    Note that the XML specification says that [the order of attributes doesn't matter](http://www.w3.org/TR/xml/#sec-starttags) and therefore XML handlers don't bother to maintain the order when processing XML content. The *fix* (not workaround) would be to correctly read the XML - if you have access to the code that does that. – larsmoa Jul 06 '11 at 05:59
  • hello, thanks, i know the order of attribute doesn't matter, but the XML files i create from my project are used by other projects (may not be created in QT) hence they asked me to preserve the attribute order. so i have to anyway do it now. – maxchirag Jul 06 '11 at 06:15
  • @maxchirag: Did you try QXmlStreamWriter? It writes directly to the underlying I/O device when you call writeAttribute() so the attributes will appear in the order you write them in the code. – Frank Osterfeld Jul 06 '11 at 06:53

2 Answers2

4

Check QXmlStreamReader and QXmlStreamWriter classes. They are in QtCore and really useful for XML handling.

You should do something like this:

QString string;
QXmlStreamWriter writer(&string);
// use QXmlStreamWriter class to generate the XML
writer.setAutoFormatting(true);
writer.writeStartDocument();
...
writer.writeStartElement("html");
writer.writeStartElement("a");
writer.writeAttribute("href", "http://example.com/");
writer.writeCharacters("My wonderful link");
writer.writeEndElement(); // a
writer.writeEndElement(); // html
...
writer.writeEndDocument();
Drise
  • 4,310
  • 5
  • 41
  • 66
Naszta
  • 7,560
  • 2
  • 33
  • 49
  • oppss... sorry, i forgot to mention that i use Qt Version 4.1.4 due to project reasons, and QXmlStreamReader is probably not introduced before Qt4.3.... thanks for your reply anyway. – maxchirag Jul 08 '11 at 04:24
  • @maxchirag: why do you use this really old library? – Naszta Jul 08 '11 at 08:30
0

Since your problem is that you can't use the QT XML classes due to its behaviour, I'm assuming you don't have a problem with using the QT XML module in your project. The only way, I can think of detecting is, validating the code and if it validates as valid XML (syntax only).

QString test = "<TestData>Valid XML</TestData>";
QString wrapper_start = "<?xml version=\"1.0\"?><dummyrootelement>";
QString wrapper_end = "</dummyrootelement>";
QDomDocument document;
if(document.setContent(wrapper_start + test + wrapper_end)) {
    // Valid XML, handle test here
} else {
    // Probably not valid XML
}

The reason you need wrapper_start and wrapper_end variables is since you may be handling XML fragments in between, you might be considering this as XML that needs to be processed:

<TestData>Data1</TestData><TestData>Data2</TestData>

but a parse error will be caused on this as an XML document needs to have a single root element.

Also, do note that, the above method is not a recommended by me, but I have mentioned it because I understand the pain and sympathize with anyone who has to maintain legacy systems. If your application relies on the ordering of attributes, I strongly suggest that you at least consider revising the architecture of your application.

Rohan Prabhu
  • 7,180
  • 5
  • 37
  • 71
  • Hello Rohan, thank you for the comment, but first thing is that, in my project we use full XML so we dont have that issue with our native files, its just issue for some XML files which my tool generate(as output) for other tools. because of those tools restriction we manually write XML using strings. so i have to any make sure that the files my tool writing are valid xml and preserve the order of attributes. what you have suggested can help detect the issue, but how do i actually resolve it? – maxchirag Jul 06 '11 at 06:50
  • Once you have detected the issue, if you note, in the section I have marked as "//Valid XML, handle test here", you can process the XML string as you wish, because the string hasn't been modified at all and the attribute order is unchanged. The only thing is that now, you know for a fact that the variable is indeed, XML. – Rohan Prabhu Jul 06 '11 at 08:51