Solved

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

Posted on 2012-04-06
6
2,672 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
Independent Software Vendors: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Make the most of your online learning experience.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

690 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