用dom4j生成xml文件。以字符串输出的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用dom4j生成xml文件。以字符串输出的问题相关的知识,希望对你有一定的参考价值。

生成xml后调用asXML是以无格式字符串输出的。我想以有格式字符串输出。应怎么办。如下面这样输出

<file>
<chapter id="1" name="a"/>
<chapter id="2 name="b/>
<chapter id="3 name="c"/>
<chapter id="4 name="d/>
</file>

楼主的问题应该在于如何创建出指定格式的format

打开dom4j的文档查看OutputFormat类
该类其实提供了2个static方法,返回一个outputFormat对象
createCompactFormat() 返回的outputForamt的格式为压缩,即空格,回车等都被忽略了
createPrettyPrint() 这个方法返回的outputFormat的格式则为良好格式

因为在html语言中,大家书写都喜欢换行和缩进,保持良好的书写习惯,当在xml语言中空格和换行都作为原始内容被处理,即空格和换行对应也是一个对象
所以 Document doc = DocumentHelper.parseText(str);
这段代码在解析的时候并没有针对空格和换行进行操作
OutputFormat针对此专门提供了2个satic方法来获得对应的格式
document的asXML方法的源码中new出的outputFormat其实是一个空的格式
以上分析仅为个人看法,毕竟我不是dom4j的开发人员,如有错误,请指正

以下为相关代码:

import java.io.StringWriter;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class Demo

public static void main(String[] args) throws Exception

String str = "<file>"+
"<chapter id=\"1\" name=\"a\"/>" +
"<chapter id=\"2\" name=\"b\"/>" +
"<chapter id=\"3\" name=\"c\"/>" +
"<chapter id=\"4\" name=\"d\"/>" +
"</file>";

// 将字符串格式转换成document对象
Document document = DocumentHelper.parseText(str);

// 注意,用这种方式来创建指定格式的format
OutputFormat format = OutputFormat.createPrettyPrint();

// 创建String输出流
StringWriter out = new StringWriter();

//包装String流
XMLWriter writer = new XMLWriter(out, format);

// 将当前的document对象写入底层流out中
writer.write(document);
writer.close();
System.out.println(out.toString());

参考技术A 已经修改过了,可以得到String对象了。

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.*;

public class DomTest
public static void main(String[] args)
String str = "<file>"+
"<chapter id=\"1\" name=\"a\"/>" +
"<chapter id=\"2\" name=\"b\"/>" +
"<chapter id=\"3\" name=\"c\"/>" +
"<chapter id=\"4\" name=\"d\"/>" +
"</file>";

try
Document doc = DocumentHelper.parseText(str);
//输出格式化器
OutputFormat format = new OutputFormat(" ", true);
//设置编码
format.setEncoding("gb2312");
//xml输出器
StringWriter out = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(out, format);
//打印doc
xmlWriter.write(doc);
xmlWriter.flush();
//关闭输出器的流,即是printWriter
String s = out.toString();
System.out.println(s);
catch (Exception e)
e.printStackTrace();



============
刚刚下载的dom4j源代码,它的asXML源代码如下:
public String asXML()
OutputFormat format = new OutputFormat();
format.setEncoding(encoding);

try
StringWriter out = new StringWriter();
XMLWriter writer = new XMLWriter(out, format);
writer.write(this);
writer.flush();

return out.toString();
catch (IOException e)
throw new RuntimeException("IOException while generating textual "
+ "representation: " + e.getMessage());


它没有接受Format参数的asXML,所以只能象上面代码那样写了。本回答被提问者采纳

以上是关于用dom4j生成xml文件。以字符串输出的问题的主要内容,如果未能解决你的问题,请参考以下文章

dom4j的介绍

关于dom4j 解析xml文件的问题

安卓客户端用dom4j生成的带有汉字的xml文件,发送到Servlet页面解析出来中文是乱码,应该怎么处理??

使用dom4j写xml文件——源码

怎么用dom4j 修改XML文件中的信息?

如何根据xml和schema文件生成java类(用程序实现)!