【问题标题】:XercesC setting output to UTF-8XercesC 将输出设置为 UTF-8
【发布时间】:2013-06-20 04:31:03
【问题描述】:

我正在使用 XercesC Lib 来创建我的数据的序列化。如何将其设置为 UTF-8?它总是使用 UTF-16 生成的,我找不到改变它的方法。

xercesc::DOMImplementation *gRegistry = xercesc::DOMImplementationRegistry::getDOMImplementation(X("Core"));
xercesc::DOMDocument *doc = gRegistry->createDocument(
        0,                      // root element namespace URI.
        X(oDocumentName.c_str()),       // root element name
        0);                 // document type object (DTD).
doc->setXmlStandalone(true);
... prepare the document ...
serializer = ((xercesc::DOMImplementationLS *)gRegistry)->createLSSerializer();
serializer->setNewLine(xercesc::XMLString::transcode("\n"));

XMLCh *xmlresult = serializer->writeToString(doc);
char *temp = xercesc::XMLString::transcode(xmlresult);
std::string result(temp);

xercesc::XMLString::release(&temp);
xercesc::XMLString::release(&xmlresult);
doc->release();
serializer->release();
getStream() << result.c_str();

当我在 Java 端使用 JAXB 反序列化时,我总是得到一个 content is not allowed in prolog,到目前为止,这是我在 XML 中看到的唯一区别。当我尝试在 JAXB 中进行本地反序列化时,它可以工作。当我使用我的 XercesC XML 时,我得到了这个错误。当我尝试使用 XML 插件在 Notepad++ 中对其进行格式化时,它还说有错误,但没有告诉我任何细节。

【问题讨论】:

标签: c++ xerces-c


【解决方案1】:

检查DOMLSOutput 的使用情况,这应该会为您提供您想要的。 IE。你创建了一个 DOMLSOutput 对象来写入(而不是使用 DOMLSSerializer::writeToString)。

【讨论】:

  • 是的,我目前正在调查这个。根据此文档ibm.com/developerworks/library/x-serial.html,序列化程序只能执行 UTF-16。我正在尝试实现MemBufFormatTarget,但我不知道如何用它来序列化文档。
  • 几乎你会创建一个MemBufFormatTarget 并将其连接到DOMLSOutput,然后将DOMLSSerializer::write 连接到DOMLSOutput 对象(DOMNode 是文件)。然后你调用MemBufFormatTarget::getRawBuffer 并使用TranscodeFromStr 来获取你的XMLCh 字符串。
  • 谢谢。 TranscodeFromStr 是缺失的部分。现在可以了。 :)
猜你喜欢
  • 1970-01-01
  • 2016-10-11
  • 2019-04-12
  • 2018-03-04
  • 1970-01-01
  • 2012-03-10
  • 2012-02-01
  • 1970-01-01
  • 2019-08-04
相关资源
最近更新 更多