【问题标题】:OracleWebRowSet writeXml method fails to Escape Special characters like Ampersand &OracleWebRowSet writeXml 方法无法转义特殊字符,例如 &
【发布时间】:2017-05-24 13:42:34
【问题描述】:

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 文件时听起来效率不高一口气

解决方案?有人吗?

【问题讨论】:

    标签: xml oracle jdbc resultset ojdbc


    【解决方案1】:

    我找到了解决此问题的解决方法...但我不确定它是否正确...

    来了……

    更新:

    扩展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();
    

    希望这可以帮助任何偶然发现此问题的人...如果此代码需要完善,请发表您的建议

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      相关资源
      最近更新 更多