JAX-B Nested Lists - java

I've got an issue with nested lists & was hoping someone could help.I would like to unmarshal the list in the below XML. I was hoping someone could point me in the right direction, I believe me annotations are off. When I try and unmarshall the MyList array it's returned null.
I have an XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<myList>
<name>N1</name>
<type>T1</type>
<version>V1</version>
</myList>
<myList>
<name>N2</name>
<type>T2</type>
<version>V2</version>
</myList>
</root>
MyList object:
#XmlRootElement(name = "myList")
#XmlAccessorType(XmlAccessType.FIELD)
public class MyList {
#XmlValue
private String name;
#XmlValue
private String type;
#XmlValue
private String version;
Root Class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement
public class Root {
#XmlElementWrapper(name="myLists")
#XmlElement(name="myList")
private List<MyList> list = new ArrayList<MyList>();
Any help would be much appreciated.

The #XmlValue are wrong. In addition, the #XmlElementWrapper is wrong if the myList elements are not wrapped.
Try this, it works for me.
Root.java
package de.lhorn.so;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "root")
public class Root {
#XmlElement(name = "myList")
private List<MyList> list = new ArrayList<>();
public Root() {
}
public List<MyList> getList() {
return list;
}
public void setList(List<MyList> list) {
this.list = list;
}
#Override
public String toString() {
return "Root{" + "list=" + list + '}';
}
}
MyList.java
package de.lhorn.so;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "myList")
#XmlAccessorType(XmlAccessType.FIELD)
public class MyList {
private String name;
private String type;
private String version;
public MyList() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
#Override
public String toString() {
return "MyList{" + "name=" + name + ", type=" + type + ", version=" + version + '}';
}
}
Main
InputStream is = SOPlayground.class.getResourceAsStream("root.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Root root = (Root) jaxbUnmarshaller.unmarshal(is);
System.out.println(root);
Output
Root{list=[MyList{name=N1, type=T1, version=V1}, MyList{name=N2, type=T2, version=V2}]}

try this, It should work.
#XmlRootElement(name = "root")
#XmlAccessorType (XmlAccessType.FIELD)
public class Root {
#XmlElement(name="myList")
private List<MyList> list = new ArrayList<MyList>();
}
unmarshal
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
//We had written this file in marshalling example
Root root= (Root) jaxbUnmarshaller.unmarshal( new File("c:/foo.xml") );
List<MyList> myLists = root.getList();

Related

Mapping XML to Object with same tag names but different attributes

I am trying to convert the below XML string to a Java object using JAXB and eclipse persistence oxm annotations package.
<output>
<rtEvent>
<eventData name="tcppayload">
<data>111111-000000-111111</data>
</eventData>
<eventData name="text">
<data>ABCD</data>
</eventData>
</rtEvent>
</output>
However, the de-serialization does not seem to work. Can someone point out what I might be doing wrong.
Below is the class i'm using to deserialize the string into an object
#XmlRootElement(name = "output")
#XmlAccessorType(XmlAccessType.FIELD)
public class CameraTriggerOutput {
#XmlPath("/rtEvent/eventData[#name=tcppayload]/data/text()")
private String data;
public void toXml() {
try {
JAXBContext ctx = JAXBContext.newInstance(CameraTriggerOutput.class);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(this, System.out);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
After running I get the following output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><output/>
I provide below the code with pure Jaxb, you can try.
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "eventData")
public class EventData {
private String name;
private String data;
#XmlElement(name = "data")
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "rtEvent")
public class RtEvent {
private List<EventData> edataList;
#XmlElement(name = "eventData")
public List<EventData> getEdataList() {
return edataList;
}
public void setEdataList(List<EventData> edataList) {
this.edataList = edataList;
}
}
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "output")
public class Output {
private RtEvent rtEvent;
public RtEvent getRtEvent() {
return rtEvent;
}
public void setRtEvent(RtEvent rtEvent) {
this.rtEvent = rtEvent;
}
public static void main(String[] args) {
try {
EventData eData1 = new EventData();
eData1.setData("111111-000000-111111");
eData1.setName("tcppayload");
EventData eData2 = new EventData();
eData2.setData("ABCD");
eData2.setName("text");
List<EventData> eDataList = new ArrayList<>();
eDataList.add(eData1);
eDataList.add(eData2);
RtEvent rtEvent = new RtEvent();
rtEvent.setEdataList(eDataList);
Output output = new Output();
output.setRtEvent(rtEvent);
JAXBContext ctx = JAXBContext.newInstance(Output.class);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(output, System.out);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
You Jaxb only as it is part of java, there is no need to include any other annotations. You can make individual classes in your ide and you can test the class Output which has a main method.

JAXB Annotated Class to Map

I have a class like this
public class Item {
#XmlElement(name = "my_id")
private String id;
#XmlElement(name = "my_type")
private String type;
}
I would like to convert this class to a Map which considers the jaxb annotated fields.
E.g. the Result is a map with following entries:
Key: my_id , Value: "the id"
Key: my_type , Value: "the type"
I am not sure how your xml looks like but assuming, it will be an item element, under some parent, you can do it usingadapter (XmlJavaTypeAdapter) . Sample code looks like below:
package test;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
#XmlAccessorType(XmlAccessType.FIELD)
public class Item {
public Item() {
}
#XmlElement(name = "my_id")
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
#XmlElement(name = "my_type")
private String type;
public String toString() {
return "Item : id-" + getId() + ", type -" + getType();
}
public static void main(String[] args) throws Exception {
String xmlString = "<items>\n"
+ " <item>\n"
+ " <my_id>someID</my_id>\n"
+ " <my_type>someType</my_type>\n"
+ " </item>\n"
+ "</items>";
//System.out.println("xmlString.." + xmlString);
RootElement o = unmarshal(RootElement.class, xmlString);
System.out.println("item Map : "+o.getItem());
}
private static <C> C unmarshal(Class<C> c, String sampleXML) throws Exception {
JAXBContext jc = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader reader = new StringReader(sampleXML);
//System.out.println("" + sampleXML);
return (C) unmarshaller.unmarshal(reader);
}
}
#XmlRootElement(name = "items")
#XmlAccessorType(XmlAccessType.FIELD)
class RootElement {
public RootElement() {
System.out.println("RootElement");
}
public Map<String, String> getItem() {
return item;
}
public void setItem(Map<String, String> item) {
this.item = item;
}
#XmlJavaTypeAdapter(ItemAdapter.class)
#XmlElement()
private Map<String, String> item;
}
class ItemAdapter extends XmlAdapter<Item, Map<String, String>> {
#Override
public Map<String, String> unmarshal(Item i) throws Exception {
Map<String, String> r = new HashMap<String, String>();
r.put("my_id", i.getId());
r.put("my_type", i.getType());
return r;
}
#Override
public Item marshal(Map<String, String> v) throws Exception {
Item i = new Item();
i.setId(v.get("my_id"));
i.setType(v.get("my_type"));
return i;
}
}

Converting XML to Java Object using jaxb (Unmarshal)

I am trying to convert my below xml to java object.
This is my xml:
<ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:voc="urn:hl7-org:v3/voc">
<confidentialityCode code="" codeSystem=""/>
<languageCode code="en-"/>
<recordTarget>
<patientRole>
<id root="" extension=""/>
<telecom value="" use=""/>
<providerOrganization>
<id root="" extension=""/>
<id root="" extension=""/>
<name>Something</name>
<telecom value=""/>
<addr use="">
<state></state>
<city></city>
<postalCode></postalCode>
<streetAddressLine>2121</streetAddressLine>
</addr>
</providerOrganization>
</patientRole>
</recordTarget>
</ClinicalDocument>
I need to get the value of "name" under "providerOrganization".
Below are my Java classes.
ClinicalDocument.java
package com.biclinical.data;
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name="ClinicalDocument", namespace="urn:hl7-org:v3")
public class ClinicalDocument {
#XmlElement(name="recordTarget")
private List<RecordTarget> recordTarget;
public List<RecordTarget> getRecordTarget() {
return recordTarget;
}
public void setRecordTarget(List<RecordTarget> recordTarget) {
this.recordTarget = recordTarget;
}
#Override
public String toString() {
return "ClinicalDocument [recordTarget=" + recordTarget + "]";
}
}
RecordTarget.java
package com.biclinical.data;
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name="recordTarget")
public class RecordTarget {
#XmlElement(name="patientRole")
private List<PatientRole> patientRole;
public List<PatientRole> getPatientRole() {
return patientRole;
}
public void setPatientRole(List<PatientRole> patientRole) {
this.patientRole = patientRole;
}
#Override
public String toString() {
return "RecordTarget [patientRole=" + patientRole +"]";
}
}
PatientRole.java
package com.biclinical.data;
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "patientRole")
public class PatientRole {
/*#XmlElement(name = "id")
private String id;
Double root;
String extension;*/
#XmlElement(name="providerOrganization")
private List<ProviderOrganization> providerOrganization;
public List<ProviderOrganization> getProviderOrganization() {
return providerOrganization;
}
public void setProviderOrganization(List<ProviderOrganization> providerOrganization) {
this.providerOrganization = providerOrganization;
}
}
ProviderOrganisation.java
package com.biclinical.data;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name="providerOrganization")
public class ProviderOrganization {
#XmlElement(name="name")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "Patient [Name=" + name + "]";
}
}
XMLFileParserSAXUtility.java
public class XMLFileParserSAXUtility extends DefaultHandler {
public static void main(String[] args) {
try {
File file = new File("C:/Users/shivendras/Desktop/Patient19999_Test_Organization1.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {ClinicalDocument.class,RecordTarget.class,PatientRole.class,ProviderOrganization.class});
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ClinicalDocument clinicalDocument = (ClinicalDocument) jaxbUnmarshaller.unmarshal(file);
//clinicalDocument.getRecordTarget()
String s = ((File) ((PatientRole) ((RecordTarget) clinicalDocument.getRecordTarget()).getPatientRole()).getProviderOrganization()).getName();
System.out.println(s);
} catch (JAXBException e) {
e.printStackTrace();
}
}
I get the result as
Exception in thread "main" java.lang.NullPointerException
at com.biclinical.util.XMLFileParserSAXUtility.main(XMLFileParserSAXUtility.java:27)
And if i try to print syso(clinicalDocument);
Result is ClinicalDocument [recordTarget=null]
Please help me out here!
I think you add the namespace to your #XmlElement :
#XmlElement(name="patientRole")
private List<PatientRole> patientRole;
Should be :
#XmlElement(name="patientRole",namespace="urn:hl7-org:v3")
private List<PatientRole> patientRole;
If you're having any other null in your objects, try adding the namespace.
Also, #XmlRootEntity is only necessary for your root element, in this case your ClinicalDocumentclass, and you only need to give the root class to your JAXBContext :
JAXBContext jaxbContext = JAXBContext.newInstance(ClinicalDocument.class);

Marshall a List to XML works - but how to unmarshall?

I can marshall a ObservableList using a "Wrapper"-class like below. But I cannot unmarshall it back to the wrapperclass it was before.
The idea is:
I have an ObservableList of "Expenses". I put this List into a wrapper-class and save this class to XML. The result looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<List>
<root>
<category>[none]</category>
<period>Year</period>
<title>asd</title>
<value>354</value>
</root>
</List>
I cannot bring it back to the wrapper-object.
I really appreciate any kind of help.
Main-class JAXBContext (visible for all):
JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class, Expense.class);
Main-class SAVEBUTTON:
public class SaveButtonListener implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent arg0) {
File serializedFile = new File(PATH);
try {
if (serializedFile.exists() == false)
serializedFile.createNewFile();
PrintWriter xmlOut = new PrintWriter(serializedFile);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
List<Expense> saveList = new ArrayList<>();
saveList.addAll(data);
MyWrapperForList<Expense> wrapper = new MyWrapperForList<>(saveList);
JAXBElement<MyWrapperForList> jaxbElement = new JAXBElement<>(
new QName("List"), MyWrapperForList.class, wrapper);
m.marshal(jaxbElement, xmlOut);
xmlOut.flush();
xmlOut.close();
Main-class-LOADBUTTON:
public class LoadButtonListener implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent arg0) {
try {
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource(PATH);
MyWrapperForList<Expense> unwrapper = unmarshaller.unmarshal(xml,
MyWrapperForList.class).getValue();
List<Expense> tempList = new ArrayList<>();
tempList.addAll(unwrapper.getItems());
System.out.println(tempList.get(0).getTitle());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Wrapper-class:
public class MyWrapperForList {
private List<Expense> list;
public MyWrapperForList() {
list = new ArrayList<>();
}
public MyWrapperForList(List<Expense> expenses) {
this.list = expenses;
}
#XmlAnyElement(lax=true)
public List<Expense> getItems() {
return list;
}
}
Expense-class:
#XmlRootElement(name = "root")
public class Expense {
private String title;
private String category;
private String period;
private String value;
public Expense() {} //Default constructor is needed for XML-handling
public Expense(String title, String value, String period, String category) {
this.title = title;
this.value = value;
this.period = period;
this.category = category;
}
#XmlElement(name = "title")
public String getTitle() {
return this.title;
}
#XmlElement(name = "category")
public String getCategory() {
return this.category;
}
#XmlElement(name = "period")
public String getPeriod() {
return this.period;
}
#XmlElement(name = "value")
public String getValue() {
return this.value;
}
}
I used this tutorial from Blaise Doughan: http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html
MyListWrapper
If you want MyWrapperForList to unmarshal holding an instance of ObservableList then you will need to setup your class in one of the following ways.
Property of Type ObservableList
import javax.xml.bind.annotation.XmlAnyElement;
import javafx.collections.*;
public class MyWrapperForList<T> {
private ObservableList<T> list;
public MyWrapperForList() {
list = FXCollections.<T>observableArrayList();
}
public MyWrapperForList(ObservableList<T> list) {
this.list = list;
}
#XmlAnyElement(lax = true)
public ObservableList<T> getItems() {
return list;
}
}
List Property Initialized to Instance of ObservableList
import java.util.List;
import javax.xml.bind.annotation.XmlAnyElement;
import javafx.collections.*;
public class MyWrapperForList<T> {
private List<T> list = FXCollections.<T>observableArrayList();
public MyWrapperForList() {
list = FXCollections.<T>observableArrayList();
}
public MyWrapperForList(List<T> list) {
this.list = list;
}
#XmlAnyElement(lax = true)
public List<T> getItems() {
return list;
}
}
Demo Code
Input (nub.xml)
<List>
<root>
<category>[none]</category>
<period>Year</period>
<title>dfg</title>
<value>4</value>
</root>
<root>
<category>[none]</category>
<period>Year</period>
<title>ROBO</title>
<value>1234</value>
</root>
</List>
Demo
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class, Expense.class);
//UNMARSHALLING
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("src/forum18594548/nub.xml");
MyWrapperForList<Expense> wrapper = (MyWrapperForList<Expense>) unmarshaller.unmarshal(xml, MyWrapperForList.class).getValue();
List<Expense> data = wrapper.getItems();
System.out.println(data.getClass());
for(Expense expense : data) {
System.out.println(expense);
}
}
}
Output
class com.sun.javafx.collections.ObservableListWrapper
forum18594548.Expense#789df61d
forum18594548.Expense#4a8927c8
UPDATE
First: Thanks for you work Blaise!! I'm really glad for what you do to
me! I tried what you wrote here (it was nearly the same as I had) and
I got a similar (same type of) output as you got. BUT the objects in
the lists are all referenced with null. If I write
System.out.println(data.get(0).getTitle()); it says null. There is the
exact amount of objects in the list, but all attributes are referenced
with null. :(
I think I got tunnel vision on the ObservableList aspect only to miss your real problem was with how you mapped the Expense class. Since you only have get methods you should map to the fields using #XmlAccessorType(XmlAccessType.FIELD) as follows.
import javax.xml.bind.annotation.*;
#XmlRootElement(name="root")
#XmlAccessorType(XmlAccessType.FIELD)
public class Expense {
private String title;
private String category;
private String period;
private String value;
public Expense() {
}
public Expense(String title, String value, String period, String category) {
this.title = title;
this.value = value;
this.period = period;
this.category = category;
}
public String getTitle() {
return this.title;
}
public String getCategory() {
return this.category;
}
public String getPeriod() {
return this.period;
}
public String getValue() {
return this.value;
}
}

JAVA/JAXB Help needed

This is gonna be lengthy but I need some enlightenment. I'm new to JAXB so please be lenient with me.
CourseApp:
package Courses;
import java.io.File;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class CoursesApp {
public static void main(String[] args) {
Courselist courselist = new Courselist();
courselist.setclassType("Lecture");
courselist.setcourseCode("2002");
courselist.setgroupIndex("1");
courselist.setprofessor("Professor James");
try{
File file = new File("C:\\Courselist.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Courselist.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(courselist, file);
jaxbMarshaller.marshal(courselist, System.out);
}catch(JAXBException e)
{
e.printStackTrace();
}
}
}
Courselist:
package Courses;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Courselist {
String courseCode;
String classType;
String groupIndex;
String professor;
public String getcourseCode() {
return courseCode;
}
#XmlElement
public void setcourseCode(String courseCode) {
this.courseCode = courseCode;
}
public String getclassType() {
return classType;
}
#XmlElement
public void setclassType(String classType) {
this.classType = classType;
}
public String getgroupIndex() {
return groupIndex;
}
#XmlElement
public void setgroupIndex(String groupIndex) {
this.groupIndex = groupIndex;
}
public String getprofessor() {
return professor;
}
#XmlElement
public void setprofessor(String professor) {
this.professor = professor;
}
}
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <courselist>
<classType>Lecture</classType>
<courseCode>2002</courseCode>
<groupIndex>1</groupIndex>
<professor>Professor James</professor>
</courselist>
What I want is to create another instance of courselist within the same XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <courselist>
-<course>
<classType>Lecture</classType>
<courseCode>2002</courseCode>
<groupIndex>1</groupIndex>
<professor>Professor James</professor>
</course>
-<course>
<classType>Lecture</classType>
<courseCode>2003</courseCode>
<groupIndex>2</groupIndex>
<professor>Professor John</professor>
</course>
</courselist>
I would recommend to have one member in CourseList: List<Course> when Course will include all the members currently in CourseList.
This is the code:
#XmlRootElement
public class Courselist {
#XmlElement List<Course> course = new ArrayList<Course>();
}
Courselist
As oshai answered I would have a model with two classes Courselist and Course. Below is what the Courselist class would look like. To match Java programming conventions the package name is normally lower case. Also it is also often based on a domain name (such as com.example.courses). By default JAXB (JSR-222) implementations look for metadata on the property (get or set methods) so I've put them there (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).
package courses;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Courselist {
List<Course> courses;
#XmlElement(name="course")
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}
Course
The information you had in the Courselist class I have moved to a new Course class. JAXB is configuration by exception so you only need to add annotations where you wish the XML representation to differ from the default. In your use case you don't need any annotations on this class (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html). I have fixed the casing on your property methods to match the normal Java coding conventions.
package courses;
public class Course {
String courseCode;
String classType;
String groupIndex;
String professor;
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
public String getClassType() {
return classType;
}
public void setClassType(String classType) {
this.classType = classType;
}
public String getGroupIndex() {
return groupIndex;
}
public void setGroupIndex(String groupIndex) {
this.groupIndex = groupIndex;
}
public String getProfessor() {
return professor;
}
public void setProfessor(String professor) {
this.professor = professor;
}
}

Categories