0

我有这个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:”吗?

4

1 回答 1

1

QXmlStreamReader::setNamespaceProcessing() 是答案:

  reader.setNamespaceProcessing(false);
于 2015-05-18T20:56:08.243 回答