And*_*ard 8 java xml oracle encoding
在我的环境中,我使用Java将结果集序列化为XML.它基本上是这样的:
//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");
Run Code Online (Sandbox Code Playgroud)
在Firefox中,XML看起来像这样:
<row num="69004">
<column num="1">10069</column>
<column num="2">sd</column>
<column num="3">FCVolume </column>
</row>
Run Code Online (Sandbox Code Playgroud)
但是当我解析XML时,我得到了a
org.xml.sax.SAXParseException:字符引用" "是无效的XML字符.
我现在的问题是:我必须替换哪些字符,或者如何编码我的字符,它们将是有效的XML?
我在Xml规范中找到了一个有趣的列表:根据该列表,它不鼓励使用字符#26(十六进制:#x1A).
还不鼓励在以下范围中定义的字符.它们是控制字符或永久未定义的Unicode字符
查看完整的范围.
此代码从String中替换所有无效的Xml Utf8:
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();
}
Run Code Online (Sandbox Code Playgroud)
它来自无效的XML字符:当有效的UTF8不代表有效的XML时
但有了这个,我还有UTF-8的比较问题:
org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence
Run Code Online (Sandbox Code Playgroud)
在阅读XML之后 - 从servlet返回XML作为UTF-8我刚尝试了如果我将Contenttype设置为这样会发生什么:
response.setContentType("text/xml;charset=utf-8");
Run Code Online (Sandbox Code Playgroud)
它工作....