[Help] XML element's attribute

问题描述:
我在把一个对象serialize to XML document, 然后store in database, 在需要用到这个对象的时候deserialize, 用document的element attribute来表示该对象的一些数据. 但是如果该数据是一些中文的话, xml解析就有可能出错. 由于我是用UTF-8编码的, 进入数据库以后, 再取出来, 就变乱码了. 但是xml可以只要可以正确的解析, 就没有问题. 但是一些特殊的字符会出现问题.
比分说 一个片断:
<person localFamilyName="王" .../>
从数据库出来以后, 解析器就会认为"王"里面含有一个引号.

附加信息:
但是如果是用子节点 + CDATA的方式就一切OK. 但是已经写了很多代码了, 不可能再改.
郁闷中, 找了不少资料, 好像说是用转义符合来写属性值 (以#&开头) 不知道在java里面怎样写?

环境:
JDK 1.3.1, MSSQL Server, JDOM.

请问大家有没有类似的遭遇?

是这样,我曾经用


&x000D;&x000A;在XML属性中表示回车换行符,效果不错。

怎样用API来进行这样的编码?

不理解你的问题

一个猜想: 是否可以用java属性文件(.properties)处理中文字符的方式, 将中文字符转化为UNICODE的转义符(\uxxxx的形式), 然后在XML中保存?

To jxb8901,
谢谢你的想法, 但是我想依靠现有的XML的方法去实现.

To Banq,
我的意思是有没有什么现成的关于XML的API可以做这样的转换, 在javadoc里面关于org.w3c.dom.Element.setAttribute()的方法是这样描述的:

Adds a new attribute. If an attribute with that name is already present in the element, its value is changed to be that of the value parameter. This value is a simple string; it is not parsed as it is being set. So any markup (such as syntax to be recognized as an entity reference) is treated as literal text, and needs to be appropriately escaped by the implementation when it is written out. In order to assign an attribute value that contains entity references, the user must create an Attr node plus any Text and EntityReference nodes, build the appropriate subtree, and use setAttributeNode to assign it as the value of an attribute.

说明这个转换是可以依靠实现类来做的, 我用的XML Parser是Xerces2 2.4.0. 但是它的文档里面没有提到这个问题, 你以前写的时候是用自己编码解决的吗? 还是用其他的Parser来解决的?

谢谢!

我觉得你解决问题的方向好象已经不对了, 从你的描述来看, XML文件出现乱码导致解析出错, 不是XML解析器本身的问题, 因为XML规范本身就是支持多字节字符集的, 你的问题不是出在解析上, 而是出在典型的"中文问题"上, 也就是进出数据库的转码不对.

我认为只要保证从数据库取出的数据是UTF-8编码, 且取出的XML文件的字符集宣告也是UTF-8, 那么解析绝对不应该出问题, 我认为你可以以此为切入点查找问题的根源所在.

有不对的地方请多多指教!

谢谢 jxb8901,
在你的提醒下, 我追踪了有牵涉到字符转换的代码, 最后发现是在用write Document to OutputStream的时候, 生成的OutputStream.toString()没有指明编码, 改成了OutputStream.toString("UTF-8") 就OK了.

谢谢! //bow

jxb8901说的比较对,我们是设置成UTF-16 使用Jdom来解析XML,回车符是用我上面的处理方法,最后再导出,一切正常。

XML好像很简单,但是其属性attribute确实很难用,建议尽量不用attribute。使用


<xx>你的值</xxxx>

To banq
借这个帖子请教一个问题, 如果一个element里面嵌套了一些元素, 我的习惯是用属性来表明一些主键, 而不是用textNode, 比如说输出某个部门下的所有员工:


<org orgId="HR">
<employee employeeId=
"E0001">
<name>Bruce Lee</name>
<gender>male<gender>
</employee>
<employee employeeId=
"...">
...
</employee>
</org>

如果是按照你所建议的用TextNode, 那么是否是写成这样:


<org>
<orgId>HR</orgId>
<employee>
<employeeId><employeeId>
<name></name>
<gender></gender>
</employee>
...
</org>

我觉得第一种看起来结构清晰一点, 程序处理起来也方便一些. 用第二种方式的话是否有别的结构? 有什么样的优点?

好像代码被过滤掉了, 重新贴一次:
1:


<org orgId="HR">
<employee employeeId=
"E0001">
<name>Bruce Lee</name>
<gender>male<gender>
</employee>
<employee employeeId=
"...">
...
</employee>
</org>

2:

<org>
<orgId>HR</orgId>
<employee>
<employeeId><employeeId>
<name></name>
<gender></gender>
</employee>
...
</org>

我是个新手,说的不对大家别笑话。
曾经将一个属性中带中文的XML文件解析成了一个字符串,属性所有的"都是用\"来在字符串中转义的。数据库中的数据编码格式决定了取出数据要做相关的转换,使用new String (myDBData.getBytes(),"GBK")转化一下可以正确编码。
如果要用流的方式读取,建议使用ByteArrayInputStream。

觉得在入库前将中文字段编一下码:new String(myDBData.getBytes(),"ISO8859_1"),然后取的时候new String (myDBData.getBytes("ISO8859_1"),"GBK"),可能更保险些。
具体的字符集使用和数据库可能还有关系,曾经在8i下没问题的程序在9i下就出现过乱码,可能是内码的问题。
另外,XML文件的头一句指明编码格式应该很重要。

to wys1978

对于属性是ID之类 当然使用你的第一种方法比较好,但是如果不是Id这样小整数的属性值,有点模糊不能确定的 都应该采取第二种方式。

Hi all,
Really appreciate the discuss here, my incoming project will use XML intensively, but I have no experience in dealing with Chinese characters in XML, so I hope the discussion continue and I will benefit from it.

But put aside Chinese related issues, I think Banq's statement about attribute vs. element are biased. Yes, there are obvious advantage of using element: use of <!{CDATA[; specify data type and NULL if you are using Schema etc.

But there are also lots of benefit of using attribute:
1> namespace, unless you are using W3c schema, you have to becare to keep name unqiue as that's required in DTD
2> if you have a text be a element, you can only declare "ANY" if you want to extend it for sub element
3> performance, if you understand how DOM works, parsing more node will slow down processing

I was also struggling about attr vs. element when I started to use XML, my personal perference now is still try to use attr if it's straight forward basic data, text, integer,whatever

One time I was going to switch to element as I was using JAXB to automate the JAVA to XML binding, but then I realize that most of time I don't have to bother to deal with JAXB crap, JDOM is good enough and simple for most of my work.


my $0.02
-Wanchun