昨天发现一个问题,使用dom4j的XMLWrtier输出的问题。
首先先说一下现象吧,如果XML里面包含了一些特殊字符,比如 这个是line feed,也就是换行符。在经过它输出后总是会变成\n, 不好干预它的转化。下面来看具体的例子。
这个是原始的xml文件:
Xml代码
- xml version="1.0" encoding="UTF-8"?>
- <Package id = "test" name = "Test Test">
- < span>Package>
下面是测试代码:
Java代码
- package org.powermock.examples.dom4j;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.stream.StreamResult;
- import junit.framework.Assert;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.io.DocumentSource;
- import org.dom4j.io.XMLWriter;
- import org.junit.Test;
- public class TestXMLOutput {
- public final static String FILE_NAME = "text.xml";
- @Test
- public void testDom4j() {
- Document document = createDocument();
- try {
- Element rootElement = document.getRootElement();
- String packageName = rootElement.attributeValue("name");
- Assert.assertEquals("Test\nTest", packageName);
- FileOutputStream fos = new FileOutputStream("dom4j_"+FILE_NAME);
- XMLWriter writer = new XMLWriter(fos);
- writer.setEscapeText(false);
- writer.write(document);
- fos.close();
- writer.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Test
- public void testDom(){
- Document document = createDocument();
- try {
- FileOutputStream fos = new FileOutputStream("dom_"+FILE_NAME);
- // Use a Transformer for output
- TransformerFactory tFactory =
- TransformerFactory.newInstance();
- Transformer transformer = tFactory.newTransformer();
- transformer.setOutputProperty("indent","yes");
- transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
- transformer.setOutputProperty("encoding","UTF-8");
- DocumentSource source = new DocumentSource(document);
- StreamResult result = new StreamResult(fos);
- transformer.transform(source,result);
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private Document createDocument(){
- Document document = null;
- try {
- File file = new File(FILE_NAME);
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(new FileInputStream(file)));
- StringBuffer buffer = new StringBuffer();
- String line = "";
- while((line = reader.readLine()) != null){
- buffer.append(line);
- }
- reader.close();
- document = DocumentHelper.parseText(buffer.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- return document;
- }
- }
下面是dom4j的转换后的输出:
Xml代码
- xml version="1.0" encoding="UTF-8"?>
- <Package id="test" name="Test
- Test"> < span>Package>
下面是transfomer的输出:
Xml代码
- xml version="1.0" encoding="UTF-8"?>
- <Package id="test" name="Test Test"> < span>Package>
大家看到效果了把,dom4j变成了 \n, 但是transfomer还是原来的格式。
如果大家谁有对这块有研究的,希望给我留言解决这个问题。
【编辑推荐】