我有这个词典。xml:
<?xml version="1.0" encoding="UTF-8"?>
<DictionarySet xmlns:mc="urn:fmosoft-map-creator" xmlns="urn:fmosoft-map-creator" Version="1">
<Dictionary SourceLanguage="en_US" SourceLanguageIsPredefined="true" TargetLanguage="es" TargetLanguageIsPredefined="true">
<Translation Source="asdf" Target="fdsa"/>
<Translation Source="xyz" Target="jkl"/>
</Dictionary>
<Dictionary SourceLanguage="en_US" SourceLanguageIsPredefined="true" TargetLanguage="pt" TargetLanguageIsPredefined="true">
<Translation Source="asdf" Target="wer"/>
<Translation Source="xyz" Target="poi"/>
</Dictionary>
</DictionarySet>
我想使用QXMLStreamReader和QXMLStreamWriter复制文件,因此我可以在整个副本中插入新元素。这是我简单地复制文件的代码(一旦工作起作用,我将在循环中插入代码以添加途中的其他元素):
QXmlStreamWriter writer(&output);
if (!output.open(QIODevice::WriteOnly | QIODevice::Text)) {
throw std::runtime_error("Unable to open the file for writing.");
}
writer.setAutoFormatting(true);
writer.writeDefaultNamespace("urn:fmosoft-map-creator");
while (!reader.atEnd()) {
writer.writeCurrentToken(reader);
reader.readNext();
}
这会产生:
<?xml version="1.0" encoding="UTF-8"?>
<mc:DictionarySet xmlns="urn:fmosoft-map-creator" xmlns:mc="urn:fmosoft-map-creator" Version="1">
<mc:Dictionary SourceLanguage="en_US" SourceLanguageIsPredefined="true" TargetLanguage="es" TargetLanguageIsPredefined="true">
<mc:Translation Source="asdf" Target="fdsa"/>
<mc:Translation Source="xyz" Target="jkl"/>
</mc:Dictionary>
<mc:Dictionary SourceLanguage="en_US" SourceLanguageIsPredefined="true" TargetLanguage="pt" TargetLanguageIsPredefined="true">
<mc:Translation Source="asdf" Target="wer"/>
<mc:Translation Source="xyz" Target="poi"/>
</mc:Dictionary>
</mc:DictionarySet>
字典集元素的XMLN属性是反转的,但我认为这并不重要。更大的问题是,我可以让QXMLStreamWriter不使用每个元素名称之前使用前缀" MC:"?
qxmlStreamReader :: setNamespaceProcessing()是答案:
reader.setNamespaceProcessing(false);