Solved

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

Posted on 2012-04-06
6
2,036 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Ronjer
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now