如何用Qt5.4 QXmlStreamWriter压缩名称空间前缀

时间:2015-05-18 16:30:31

标签: xml qt qt5 xml-namespaces qt5.4

我有这个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不使用前缀&#34; mc:&#34;在每个元素名称之前?

1 个答案:

答案 0 :(得分:1)

QXmlStreamReader :: setNamespaceProcessing()就是答案:

  reader.setNamespaceProcessing(false);