当前位置: 技术问答>java相关
如何在向xml文件写入节点时,能够实现分行显示,而不是都排在一行里,即如何实现换行?
来源: 互联网 发布时间:2015-09-12
本文导语: 下面是一段向xml文件写入新节点的简单程序: ..... DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc=builder.parse("links.xml"); doc.normalize(); ..... Element...
下面是一段向xml文件写入新节点的简单程序:
.....
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("links.xml");
doc.normalize();
.....
Element link=doc.createElement("link");
Element linktext=doc.createElement("text");
textseg=doc.createTextNode(text);
linktext.appendChild(textseg);
link.appendChild(linktext);
....
doc.getDocumentElement().appendChild(link);
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File("links.xml"));
transformer.transform(source, result);
------------------------
其运行结果可以将一个新节点元素link写入links.xml中,但是写入的所有东西都凑在一起,显示为一行,极其难看和不便,不知道我应该如何修改程序才能解决这个问题,使得每个节点和子节点能分行显示?
.....
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("links.xml");
doc.normalize();
.....
Element link=doc.createElement("link");
Element linktext=doc.createElement("text");
textseg=doc.createTextNode(text);
linktext.appendChild(textseg);
link.appendChild(linktext);
....
doc.getDocumentElement().appendChild(link);
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File("links.xml"));
transformer.transform(source, result);
------------------------
其运行结果可以将一个新节点元素link写入links.xml中,但是写入的所有东西都凑在一起,显示为一行,极其难看和不便,不知道我应该如何修改程序才能解决这个问题,使得每个节点和子节点能分行显示?
|
可使用OutputKeys对输出进行格式化
DOMSource doms = new DOMSource( document );
File file = new File( name );
StreamResult result = new StreamResult( file );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
Properties properties = transformer.getOutputProperties();
properties.setProperty( OutputKeys.ENCODING, "GB2312" );
properties.setProperty( OutputKeys.METHOD, "xml" );
properties.setProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperties( properties );
transformer.transform( doms, result );
DOMSource doms = new DOMSource( document );
File file = new File( name );
StreamResult result = new StreamResult( file );
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
Properties properties = transformer.getOutputProperties();
properties.setProperty( OutputKeys.ENCODING, "GB2312" );
properties.setProperty( OutputKeys.METHOD, "xml" );
properties.setProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperties( properties );
transformer.transform( doms, result );