'Unmarshal the List Object Created in POJO classes
I created one Object class which will be my root element and in that I have two attributes and one element shown below:
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Object {
private String id;
private String type;
List <UniqueId> UniqueId;
public List<UniqueId> getUniqueId() {
return UniqueId;
}
public void setUniqueId(List<UniqueId> uniqueId) {
UniqueId = uniqueId;
}
@XmlAttribute
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Object() {
super();
}
public Object(String id, String type, List<classes.UniqueId> uniqueId) {
super();
this.id = id;
this.type = type;
UniqueId = uniqueId;
}
}
and then one class for List <UniqueId> UniqueId;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class UniqueId {
private String Uid;
public String getUid() {
return Uid;
}
public void setUid(String uid) {
Uid = uid;
}
public UniqueId(String uid) {
super();
Uid = uid;
}
public UniqueId() {
super();
}
}
and in the main class, I want to Unmarshal the UniqueId data from VPMobject and I'm getting null value there. How can I fetch the uniqueId if there are n UId's present in the xml file?
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import pojoClasses.VPMObject;
public class MarshalEAD {
public static void main(String[] args) {
try {
File f1 = new File("src\\xmlData\\Test1.xml");
JAXBContext contextObj = JAXBContext.newInstance(Assembly.class);
Unmarshaller unmarshal = contextObj.createUnmarshaller();
// add information or objects to be fetched and written as output
Assembly AD= (Assembly)unmarshal.unmarshal(f1);
System.out.println("Extracted Xml File Information:\n\t");
System.out.println("Root Element: ");
List<VPMObject> list = AD.getVPMObject();
for(VPMObject fetch:list) {
System.out.println("ID: " + fetch.getId());
System.out.println("Type: " + fetch.getType());
System.out.println("UniqueId: " + fetch.getUniqueId() );
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
