如何在XML中编写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();
输出应为:
<start>
This is a <b>XML</b> code. And should be written as is it
</start>
答案 0 :(得分:2)
是的,你可以! :)我使用的Variant,工作得很好:
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();
此处还建议:http://www.qtcentre.org/threads/60045-writing-raw-data-into-xml-sub-node
答案 1 :(得分:-1)
你不能,QXmlStreamWriter将逃避少于,引号和&符号,并且它不提供用于编写不会被转义的文本的机制。你有两个选择,一个是尽可能分解写xmlCode(如果不是因为它是从其他来源传入的,请继续阅读选项2)。
QString xmlCode1 = "This is a ";
QString xmlCode2 = "XML";
QString xmlCode3 = " code. And should be written as is it";
xmlWriter.writeStartElement("start");
xmlWriter.writeCharacters(xmlCode1);
xmlWriter.writeStartElement("b");
xmlWriter.writeCharacters(xmlCode2);
xmlWriter.writeEndElement();
xmlWriter.writeCharacters(xmlCode3);
xmlWriter.writeEndElement();
第二个选项是通过继承QXmlStreamWriter
并添加将直接写入QIODevice
的方法来自行添加行为。这是另一个人问的问题,这个问题类似但涉及not-escaping quotes。我已经链接到的答案看起来很容易被修改为不会逃避小于(或大于)。