用jdom创建xml,如何设置standalone =" no"属性 [英] create xml with jdom, how to set standalone="no" attribute

查看:183
本文介绍了用jdom创建xml,如何设置standalone =" no"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个jdom文档(Document doc = new Document();)时,默认情况下我只在xml标题中看到版本和编码:

When I create a jdom document (Document doc = new Document();), by default I only see version and encoding in the xml header:

<?xml version="1.0" encoding="utf-8" ?>

如何添加独立属性以获取:

How can I add the standalone attribute to get:

<?xml version="1.0" encoding="utf-8" standalone="no" ?>


推荐答案

Header通常在文档之前被XMLParser剥离进入JDOM。我很确定你的意思是你正在查看来自JDOM的输出,它会重新添加XML声明。

The Header is normally stripped by the XMLParser before the document gets to JDOM. I'm pretty sure you mean you're looking at the output from JDOM, which adds the XML declaration back in.

你可以调整如何通过创建自定义XMLOutput处理器 ...使用此自定义类,覆盖printDeclaration方法并将其更改为执行您需要的操作....

You can adjust how the XML Declaration is processed by creating a custom XMLOutput processor... with this custom class, override the printDeclaration method and change it to do what you need....

public static final XMLOutputProcessor XMLOUTPUT = new AbstractXMLOutputProcessor() {
    @Override
    protected void printDeclaration(final Writer out, final FormatStack fstack) throws IOException {
        write(out, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?> ");
        write(out, fstack.getLineSeparator());
    }
};

然后,当您想要使用它时,将其传递给XMLOutputter:

Then, when you want to use this, you pass it to your XMLOutputter as:

XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat(), XMLOUTPUT);
xout.output(doc, System.out);

很明显,这样做的机制相当繁琐。我会查看有哪些替代方案,也许可以在将来的版本中解决这个问题。

It is apparent that the mechanism for doing this is rather cumbersome. I will look in to what alternatives there are, and perhaps fix this in a future version.

这篇关于用jdom创建xml,如何设置standalone =&quot; no&quot;属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆