dom4j生成xml时添加standalone属性

小小嗨皮 2010-10-13 04:28:04
我现在用dom4j生成xml文件时默认是<?xml version="1.0" encoding="utf-8"?>,想要变成<?xml version="1.0" encoding="UTF-8" standalone="no" ?>,不知道如何修改。
...全文
378 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
stl0 2010-10-13
引用个高人解答
------------------------------------------------------------
dom4j's XMLWriter doesn't support this currently, you could
override the writeDeclaration method though:


public class StandaloneWriter extends XMLWriter {

protected void writeDeclaration() throws IOException {
OutputFormat format = getOutputFormat();

String encoding = format.getEncoding();

if (!format.isSuppressDeclaration()) {
if (encoding.equals("UTF8")) {
writer.write("<?xml version=\"1.0\"");

if (!format.isOmitEncoding()) {
writer.write(" encoding=\"UTF-8\"");
}

writer.write(" standalone=\"true\"");
writer.write("?>");
} else {
writer.write("<?xml version=\"1.0\"");

if (!format.isOmitEncoding()) {
writer.write(" encoding=\"" + encoding + "\"");
}

writer.write(" standalone=\"true\"");
writer.write("?>");
}

if (format.isNewLineAfterDeclaration()) {
println();
}
}
}


Regards
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-13 04:28
社区公告
暂无公告