Reading XML files in Java

I have a large XML file and several POJO classes needed to read this XML. When I try to read a 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 POJO, what will I do? Create this 4 lines 30 times? Give me some advice.

UPDATE

As I understand from this example http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html to use multiple POGO I'm going to use

 JAXBContext.newInstance("message:customer:product:order"); 

And in this example, the author has 3 clesses, but only in two of them does he refer to the @XmlRootElement annotation. Why?

+4
source share
3 answers

You can create a JAXBContext with all 30 POJOs that you have. You can also save their names in the jaxb.index index file in your package and create JAXBContext.newInstance ("your.package")

Here are some details about jaxb.index from javadoc

Alternatively, than indicated in the context path, programmable annotated JAXB mapped classes can be listed in the jaxb.index resource file, described below. Note that a java package can contain both classes derived from the schema and custom JAXB classes. In addition, the java package may contain JAXB package annotations that need to be processed. (see JLS 3rd Edition, section 7.4.1. "Packet Annotations").

Your classes should be annotated using @XmlRootElement or @XmlType annotations.

You can also find all classes annotated as @XmlRootElement using the scannotation framework and create a JAXBContext with all the JJXB POJOs you have.

Please, if you have questions, comments, and I will update the answer. Hope this helps.

+6
source

Ideally, you should create a JAXBContext only once and cache it for reuse. Now

But when I have 30 POJO, what will I do? Create this 4 lines 30 times?

If all your 30 POJOs are in the same package (for example, com.abc), create a context as JAXBContext.newInstance("com.abc")

In this example, the author has 3 clesses, but only in two of them does he refer to the @XmlRootElement annotation. Why?

Only POJOs that match global element declarations in the XSD schema have the @XmlRootElement annotation. This is because declaring a global element is a potential root element in an instance document.

It would be better if you could publish a sample of your XML schema and XML instance document so that we can provide a more specific answer.

+1
source

The following is help.

EXPLANATIONS

JAXB class classes do not require annotations. There is no annotation indicating that the class should be automatically processed when creating the JAXBContext .

CREATING A JAXBContext

There are two main ways to create a JAXBContext

1 - In the classes

You are passing an array of domain classes. Then, mappings are created for these classes. Mappings are also created for references (see WHAT CLASSES A BROUGHT IN below).

2 - Context paths

In my article you pointed out ( http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html ), the context path is used. The context path consists of colon-delimited package names. Each package must contain either a jaxb.index file or an ObjectFactory . The jaxb.index file is a separate list of class names that you want to create JAXBContext . Just as when creating a JAXBContext in an array of classes, reference classes are also processed.

WHAT CLASSES TO VIEW

The following are some key concepts related to which secondary classes are processed when creating a JAXBContext .

1 - Related classes

If a JAXBContext is created in the Foo class, then Bar will also be processed because it is referenced by Foo .

 @XmlAccessorType(XmlAccessType.FIELD) public class Foo { private List<Bar> bar; } 

2 - Superclasses

If a class is being processed, its superclass is also being processed. You can put the @XmlTransient annotation in a class to prevent it from being processed (see http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html ).

 public class Foo extends Bar { } 

3 - Subclasses

If a class is processed, then its subclasses are not processed automatically. You can put the @XmlSeeAlso annotation in a class to indicate its subclasses that you want to process.

 @XmlSeeAlso({Bar.class}) public class Foo { } 

4 - Classes related to JAXB annotations

If a class is being processed, then the classes specified in the JAXB annotations inside this class are also processed

 public class Foo { @XmlElements({ @XmlElement(name="a", type=A.class), @XmlElement(name="b", type=B.class) }) private Object bar; } 
+1
source

All Articles