I have a big XML file and several POJO clasess needed to read this XML. When i try read test file with one POJO i use this:
JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Test ts = (Test)jaxbUnmarshaller.unmarshal(file);
System.out.println(ts.getName());
But when i have 30 POJOs what i gonna do? Create this 4 lines 30 times? Give me advice.
UPDATE
How i understand from this example http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html for using several POGOs i gonna use
JAXBContext.newInstance("message:customer:product:order");
And in this examle autor have 3 clesses but only in two of it he whire @XmlRootElement
annotation. Why?
You could create JAXBContext with all 30 POJOs you have. Also you could store their names in jaxb.index index file in your package, and create JAXBContext.newInstance("your.package")
Here is some details about jaxb.index from javadoc
Alternatively than being listed in the context path, programmer annotated JAXB mapped classes can be listed in a jaxb.index resource file, format described below. Note that a java package can contain both schema-derived classes and user annotated JAXB classes. Additionally, the java package may contain JAXB package annotations that must be processed. (see JLS 3rd Edition, Section 7.4.1. "Package Annotations").
Your classes should be annotated with @XmlRootElement or @XmlType annotations.
Also you could find all classes annotated as @XmlRootElement using scannotation framework, and create JAXBContext with all JAXB POJOs you have.
Please if you have questions, comment and I will update an answer. Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With