OracleWebRowSet writeXml 方法无法转义特殊字符,例如&符号 &
OracleWebRowSet writeXml method fails to Escape Special characters like Ampersand &
OracleWebRowSet
有一个 writeXml(FileWriter)
方法将结果集转换为 XML 文件。
使用时,无法转义特殊字符,如&符号,因此生成的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 个文件时效率高
解决方案?有人吗?
我找到了解决此问题的解决方法...但我不确定它是否正确...
开始了...
更新:
扩展 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();
希望这对遇到此问题的任何人有所帮助...如果需要完善此代码,post你们的建议
OracleWebRowSet
有一个 writeXml(FileWriter)
方法将结果集转换为 XML 文件。
使用时,无法转义特殊字符,如&符号,因此生成的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 个文件时效率高
解决方案?有人吗?
我找到了解决此问题的解决方法...但我不确定它是否正确...
开始了...
更新:
扩展 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();
希望这对遇到此问题的任何人有所帮助...如果需要完善此代码,post你们的建议