Removing !DOCTYPE from the XML header
-
wrote on 20 Sept 2011, 17:52 last edited by
Hi All
I am generatiing a xml file using QDomDocument.
@QDomDocument mainDoc("?xml version=1.0?");@The above line of code generates the xml title as below
<!DOCTYPE ?xml version=1.0?>I want to generate header as below
<?xml version=1.0?>
How to do that? -
wrote on 20 Sept 2011, 22:07 last edited by
You're doing it wrong™
bq. "QDomDocument::QDomDocument ( const QString & name )":http://doc.qt.nokia.com/4.7/qdomdocument.html#QDomDocument-2
Creates a document and sets the name of the document type to name.So, you're setting the root tag name to the string "?xml version=1.0?".
Most probably this is what you want:
@
QDomDocument dom;
QDomElement root = dom.createElement("MyRoot");
dom.appendChild(root);
@ -
wrote on 21 Sept 2011, 02:38 last edited by
HI volker
This is not what i want to do.
I want to set document name to <?xml version=1.0?> -
wrote on 21 Sept 2011, 05:37 last edited by
What do you mean with "document name"?
A valid XML file always starts with <? xml version="1.0" ?>. There is no way to change that without producing an invalid XML file. The root element - which can be seen as the "document name" - can be chosen freely but may only consist of a "limited set":http://www.w3.org/TR/2008/REC-xml-20081126/#NT-NameStartChar of characters; it must not contain ?.
-
wrote on 21 Sept 2011, 06:06 last edited by
@Lukas, I think that Rajiveer is aware that a valid document is
bq. A valid XML file always starts with <? xml version=“1.0” ?>
It seems that his problem is that for some reasons he should manage a non-valid xml file with the !DOCTYPE included in the header to transform it in a valid XML document.
If the data are in someway already in a stream it is possible to convert to QStream and apply some transform i.e. split the bad heather and remove the wrong characters, or use a string substitution api or something like this before saving the line somewhere.The strange question is that QDomDocument creates a wrong header ...
-
wrote on 21 Sept 2011, 06:15 last edited by
In the "QDomDocument class documentation":http://developer.qt.nokia.com/doc/qt-4.7/qdomdocument.html page you can read:
To create a document using DOM use code like this:
@
QDomDocument doc("MyML");
QDomElement root = doc.createElement("MyML");
doc.appendChild(root);QDomElement tag = doc.createElement("Greeting");
root.appendChild(tag);QDomText t = doc.createTextNode("Hello World");
tag.appendChild(t);QString xml = doc.toString();
@
It seems that you stop to only the first step, so the valid XML document is not yet created until you append the nodes to - as I see from your example - Probably what you see is the pure string built by the class still incomplete. Try to add a fake node and close the sequence as in the example above (from the QDomDocument class documentation) to see if it works.
Reading the documentation of this class it seems too that if you create the XML and add something wrong or do not add those stuff that it is expected the object is deleted. -
wrote on 21 Sept 2011, 06:22 last edited by
Well.
Be aware that if you pass a QString to the QDomDocument constructor you will set the "document type":http://www.w3.org/TR/xml/#dt-doctype, not the name of the root element nor any processing instruction (<? ... ?>).
@
QDomDocument domDocument;QDomProcessingInstruction xmlProcessingInstruction = domDocument.createProcessingInstruction("xml", "version=\"1.0\""); domDocument.appendChild(xmlProcessingInstruction); QDomElement rootElement = domDocument.createElement("root"); domDocument.appendChild(rootElement); QDomElement childElement = domDocument.createElement("child"); rootElement.appendChild(childElement); qDebug() << domDocument.toString(); <?xml version=1.0?> <root> <child/> </root>
@
-
wrote on 21 Sept 2011, 06:31 last edited by
@Lukas: in the example above it seems that the string is only to save the final xml file, is it true?
About this, then a pure QString can be used as source in a XmlListModel as you know ?
-
wrote on 21 Sept 2011, 06:38 last edited by
[quote author="Alicemirror" date="1316586696"]@Lukas: in the example above it seems that the string is only to save the final xml file, is it true?[/quote]
I assume you are referring to the example in your post as mine has been added later on.
To get the textual representation ("a XML file") of the in-memory DOM tree one might either use QDomDocument()::toString() or QDomNode()::save() (QDomDocument is-a QDomNode) which both should yield the same results.
The QString can be written to a file or passed to a XmlListModel (as .xml, not as .source).
-
wrote on 21 Sept 2011, 06:47 last edited by
:) Sure, the trick is the use of .xml instead of .source.
Many thanks for you clarifications. Hope it will be useful for Raijveer too.
-
wrote on 21 Sept 2011, 07:31 last edited by
It might be confusing that QDomDocument does not contain an XML declaration per default, but it is not required as per the XML standard. I've added a doc note on this to QDomDocument.
-
wrote on 21 Sept 2011, 07:36 last edited by
Meny thanks Lukas !
-
wrote on 21 Sept 2011, 19:01 last edited by
A complete example to create an XML document including the XML declaration as a processing instruction follows:
@
QDomDocument dom;
QDomProcessingInstruction xmlHeaderPI = dom.createProcessingInstruction("xml", "version="1.0" " );
dom.appendChild(xmlHeaderPI);
QDomElement root = dom.createElement("MyRoot");
dom.appendChild(root);
@Also, be aware of the implications induced by the toString() method of QDomNode (and thus QDomDocument). It returns the text representation as UTF-8 string, although you might not want it encoded that way. The most reliable way is to use the method
@
QDomNode::save ( QTextStream & str, int indent, EncodingPolicy encodingPolicy )
@It respects the encoding settings. You might wan to use a QBuffer or the like as the "device" for the text stream.
-
wrote on 21 Sept 2011, 19:41 last edited by
@Volker: Thanks you, very very useful. I am just working with a multilanguage aplication including Turkis.
1/14