Victor
Victor

Reputation: 8480

Writing XML code using QXmlStreamWriter

How can I Write a XML text inside a XML?

QXmlStreamWriter xmlWriter(&file);
(...)
QString xmlCode = "This is a <b>XML</b> code. And should be written as is it";
xmlWriter.writeStartElement("start");
xmlWriter.writeCharacters(xmlCode);
xmlWriter.writeEndElement();

The output should be:

<start>
    This is a <b>XML</b> code. And should be written as is it
</start>

Upvotes: 2

Views: 7245

Answers (1)

Aleksey Kontsevich
Aleksey Kontsevich

Reputation: 4991

Yes, you can by using the write method on the QXmlStreamWriter's device directly; example:

QXmlStreamWriter xmlWriter(&file);
xmlWriter.writeStartElement("start");
xmlWriter.writeCharacters("");  // This will open and close <start> tag correctly
xmlWriter.device()->write(xmlCode.toLatin1().constData(), xmlCode.length());
xmlWriter.writeEndElement();

It was also suggested here: http://www.qtcentre.org/threads/60045-writing-raw-data-into-xml-sub-node

Upvotes: 4

Related Questions