JAXB - 使用递归依赖进行编组

时间:2013-01-13 18:55:34

标签: xsd jaxb2 maven-jaxb2-plugin

是否有人试图通过重新审判来整理JAXB对象?我有以下课程:

public class A {

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

}

我想把它编组到:

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

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

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

<强> A

您可以在@XmlElementRef字段上使用aList注释,以便它使用的元素来自@XmlRootElement类的A注释。

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class A {

    private Long id;

    private String name;

    @XmlElementRef
    private List<A> aList;

}

<强>演示

下面是一些示例代码,用于证明一切正常。

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(A.class);

        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);
    }

}

<强> input.xml中/输出

以下是运行演示代码的输入和输出。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
    <id>1</id>
    <name>A</name>
    <a>
        <id>2</id>
        <name>B</name>
        <a>
            <id>3</id>
            <name>C</name>
        </a>
    </a>
</a>

XML架构

以下是与此模型对应的XML架构。您可以从中生成模型,也可以从Java模型开始。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="a" type="a"/>

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

</xs:schema>

答案 1 :(得分:1)

以下是从XML架构开始支持此用例的方法。

XML SCHEMA(schema.xsd)

以下XML架构基于您对上一个答案所做的评论(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方法和注释以节省空间。

A

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;

}

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;

<强>的ObjectFactory

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();
    }

}

DEMO CODE

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);
    }

}

INPUT(input.xml)/ OUTPUT

<?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>