使用 Xerces 2.6 序列化汉字

发布于 2024-09-04 16:41:28 字数 581 浏览 5 评论 0原文

我有一个编码为 UTF-8 的 Xerces (2.6) DOMNode 对象。我通常这样读取它的 TEXT 元素:

CBuffer DomNodeExtended::getText( const DOMNode* node ) const {
  char* p = XMLString::transcode( node->getNodeValue( ) );
  CBuffer xNodeText( p );
  delete p;
  return xNodeText;
}

CBuffer 是一个缓冲区对象,它最近像在数据库中一样被持久化。

直到 TEXT 中只有常见的 ASCII 字符时,这种情况才会起作用。如果我们有中文的,它们就会在转码操作中丢失。

我用谷歌搜索了很多寻找解决方案。看起来 Xerces 3 中的 DOMWriter 类应该可以解决这个问题。使用 Xerces 2.6,我尝试使用 XMLTranscoder,但尚未成功。有人可以帮忙吗?


编辑 看起来我错了,DOMWriter 类已经在 Xerces 2.6 中可用。我现在正在尝试基于它的解决方案。

I have a Xerces (2.6) DOMNode object encoded UTF-8. I use to read its TEXT element like this:

CBuffer DomNodeExtended::getText( const DOMNode* node ) const {
  char* p = XMLString::transcode( node->getNodeValue( ) );
  CBuffer xNodeText( p );
  delete p;
  return xNodeText;
}

Where CBuffer is, well, just a buffer object which is lately persisted as it is in a DB.

This works until in the TEXT there are just common ASCII characters. If we have i.e. chinese ones they get lost in the transcode operation.

I've googled a lot seeking for a solution. It looks like with Xerces 3, the DOMWriter class should solve the problem. With Xerces 2.6 I'm trying the XMLTranscoder, but no success yet. Could anybody help?


Edit
It looks I was wrong and the DOMWriter class is already available in Xerces 2.6. I'm now trying a solution based on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2024-09-11 16:41:28

不要使用转码方法。该文档明确指出,它将文本转换为“本机代码页” - 这几乎总是有损操作。

Don't use the transcode method. The documentation clearly states that it translates the text to the "native code-page" - which is almost always a lossy operation.

三人与歌 2024-09-11 16:41:28

我现在已经解决如下。我仍然不确定这是否是最佳解决方案

CBuffer DomNodeExtended::getText( const DOMNode* node ) const
{
  XMLCh tempStr[100];  
  XMLString::transcode("LS", tempStr, 99);
  DOMImplementation *impl = 
      DOMImplementationRegistry::getDOMImplementation(tempStr);
  DOMWriter* myWriter = ((DOMImplementationLS*)impl)->createDOMWriter();
  XMLCh *strNodeValue = myWriter->writeToString(*node);

  XMLTransService::Codes resCode;  
  XMLTranscoder* t = 
      XMLPlatformUtils::fgTransService->makeNewTranscoderFor(
      "UTF-8", resCode, 16*1024);

  unsigned int charsEaten = 0;  
  unsigned int charsReturned = 0;
  char bytesNodeValue[16*1024+4];  
  charsReturned = t->transcodeTo( strNodeValue,
                                  XMLString::stringLen(strNodeValue),
                                  (XMLByte*) bytesNodeValue,
                                  16*1024,
                                  charsEaten,
                                  XMLTranscoder::UnRep_Throw);

  CBuffer xNodeText( bytesNodeValue, charsReturned);

  XMLString::release(&strNodeValue);  
  myWriter->release();
  delete t;

  return xNodeText;
}

I've now solved it as follows. I'm still not sure this is the optimal solution though

CBuffer DomNodeExtended::getText( const DOMNode* node ) const
{
  XMLCh tempStr[100];  
  XMLString::transcode("LS", tempStr, 99);
  DOMImplementation *impl = 
      DOMImplementationRegistry::getDOMImplementation(tempStr);
  DOMWriter* myWriter = ((DOMImplementationLS*)impl)->createDOMWriter();
  XMLCh *strNodeValue = myWriter->writeToString(*node);

  XMLTransService::Codes resCode;  
  XMLTranscoder* t = 
      XMLPlatformUtils::fgTransService->makeNewTranscoderFor(
      "UTF-8", resCode, 16*1024);

  unsigned int charsEaten = 0;  
  unsigned int charsReturned = 0;
  char bytesNodeValue[16*1024+4];  
  charsReturned = t->transcodeTo( strNodeValue,
                                  XMLString::stringLen(strNodeValue),
                                  (XMLByte*) bytesNodeValue,
                                  16*1024,
                                  charsEaten,
                                  XMLTranscoder::UnRep_Throw);

  CBuffer xNodeText( bytesNodeValue, charsReturned);

  XMLString::release(&strNodeValue);  
  myWriter->release();
  delete t;

  return xNodeText;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文