Solved

How to convert nested xml element with attributes to Java Object using JAXB?

Posted on 2012-04-06
6
2,260 Views
Last Modified: 2013-11-13
hi experts,

i have this xml:-

<?xml version="1.0" encoding="UTF-8"?>
<employee id="ROOT">
    <employee id="employee1">
        <employee id="employee2"/>
        <employee id="employee3"/>
    </employee>
    <employee id="employee4">
        <employee id="employee5"/>
    </employee>
</employee>

Open in new window


and my java object is:-

public class Employee{
    public String id;
    public List<Employee> empList;

    public Employee(){
        empList = new ArrayList<Employee>();
    }
}

Open in new window


how do i convert the Nested xml element to the java object define above?

the case based on above, the final result where the java object will hold is

Employee object has id="ROOT" and its empList will have 2 Employee object which id are "employee1" and "employee4" and so on.

I've searched on google, all of the example was meant for simple xml 1 level element which can't apply to my case as the xml has nested elements.

can Jaxb convert it?

please advice
0
Comment
Question by:Ronjer
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 37823131
You create elements based on an xsd. If you want to nest elements you need to define this structure in your xsd file. You could do *something* like the following:

<xsd:element name="Employee">
		<xsd:complexType>
			<xsd:sequence>
				<xsd:element minOccurs="1" ref="Employee />
			</xsd:sequence>
		</xsd:complexType>
	</xsd:element>

Open in new window

0
 

Author Comment

by:Ronjer
ID: 37826594
hi there,

i think you misinterpret my question, what i want is to convert the xml given example to java object using JAXB 2. please advice
0
 
LVL 35

Expert Comment

by:girionis
ID: 37836733
In order to do that you need to have a schema first. Otherwise JAXB does not know how to convert XML to Java classes. First you define the schema and then you convert your XML.

Have a look here for a simple tutorial: http://jaxb.java.net/tutorial/section_1_3-Hello-World.html#Hello%20World
and here for more information: http://jaxb.java.net/tutorial/
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Ronjer
ID: 37839770
Is there no way to convert the xml into java object directly instead relying on the schema?
the xml is not generated on my side, its passed from other source. I found out that Jaxb has rich set of Annotations that can annotate fields to unmarshal xml into java object.

my environment is :-
win 7,
jdk 6

please advice
0
 

Accepted Solution

by:
Ronjer earned 0 total points
ID: 37869135
found the solution,

all that need to do is annotate the java class given below:-

@XmlRootElement(name="country")
@XmlAccessorType(XmlAccessType.FIELD)
class Employee {

    @XmlAttribute
    private String id;

    @XmlElement(name="employee")
    private List<Employee> empList;

}

Open in new window


with this xml:- "mydata.xml" added country element to identify the root element

<?xml version="1.0" encoding="UTF-8"?>
<country>
<employee id="ROOT">
    <employee id="employee1">
        <employee id="employee2"/>
        <employee id="employee3"/>
    </employee>
    <employee id="employee4">
        <employee id="employee5"/>
    </employee>
</employee>
</country>

Open in new window


the procedure to convert it is straight forward:-


public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Employee.class);
        
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("mydata.xml");
        Employee employee = (Employee) unmarshaller.unmarshal(xml);
        
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(employee, System.out);
    }
}

Open in new window


the above code however were provided by another expert on JAXB matter:- credit should be given to him "Blaise Doughan"
0
 

Author Closing Comment

by:Ronjer
ID: 37885380
because the question has be idle for some time, and i found the solution provided by yet another expert on this JAXB matter solely, since it gets the job done and simple to execute, therefore i accept it as a solid solution
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This is about my first experience with programming Arduino.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question