日期:2014-05-17  浏览次数:20766 次

关于dom4j处理转义字符
我的xml里有转义字符,如"、>等。
我用dom4j对xml进行处理,然后重新写回。
public static void execute(/*一些参数*/) throws Exception {
    filepath = "D:\\form.xml";
    SAXReader reader = new SAXReader();
    Document doc = reader.read(new File(filepath));
        
    //......一些处理
        
    XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filepath));
    xmlWriter.write(doc);
    xmlWriter.close();
}

得到的xml文件里面的"变成了双引号,>没变。
我想让他都不变,"还是",不要变成双引号。
在线等,谢谢大家。

------解决方案--------------------
saxRead.setEncoding("UTF-8");指定你使用的格式,GB2312
------解决方案--------------------
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"), format);
writer.write(document);
writer.close();

------解决方案--------------------
试试cdata,传送门:http://www.w3school.com.cn/xml/xml_cdata.asp


<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
  {
  return 1;
  }
else
  {
  return 0;
  }
}
]]>
</script>

------解决方案--------------------
引用:
引用:Java code?12345OutputFormat format = OutputFormat.createPrettyPrint();            format.setEncoding("utf-8");            XMLWriter writer = new XMLWriter(new Ou……
那你就把它<![CDATA[  XXOO  ]]>起来
------解决方案--------------------
 //转码加入xml文件的另一种方法  
  OutputFormat format = OutputFormat.createPrettyPrint();  
  format.setEncoding("UTF-8");  
  XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format);  
  writer.write(document);  
  writer.close();   
 }  
原出处:http://blog.csdn.net/tianyazaiheruan/article/