JAXB - 具有递归依赖的编组 [英] JAXB - marshalling with recursive dependency

查看:26
本文介绍了JAXB - 具有递归依赖的编组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人尝试使用递归引用来编组 JAXB 对象?我有以下课程:

is anybody try to marshal JAXB object with recurisive referency? I have following class:

public class A {

   private Long id;
   private String name;
   private List<A> aList;

}

我想将其编组到:

<a>
  <a>
    <a>...</a>
  ...
  </a>
...
</a>

我正在使用 maven 插件从 XSD 自动生成 JAXB 类.有什么建议吗?

I'm using maven plugin to auto generate JAXB class from XSD. Any suggestions?

推荐答案

以下是如何从 XML 架构开始支持此用例.

Below is how you can support this use case starting from your XML schema.

XML SCHEMA (schema.xsd)

以下 XML 架构基于您对上一个答案 (http://stackoverflow.com/a/14317461/383861) 所做的评论.

The following XML schema is based on the comment you made to a previous answer (http://stackoverflow.com/a/14317461/383861).

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org" 
    xmlns:tns="http://www.example.org"
    elementFormDefault="qualified">

    <xs:element name="b">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="tns:a" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="a">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="id" type="xs:long" />
                <xs:sequence>
                    <xs:element ref="tns:a" minOccurs="0" maxOccurs="10" />
                </xs:sequence>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

生成的模型

以下代码是使用 xjc 工具生成的.已删除 get/set 方法和注释以节省空间.

The following code was generated using the xjc tool. The get/set methods and comments have been removed to save space.

一个

package org.example;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id",
    "a"
})
@XmlRootElement(name = "a")
public class A {

    protected long id;
    protected List<A> a;

}

B

package org.example;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "a"
})
@XmlRootElement(name = "b")
public class B {

    @XmlElement(required = true)
    protected List<A> a;

}

包裹信息

@javax.xml.bind.annotation.XmlSchema(
        namespace = "http://www.example.org", 
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.example;

对象工厂

package org.example;

import javax.xml.bind.annotation.XmlRegistry;

@XmlRegistry
public class ObjectFactory {

    public ObjectFactory() {
    }

    public B createB() {
        return new B();
    }

    public A createA() {
        return new A();
    }

}

演示代码

import java.io.File;
import javax.xml.bind.*;
import org.example.A;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("org.example");

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14306965/input.xml");
        A a = (A) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(a, System.out);
    }

}

输入(输入.xml)/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a xmlns="http://www.example.org">
    <id>1</id>
    <a>
        <id>2</id>
        <a>
            <id>3</id>
        </a>
    </a>
</a>

这篇关于JAXB - 具有递归依赖的编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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