需要帮助格式化JAXB输出 [英] Need help in formatting JAXB output

查看:151
本文介绍了需要帮助格式化JAXB输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些对象让我们说两个,A和B.这些对象来自同一个类。我需要使用JAXB封送这些对象,输出XML应采用以下形式:

I have some objects let's say two, A and B. These objects from the same class. I need to marshal these objects using JAXB and the output XML should be in this form:

<Root>
    <A>
        <ID> an id </ID>
    </A>
    <B>
        <ID> an id </ID>
    </B>
</Root>

<!-- Then all A and B attributes must be listed !-->
<A>
    <ID> an id </ID>
    <attribute1> value </attribute1>
    <attribute2> value </attribute2>
</A>
<B>
    <ID> an id </ID>
    <attribute1> value </attribute1>
    <attribute2> value </attribute2>
</B>

如何在JAXB中生成此格式?任何帮助都表示赞赏。

How to generate this format in JAXB? Any help is appreciated.

更新:
更具体地说,假设我们有这样的Human类:

Update: To be more specific, Assume we have Human class like this:

@XmlRootElement
public class Human {
    private String name;
    private int age;
    private Integer nationalID;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Integer getNationalID() {
        return nationalID;
    }

    public void setNationalID(Integer nationalID) {
        this.nationalID = nationalID;
    }
}

我们的主要课程是:

public class Main3 {

    public static void main(String[] args) throws JAXBException {
        Human human1 = new Human();
        human1.setName("John");
        human1.setAge(24);
        human1.setNationalID(Integer.valueOf(123456789));

        JAXBContext context = JAXBContext.newInstance(Human.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        StringWriter stringWriter = new StringWriter();

        m.marshal(human1, stringWriter);

        System.out.println(stringWriter.toString());
    }

}

然后输出将是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
    <age>24</age>
    <name>John</name>
    <nationalID>123456789</nationalID>
</human>

现在我需要输出如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
    <nationalID>123456789</nationalID>
</human>
<human>
    <nationalID>123456789</nationalID>
    <age>24</age>
    <name>John</name>
</human>

这将帮助我绘制一个没有属性的XML对象树(仅限ID)然后但树下的所有定义。这可以使用JAXB或任何其他实现吗?

And this will help me draw a tree of XML objects without the attributes (only by ID) and then but all the definitions below the tree. Is this possible using JAXB or any other implementation?

推荐答案

试试这个:

import java.io.StringWriter;
import javax.xml.bind.Marshaller;

...

Object requestObject = ...  // This is the object that needs to be printed with indentation
Marshaller marshaller = ...
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter stringWriter = new StringWriter();
marshaller.marshal(requestObject, stringWriter);

System.out.println(stringWriter.toString());

这篇关于需要帮助格式化JAXB输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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