使用 Xerces 2.6 序列化汉字
我有一个编码为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用转码方法。该文档明确指出,它将文本转换为“本机代码页” - 这几乎总是有损操作。
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.
我现在已经解决如下。我仍然不确定这是否是最佳解决方案
I've now solved it as follows. I'm still not sure this is the optimal solution though