如何将字符从Oracle编码到XML? [英] How to encode characters from Oracle to XML?
问题描述
在我的环境中,我使用Java将结果集序列化为XML。
它基本上是这样的:
In my environment here I use Java to serialize the result set to XML. It happens basically like this:
//foreach column of each row
xmlHandler.startElement(uri, lname, "column", attributes);
String chars = rs.getString(i);
xmlHandler.characters(chars.toCharArray(), 0, chars.length());
xmlHandler.endElement(uri, lname, "column");
Firefox中的XML如下所示:
The XML looks like this in Firefox:
<row num="69004">
<column num="1">10069</column>
<column num="2">sd</column>
<column num="3">FCVolume </column>
</row>
但是当我解析XML时,我得到一个
But when I parse the XML I get the a
org.xml.sax.SAXParseException:字符引用&#26 是
无效的XML字符。
org.xml.sax.SAXParseException: Character reference "" is an invalid XML character.
我现在的问题是:我必须替换哪些特征,或者如何编码我的字符,这将是有效的XML?
My question now is: Which charactes do I have to replace or how do I have to encode my characters, that they will be valid XML?
推荐答案
我在 Xml Spec :
根据该列表,它不鼓励使用字符#26(Hex:#x1A )。 / p>
I found an interesting list in the Xml Spec: According to that List its discouraged to use the Character #26 (Hex: #x1A).
也不鼓励
以下范围中定义的字符。
他们是控制字符或
永久未定义Unicode
字符
The characters defined in the following ranges are also discouraged. They are either control characters or permanently undefined Unicode characters
请参阅完整范围。
此代码从字符串中替换所有非有效的Xml Utf8:
This code replaces all non-valid Xml Utf8 from a String:
public String stripNonValidXMLCharacters(String in) {
StringBuffer out = new StringBuffer(); // Used to hold the output.
char current; // Used to reference the current character.
if (in == null || ("".equals(in))) return ""; // vacancy test.
for (int i = 0; i < in.length(); i++) {
current = in.charAt(i);
if ((current == 0x9) ||
(current == 0xA) ||
(current == 0xD) ||
((current >= 0x20) && (current <= 0xD7FF)) ||
((current >= 0xE000) && (current <= 0xFFFD)) ||
((current >= 0x10000) && (current <= 0x10FFFF)))
out.append(current);
}
return out.toString();
}
但是,我仍然有UTF-8兼容性问题:
But with that I had the still UTF-8 compatility issue:
org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence
阅读 XML - 从servlet将XML作为UTF-8返回我刚刚尝试如果我设置这样的内容类型会发生什么:
After reading XML - returning XML as UTF-8 from a servlet I just tried out what happens if I set the Contenttype like this:
response.setContentType("text/xml;charset=utf-8");
它工作....
这篇关于如何将字符从Oracle编码到XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!