OracleWebRowSet
有writeXml(FileWriter)
方法将结果集转换为XML文件。
使用时,它无法转义像Ampersand这样的特殊字符,因此生成的XML文件无法符合XML 1.0标准
虽然来自rt.jar的默认WebRowSet
工作正常,但我有特殊理由使用OracleWebRowSet
我尝试了StringEscapeUtils.EscapeXML10.translate()
但它不像规则那样工作,而是作为一个直接的字符串翻译器。
例如:
OracleWebRowSet owrs = new OracleWebRowSet();
FileWriter fWriter = = new FileWriter("file1.xml");
owrs.setEscapeProcessing(true);
//this is where resultset is converted to XML but not escaped properly
owrs.writeXml(fWriter);
fWriter.flush();
我正处于绑定状态...我可能会尝试将生成的XML作为文本文件读取并转义内容并将其写回文件...但在处理700 xml文件时这听起来效率不高一气呵成
的解决方案?任何人吗?
答案 0 :(得分:0)
我找到了一个解决方法来解决这个问题...但我不确定它是否正确...
这里...... ...
<强>更新:强>
扩展java.io.FileWriter
并覆盖write(String)
方法
package customizations.java.io;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringEscapeUtils;
public class XMLFileWriter extends java.io.FileWriter {
private Pattern html_prefix_pattern;
private Pattern html_suffix_pattern;
private Pattern common_tags_pattern1;
private Pattern common_tags_pattern2;
private Pattern common_tags_pattern3;
public XMLFileWriter(String fileName) throws IOException {
super(fileName);
html_prefix_pattern = Pattern.compile("(?i)(.*)<[\\s]*html(.*)>(.*)", Pattern.DOTALL);
html_suffix_pattern = Pattern.compile("(?i)(.*)<[\\s]*/html[\\s]*>(.*)", Pattern.DOTALL);
common_tags_pattern1 = Pattern.compile("(.+)<[^/?](\"[^\"]*\"|'[^']*'|[^'\">])*[^?]>(.+)", Pattern.DOTALL);
common_tags_pattern2 = Pattern.compile("^<[^/?](\"[^\"]*\"|'[^']*'|[^'\">])*[^?]>(.+)", Pattern.DOTALL);
common_tags_pattern3 = Pattern.compile("(.+)<[^/?](\"[^\"]*\"|'[^']*'|[^'\">])*[^?]>$", Pattern.DOTALL);
}
@Override
public void write(String str) throws IOException {
Matcher html_prefixMatcher = html_prefix_pattern.matcher(str);
Matcher html_suffixMatcher = html_suffix_pattern.matcher(str);
boolean cdata_proc = false;
//if(str.matches("(?i)(.*)[\\s]*<[\\s]*/html[\\s]*>[\\s]*(.*)")) {
//for CLOB data in oracle table, html tags in content will violate the XMLWebRowSet Schema Structure. So enclose them in CDATA
if(html_prefixMatcher.find()) {
str = "<![CDATA["+str;
cdata_proc = true;
}
if(html_suffixMatcher.find()) {
str = str+"]]>";
cdata_proc = true;
}
if(!cdata_proc) {
Matcher common_tagsMatcher1 = common_tags_pattern1.matcher(str);
Matcher common_tagsMatcher2 = common_tags_pattern2.matcher(str);
Matcher common_tagsMatcher3 = common_tags_pattern3.matcher(str);
if(str.matches("(.*)&(.*)") || common_tagsMatcher1.find() || common_tagsMatcher2.find() || common_tagsMatcher3.find()) {
str = StringEscapeUtils.ESCAPE_XML10.translate(str);
}
}
super.write(str);
}
}
所以只要OracleWebRowset
使用write()
方法,我们的代码就会启动并检查文本是否需要转义...我们需要限制StringEscapeUtils
或者其他, XML标签也将被转义,导致一个尴尬的xml文件
修改后的代码如下所示:
OracleWebRowSet owrs = new OracleWebRowSet();
XMLFileWriter fWriter = = new XMLFileWriter("file1.xml");
owrs.setEscapeProcessing(true);
//this is where resultset is converted to XML but not escaped properly
owrs.writeXml(fWriter);
fWriter.flush();
希望这可以帮助任何偶然发现此问题的人...如果需要完善此代码,请发布您的建议人员