Serialize a javabeans graph in xml having a separate XML file for each java instance

Could you suggest a structure or tool that could serialize a javabeans graph to xml, having a separate XML file for each java instance? All java xml tools I managed to find serialzie for a single file, but I need them to be separate, for example:

Model:

 class A {
    B b;

 }

 class B {

 }

 A a = new A(); 
 a.b  = new B();

serialize:

a.xml:

 <a>
  <property name="b>somehow ref to b</property>
 </a>

b.xml

<b>
</b>

Regards, ebu.

+3
source share
1 answer

You can use JAXB and XmlAdapter to do something like the following:

AND

import java.util.ArrayList;
import java.util.List;

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

@XmlRootElement
@XmlJavaTypeAdapter(MyAdapter.class)
public class A {

    private B b;
    private List<C> c;

    public A() {
        c = new ArrayList<C>();
    }

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }

    public List<C> getC() {
        return c;
    }

    public void setC(List<C> c) {
        this.c = c;
    }

}

AT

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

@XmlRootElement
@XmlJavaTypeAdapter(MyAdapter.class)
public class B {

}

WITH

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

@XmlRootElement
@XmlJavaTypeAdapter(MyAdapter.class)
public class C {

}

Myadapter

import java.io.File;

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

public class MyAdapter extends XmlAdapter<String, Object> {

    private static int counter = 1; 

    private static JAXBContext jaxbContext;
    static {
        try {
            jaxbContext = JAXBContext.newInstance(A.class, B.class, C.class);
        } catch(JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object unmarshal(String v) throws Exception {
        File xml = new File(v);
        return jaxbContext.createUnmarshaller().unmarshal(xml);
    }

    @Override
    public String marshal(Object v) throws Exception {
        String filename = counter++ + ".xml";
        File xml = new File(filename);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(v, xml);
        return filename;
    }

}

Demo

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(A.class, B.class, C.class);

        A a = new A();
        a.setB(new B());
        a.getC().add(new C());
        a.getC().add(new C());

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

}
+1
source

Source: https://habr.com/ru/post/1787650/


All Articles