开发者

serialize graph of javabeans into xml having separate xml file for each java instance

开发者 https://www.devze.com 2023-02-06 20:10 出处:网络
Could you please suggest a framework or tool which could serialize graph of javabeans into xml having separate xml file for each java instance? All the java xml tools i managed to find serialzie to si

Could you please suggest a framework or tool which could serialize graph of javabeans into xml having separate xml file for each java instance? All the java xml tools i managed to find serialzie to 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 to :

a.xml:

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

b.xml

<b>
</b>

Best regards, ebu.


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

A

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

}

B

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

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

}

C

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消