带有 XStream 的自闭合标签?

发布于 2024-08-11 21:12:36 字数 139 浏览 7 评论 0原文

有没有办法让XStream生成自关闭元素?

例如

<foo/>

代替

<foo></foo>

Is there a way to get XStream to generate self closing elements?

E.g.

<foo/>

instead of

<foo></foo>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十级心震 2024-08-18 21:12:36

似乎没有明确的方法可以做到这一点,因为它对下游 XML 处理器来说并不重要。

您可以查看一下 PrettyPrintWriter 类的源代码,它可能正在使用它。

There doesn't seem to be an explicit way to do this, since it's not supposed to matter to downstream XML processors.

You might take a look at the source for the PrettyPrintWriter class, which is probably what it's using.

留一抹残留的笑 2024-08-18 21:12:36

我使用一些解决方法解决了这个问题。
特别是,XStream 仅针对非原始对象打印自闭合标签;所以我用一个名为“Element”的通用类替换了所有 String 对象,该类只有一个属性“value”。

public class Element {
    private String value;
    public Element() {
    }
    public Element(String value) {
        this.value = value;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

通过这种方式,Element 的空值将作为自闭合标签在 XML 中打印(如我所愿)。

在我编写了一个自定义转换器以避免在 XML 中打印非空对象的标签之后。

public class CustomElementConverter implements Converter
{
    public boolean canConvert(Class type)
    {
        return type.equals(Element.class);
    }
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        String element = ((Element)source).getValue();
        if (element != null) { 
            writer.setValue(element);
        }
    }
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        return null;
    }
}

它成功了!
我希望它对其他人有用。

多姆

I solved the problem using a little Workaround.
In particular XStream prints selfclosing tags only for non-primitive object; so I replaced all String objects with a Generic class called "Element" with only one attribute, "value".

public class Element {
    private String value;
    public Element() {
    }
    public Element(String value) {
        this.value = value;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

In this way null values of Element will be printed in XML as selfclosing tags (as I wish)

After I wrote a custom converter to avoid printing in XML the tag for not empty objects.

public class CustomElementConverter implements Converter
{
    public boolean canConvert(Class type)
    {
        return type.equals(Element.class);
    }
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        String element = ((Element)source).getValue();
        if (element != null) { 
            writer.setValue(element);
        }
    }
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        return null;
    }
}

And it worked!
I hope it will be useful to someone else.

Dom

娇纵 2024-08-18 21:12:36

您可以使用转换器,然后使用以下方法设置值: writer.setValue("");

像这样:

public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    Ack ack = (Ack)source;

    writeBasicAck(writer, ack);

    writer.setValue("");
}

protected void writeBasicAck(HierarchicalStreamWriter writer, Ack ack) {
    writer.addAttribute("pos", String.valueOf(ack.getPos()));
    writer.addAttribute("ticket", String.valueOf(ack.getTicket()));
    writer.addAttribute("estado", String.valueOf(ack.getEstado()));
    writer.addAttribute("suc", String.valueOf(ack.getSucursal()));
}

这样写:

<ackticket pos="2" ticket="123" estado="0" suc="1235"></ackticket>

而不是:

<ackticket pos="2" ticket="123" estado="0" suc="1235"/>

you can use a converter and then set value with: writer.setValue("");

like this:

public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    Ack ack = (Ack)source;

    writeBasicAck(writer, ack);

    writer.setValue("");
}

protected void writeBasicAck(HierarchicalStreamWriter writer, Ack ack) {
    writer.addAttribute("pos", String.valueOf(ack.getPos()));
    writer.addAttribute("ticket", String.valueOf(ack.getTicket()));
    writer.addAttribute("estado", String.valueOf(ack.getEstado()));
    writer.addAttribute("suc", String.valueOf(ack.getSucursal()));
}

this write:

<ackticket pos="2" ticket="123" estado="0" suc="1235"></ackticket>

instead of:

<ackticket pos="2" ticket="123" estado="0" suc="1235"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文