Solved

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

Posted on 2012-04-06
6
2,475 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

730 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