有大虾也能给我一份吗?[email protected]
解决方案 »
- 有本人通过全国计算机软件专业技术资格与水平考试认证的高级认证的么?国家级的一年只考两次那种。
- 有没有JAVA EE中文API帮助文档?
- activeMQ如何配置可以实现多个jms协同工作
- 为什么我的工程不能部署啊?
- 各位高手帮忙! 如何把线程类中的数据传递到调用这个线程类的类中
- 在Hiberate事务中,出现抛异常或者其它原因导致没有执行commit或rollback方法时的安全问题
- 求一个for循环
- 发现学EJB的人太少,特发一贴征集EJB学习同志共勉,加我QQ:34495714!
- jdbc驱动问题
- struts中,action对象如何返回参数给JSP显示?
- 高分跪求Struts、CVS、JUnit、HttpUnit、StrutsTestCase相关新闻,一条20分~~~~~~~
- JB9的Struts替换为1.1,出现奇怪的红色信息,是什么问题?
* 这个方法使用XMLOutputter将JDom对象输出到文件
* @param myDocument 将要输出的JDom对象
* @param filePath 将要输出到的磁盘路径
* @param encoding 编码方式
*/
public static void OutputToFile(Document myDocument, String filePath,
String encoding) {
//setup this like outputDocument
try {
XMLOutputter outputter = new XMLOutputter("", true, encoding);
//这里可以设置换行和空格,具体可以查看javadoc。 //output to a file
FileWriter writer = new FileWriter(filePath);
outputter.output(myDocument, writer);
writer.close();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
}
内容 http://expert.csdn.net/Expert/topic/2450/2450357.xml?temp=.4288446谢谢你的回复^_^//这里可以设置换行和空格,具体可以查看javadoc。
但是我还是不知道具体该怎么写, 你能告诉我吗? 谢谢!! 写了个例子演示一下:1. XMLOutputter outputter = new XMLOutputter("", true, "GB2312");输出结果如下:<root>
<sub1>subtext1</sub1>
<sub2>subtext2</sub2>
</root>2.XMLOutputter outputter = new XMLOutputter(" ", true, "GB2312");
<root>
<sub1>subtext1</sub1>
<sub2>subtext2</sub2>
</root>3.XMLOutputter outputter = new XMLOutputter(" ", false, "GB2312");<root><sub1>subtext1</sub1><sub2>subtext2</sub2></root>
下面是jdom的api帮助,看看就明白了:Constructor Detail XMLOutputter public XMLOutputter()This will create an XMLOutputter with no additional whitespace (indent or newlines) added; the whitespace from the element text content is fully preserved.XMLOutputter public XMLOutputter(java.lang.String indent)This will create an XMLOutputter with the given indent added but no new lines added; all whitespace from the element text content is included as well.Parameters:indent - the indent string, usually some number of spacesXMLOutputter public XMLOutputter(java.lang.String indent,
boolean newlines)This will create an XMLOutputter with the given indent that prints newlines only if newlines is true; all whitespace from the element text content is included as well.Parameters:indent - the indent String, usually some number of spacesnewlines - true indicates new lines should be printed, else new lines are ignored (compacted).XMLOutputter public XMLOutputter(java.lang.String indent,
boolean newlines,
java.lang.String encoding)This will create an XMLOutputter with the given indent and new lines printing only if newlines is true, and encoding format encoding.Parameters:indent - the indent String, usually some number of spacesnewlines - true indicates new lines should be printed, else new lines are ignored (compacted).encoding - set encoding format. Use XML-style names like "UTF-8" or "ISO-8859-1" or "US-ASCII"
但是我还是有个问题,因为我做的补写xml文件
用你给我的方法,
<root>
<sub1>subtext1</sub1>
<sub2>subtext2</sub2>
</root>
补写了3,4的话会形成
<root> <sub1>subtext1</sub1> <sub2>subtext2</sub2></root>
<root>
<sub3>subtext3</sub3>
<sub4>subtext4</sub4>
</root>
再写一次又会多一行空行(我要写n多次,可能无限长),我需要解决多余的空行,帮帮我!谢谢!!
这个是jdom自身的问题,我也遇到过,目前没有比较好的办法。我现在是把换行设置为false,在需要换行的最终输出(也就是说输出之后不再更改)的时候,我再把换行设置为true。多余的空行并不会影响数据的读写。我在定义好元素之后,就是用写xsl进行处理了,所以是否空行太多对我影响不大。你试试其它办法吧。