如何使jdom生成的xml文件不是一行?

Rainbow686 2004-10-26 03:32:59
我用jdom从数据库的表中生成xml文件,虽然可以生成,可是生成的xml文件所有的元素都在一行上,就像这样:
<?xml version="1.0" encoding="UTF-8"?>
<a><b><c><d></d></c></b></a>

我现在想生成下面这样的xml文档怎么做?
<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>
<c>
<d>
</d>
</c>
</b>
</a>
...全文
165 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwer1 2004-10-31
  • 打赏
  • 举报
回复
虽然已经结贴,支持一下逆风兄。b10不提倡那种初始化了。文档中已经说明了是Deprecated的
Rainbow686 2004-10-27
  • 打赏
  • 举报
回复
XMLOutputter 有这样的构造函数么?
XMLOutputter outputter = new XMLOutputter(" ", newline, encoding);
*******************************************************************
下面是api文档中给出的三个构造函数的说明,没有大哥你说的那样的构造啊
Constructor Summary
XMLOutputter()
This will create an XMLOutputter with the default Format matching Format.getRawFormat().
XMLOutputter(Format format)
This will create an XMLOutputter with the specified format characteristics.
XMLOutputter(XMLOutputter that)
This will create an XMLOutputter with all the options as set in the given XMLOutputter.
Rainbow686 2004-10-27
  • 打赏
  • 举报
回复
我知道了,我用的是jdom1.0,api已经改变了,必须要先构造一个Format类的一个对象,然后再用XMLOutputter(Format format)来初始化,就可以生成我要的东西了:
Format format = Format.getPrettyFormat();
format.setEncoding("GBK");
XMLOutputter outp = new XMLOutputter(format);
outp.output(xmlDocument, new FileOutputStream("c:\\subject_data3_gbk.xml")); //输出XML文档
其中Format类的构在方法是private的必须用下面三个方法实例化
getRawFormat() (no whitespace changes),
getPrettyFormat() (whitespace beautification),
getCompactFormat() (whitespace normalization).
并且实例化的Format再输出时都不一样。
zcjl 2004-10-27
  • 打赏
  • 举报
回复
public XMLOutputter(String indent, boolean newlines, String encoding) {
userFormat.setIndent(indent);
if (newlines == false) userFormat.setIndent(null);
userFormat.setEncoding(encoding);
}

我用的是jdom b10
zcjl 2004-10-26
  • 打赏
  • 举报
回复
/**
* 这个方法使用XMLOutputter将JDom Document对象输出到文件
* @param doc 将要输出的JDom Document对象
* @param filePath 将要输出到的磁盘路径
* @param newline 是否换行
* @param encoding 编码方式
*/
public static void OutputToFile(
Document myDocument,
String filePath,
boolean newline,
String encoding) {
try {
XMLOutputter outputter = new XMLOutputter(" ", newline, encoding);
FileWriter writer = new FileWriter(filePath);
outputter.output(doc, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Rainbow686 2004-10-26
  • 打赏
  • 举报
回复
如果不用浏览器呢?
我是说用文本编辑器打开看都是那个样子应该怎么做?
w5552 2004-10-26
  • 打赏
  • 举报
回复
用浏览器看就是你想看到的样子了啊,

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧