?
Solved

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

Posted on 2012-04-06
6
Medium Priority
?
2,886 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

800 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