如何使用 Qt5.4 QXmlStreamWriter 抑制命名空间前缀

how to suppress namespace prefix with Qt5.4 QXmlStreamWriter

我有这个dictionary.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>

DictionarySet 元素的 xmlns 属性被颠倒了,但我认为这无关紧要。更大的问题是,我可以让 QXmlStreamWriter 在每个元素名称之前不使用前缀 "mc:" 吗?

QXmlStreamReader::setNamespaceProcessing() 是答案:

  reader.setNamespaceProcessing(false);