How to set an object marker with Java XML binding to a specific depth?

being new to Java XML binding, I ran into a problem.

Let's say I have a scenario where my domain model is built, and I want this domain to be configured on an XML structure.

Now I want to provide different unmarshall paths:

  • Marshall entire graph of objects [no problem here]
  • Marshall - object graphics to a certain depth !!! [Call]

I can't find a good way to handle this without going into complexity. You can make a copy of the domain manually later, but this does not seem to be correct. Any other solutions available?

+3
source share
1 answer

To get this behavior, you can use XmlAdapter and Marshal.Listener:

Demo

Marshal.Listener , . XmlAdapters , . , .

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

public class Demo {

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

        Root rootA = new Root();
        rootA.setName("A");

        Root rootB = new Root();
        rootB.setName("B");
        rootA.setChild(rootB);

        Root rootC = new Root();
        rootC.setName("C");
        rootB.setChild(rootC);

        Root rootD = new Root();
        rootD.setName("D");
        rootC.setChild(rootD);

        Root rootE = new Root();
        rootE.setName("E");
        rootD.setChild(rootE);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        DepthListener depthListener = new DepthListener(3);
        marshaller.setListener(depthListener);
        marshaller.setAdapter(new RootAdapter(depthListener));
        marshaller.marshal(rootA, System.out);
    }

}

DepthListener

- .

import javax.xml.bind.Marshaller;

public class DepthListener extends Marshaller.Listener {

    private int targetDepth;
    private int currentDepth = 0;

    public DepthListener(int depth) {
        this.targetDepth = depth;
    }

    @Override
    public void beforeMarshal(Object source) {
        currentDepth++;
    }

    @Override
    public void afterMarshal(Object source) {
        currentDepth--;
    }

    public boolean isMarshalDepth() {
        return currentDepth <= targetDepth; 
    }

}

RootAdapter

XmlAdapter - , , .

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

public class RootAdapter extends XmlAdapter<Root, Root> {

    private DepthListener depthListener;

    public RootAdapter() {
    }

    public RootAdapter(DepthListener depthListener) {
        this.depthListener = depthListener;
    }

    @Override
    public Root unmarshal(Root root) throws Exception {
        return root;
    }

    @Override
    public Root marshal(Root root) throws Exception {
        if(depthListener != null && !depthListener.isMarshalDepth()) {
            return null;
        }
        return root;
    }

}

Root

, XmlAdapter @XmlJavaTypeAdapter:

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlJavaTypeAdapter(RootAdapter.class)
@XmlType(propOrder={"name", "child"})
public class Root {

    private String name;
    private Root child;

    public String getName() {
        return name;
    }

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

    public Root getChild() {
        return child;
    }

    public void setChild(Root report) {
        this.child = report;
    }

}

:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <name>A</name>
    <child>
        <name>B</name>
        <child>
            <name>C</name>
        </child>
    </child>
</root>
+2

All Articles