In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要介绍"dom4j的XMLWrtier输出问题实例分析"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"dom4j的XMLWrtier输出问题实例分析"文章能帮助大家解决问题。
首先先说一下现象吧,如果XML里面包含了一些特殊字符,比如 这个是line feed,也就是换行符。在经过它输出后总是会变成\n, 不好干预它的转化。下面来看具体的例子。
这个是原始的xml文件:
Xml代码
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代码
Package>
下面是transfomer的输出:
Xml代码
Package>
大家看到效果了把,dom4j变成了 \n, 但是transfomer还是原来的格式。
关于"dom4j的XMLWrtier输出问题实例分析"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.