Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse xml to java object? [closed]

Tags:

java

xml

I have a XML which is used to config some rules, it does not has complex structure, but this configuration is used anywhere in my system, so I want to parse this XML to java object and design as singleton mode, is any good way I can use it to unmarshal XML to Java object directly without write much codes?

I did some research on Google and known JAXB is a choice, my application is just some kinds of tool program which read rule and then follow do stuffs, JAXB could be used for web service more widely, it fit my projects?

If yes, the most important question is, I used xjc to generate java object source class according xsd file, after unmarshal I will directly get these configurationType object, is it necessary I convert again, (from the JaxB classes to my owned java pojo object configuration), I see most coder did this, but why? because they are some data, just from the object generated to JAXB and copy to ourself created POJO object

like image 559
C.c Avatar asked May 03 '13 17:05

C.c


People also ask

What is the best way to parse XML in Java?

Java XML Parser - DOM DOM Parser is the easiest java xml parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML. DOM Parser is good for small files but when file size increases it performs slow and consumes more memory.

How do I read an existing XML file in Java?

We must have followed the process to read an XML file in Java: Instantiate XML file: DOM parser loads the XML file into memory and consider every tag as an element. Get root node: Document class provides the getDocumentElement() method to get the root node and the element of the XML file.


Video Answer


4 Answers

JAXB is an ideal solution. But you do not necessarily need xsd and xjc for that. More often than not you don't have an xsd but you know what your xml is. Simply analyze your xml, e.g.,

<customer id="100">     <age>29</age>     <name>mkyong</name> </customer> 

Create necessary model class(es):

@XmlRootElement public class Customer {      String name;     int age;     int id;      public String getName() {         return name;     }      @XmlElement     public void setName(String name) {         this.name = name;     }      public int getAge() {         return age;     }      @XmlElement     public void setAge(int age) {         this.age = age;     }      public int getId() {         return id;     }      @XmlAttribute     public void setId(int id) {         this.id = id;     }  } 

Try to unmarshal:

JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Customer customer = (Customer) jaxbUnmarshaller.unmarshal(new File("C:\\file.xml")); 

Check results, fix bugs!

like image 79
Evgeniy Dorofeev Avatar answered Sep 17 '22 04:09

Evgeniy Dorofeev


One thing that is really important to understand considering you have an XML file as :

<customer id="100">     <Age>29</Age>     <NAME>mkyong</NAME> </customer> 

I am sorry to inform you but :

@XmlElement public void setAge(int age) {     this.age = age; } 

will not help you, as it tries to look for "age" instead of "Age" element name from the XML.

I encourage you to manually specify the element name matching the one in the XML file :

@XmlElement(name="Age") public void setAge(int age) {     this.age = age; } 

And if you have for example :

@XmlRootElement @XmlAccessorType (XmlAccessType.FIELD) public class Customer { ... 

It means it will use java beans by default, and at this time if you specify that you must not set another

@XmlElement(name="NAME") 

annotation above a setter method for an element <NAME>..</NAME> it will fail saying that there cannot be two elements on one single variables.

I hope that it helps.

like image 37
aks Avatar answered Sep 18 '22 04:09

aks


For performing Unmarshall using JAXB:

1) Convert given XML to XSD(by yourself or by online convertor),

2) Create a JAXB project in eclipse,

3) Create XSD file and paste that converted XSD content in it,

4) Right click on **XSD file--> Generate--> JAXB Classes-->follow the instructions(this will create all nessasary .java files in src, i.e., one package-info, object factory and pojo class),

5) Create another .java file in src to operate unmarshall operation, and run it.

Happy Coding !!

like image 7
Soumya Sarkar Avatar answered Jan 01 '70 00:01

Soumya Sarkar


JAXB is a reliable choice as it does xml to java classes mapping smoothely. But there are other frameworks available, here is one such:

https://code.google.com/p/xmappr/

like image 2
Juned Ahsan Avatar answered Jan 01 '70 00:01

Juned Ahsan