1

我正在使用 EclipseLink MOXy 并且有一个具有相同数据类型的子元素的数据结构。现在我不想序列化具有无限深度的数据结构,而只是第一级。

以下是数据结构的一些示例代码:

package test;

import java.util.Collection;
import java.util.Vector;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement
public class MyClass {
    private int id;
    private String details;
    private Collection<MyClass> children = new Vector<MyClass>();

    public MyClass() {
    }

    public MyClass(int id, String details) {
        this.id = id;
        this.details = details;
    }

    @XmlElementWrapper
    @XmlElementRef
    public Collection<MyClass> getChildren() {
        return children;
    }

    public void addChild(MyClass child) {
        children.add(child);
    }

    public String getDetails() {
        return details;
    }

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setChildren(Collection<MyClass> children) {
        this.children = children;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public void setId(int id) {
        this.id = id;
    }
}

还有我的测试程序:

package test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Test {
    public static void main(String[] args) throws Exception {
        MyClass l1 = new MyClass(1, "Level 1");
        MyClass l2 = new MyClass(2, "Level 2");
        l1.addChild(l2);
        MyClass l3 = new MyClass(3, "Level 3");
        l2.addChild(l3);

        JAXBContext jc = JAXBContext.newInstance(MyClass.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(l1, System.out);
    }
}

生成以下 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myClass id="1">
    <children>
        <myClass id="2">
            <children>
                <myClass id="3">
                    <children/>
                    <details>Level 3</details>
                </myClass>
            </children>
            <details>Level 2</details>
        </myClass>
    </children>
    <details>Level 1</details>
</myClass>

但是,我也希望 xml 看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myClass id="1">
    <children>
        <myClass id="2">
            <details>Level 2</details>
        </myClass>
    </children>
    <details>Level 1</details>
</myClass>

谢谢。

4

1 回答 1

3

为了完成这个用例,我们将利用 JAXB 中的两个概念: XmlAdapterMarshaller.Listener

我的类适配器

我们将利用默认的 JAXB 行为,即不将元素编组为空值。为此,我们将实现一个XmlAdapter在达到指定级别后返回 null 的方法。为了计算级别,我们将创建一个Marshaller.Listener.

package forum11769758;

import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MyClassAdapter extends XmlAdapter<MyClass, MyClass>{

    private int levels;
    private MyMarshallerListener marshallerListener;

    public MyClassAdapter() {
    }

    public MyClassAdapter(int levels) {
        this.levels = levels;
    }

    public Marshaller.Listener getMarshallerListener() {
        if(null == marshallerListener) {
            marshallerListener = new MyMarshallerListener();
        }
        return marshallerListener;
    }

    @Override
    public MyClass marshal(MyClass myClass) throws Exception {
        if(null == marshallerListener || marshallerListener.getLevel() < levels) {
            return myClass;
        }
        return null;
    }

    @Override
    public MyClass unmarshal(MyClass myClass) throws Exception {
        return myClass;
    }

    static class MyMarshallerListener extends Marshaller.Listener {

        private int level = 0;

        public int getLevel() {
            return level;
        }

        @Override
        public void afterMarshal(Object object) {
            if(object instanceof MyClass) {
                level--;
            }
        }

        @Override
        public void beforeMarshal(Object object) {
            if(object instanceof MyClass) {
                level++;
            }
        }

    }

}

我的课

@XmlJavaTypeAdapter注释用于指定XmlAdapter应使用的an。

package forum11769758;

import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement
public class MyClass {

    private int id;
    private String details;
    private Collection<MyClass> children = new Vector<MyClass>();

    public MyClass() {
    }

    public MyClass(int id, String details) {
        this.id = id;
        this.details = details;
    }

    @XmlElementWrapper
    @XmlElementRef
    @XmlJavaTypeAdapter(MyClassAdapter.class)
    public Collection<MyClass> getChildren() {
        return children;
    }

    public void addChild(MyClass child) {
        children.add(child);
    }

    public String getDetails() {
        return details;
    }

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setChildren(Collection<MyClass> children) {
        this.children = children;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public void setId(int id) {
        this.id = id;
    }

}

测试

由于我们需要以XmlAdapter有状态的方式使用 ,我们将在 上设置它的实例Marshaller,我们还将在 上设置Marshaller.Listener我们创建的实例Marshaller

package forum11769758;

import javax.xml.bind.*;

public class Test {

    public static void main(String[] args) throws Exception {
        MyClass l1 = new MyClass(1, "Level 1");
        MyClass l2 = new MyClass(2, "Level 2");
        l1.addChild(l2);
        MyClass l3 = new MyClass(3, "Level 3");
        l2.addChild(l3);

        JAXBContext jc = JAXBContext.newInstance(MyClass.class);
        Marshaller marshaller = jc.createMarshaller();
        MyClassAdapter myClassAdapter = new MyClassAdapter(2);
        marshaller.setAdapter(myClassAdapter);
        marshaller.setListener(myClassAdapter.getMarshallerListener());
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(l1, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<myClass id="1">
   <children>
      <myClass id="2">
         <children/>
         <details>Level 2</details>
      </myClass>
   </children>
   <details>Level 1</details>
</myClass>

了解更多信息

以下文章扩展了此答案中讨论的主题:

于 2012-08-02T10:40:45.420 回答