I have below XML -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<objectWithList>
<MyList>
<abc>hi</abc>
<list>Somewhere</list>
<list>This and that</list>
<list>Something</list>
</MyList>
</objectWithList>
which I want to convert to java object using jaxb unmarshaller.
Java File for xml mapping-
@XmlRootElement
public class ObjectWithList {
@XmlElement
private String abc;
private List<String> list;
@XmlElementWrapper(name="MyList")
@XmlElement
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
main driver class code for conversion -
public class Test {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectWithList.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ObjectWithList emps = (ObjectWithList) jaxbUnmarshaller.unmarshal(new File("c:/emp.xml"));
for (String s : emps.getList()) {
System.out.println(s);
}
System.out.println(" ");
}
}
but this is not working. I am getting the below exception -
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; systemId: file:/c:/emp.xml; lineNumber: 1; columnNumber: 10; The processing instruction target matching "[xX][mM][lL]" is not allowed.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
at com.netspend.idology.Test.main(Test.java:12)
Caused by: org.xml.sax.SAXParseException; systemId: file:/c:/emp.xml; lineNumber: 1; columnNumber: 10; The processing instruction target matching "[xX][mM][lL]" is not allowed.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
This problem has taken my whole day. Any kind of help is much appreciated.
Please find below the modified program which works. Also you have to understand about the usage of structure of class and the use of annotation at proper places.
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement
public class ObjectWithList {
private MyList myList;
@XmlElement(name = "MyList")
public MyList getMyList() {
return myList;
}
public void setMyList(MyList myList) {
this.myList = myList;
}
@Override
public String toString() {
return "ObjectWithList{" + "myList=" + myList + '}';
}
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "MyList")
public class MyList {
private String abc;
private List<String> list;
@XmlElement(name = "abc")
public String getAbc() {
return abc;
}
public void setAbc(String abc) {
this.abc = abc;
}
@XmlElement(name = "list")
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
public String toString() {
return "MyList{" + "abc='" + abc + '\'' + ", list=" + list + '}';
}
}
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.List;
public class Test {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectWithList.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ObjectWithList emps =
(ObjectWithList)
jaxbUnmarshaller.unmarshal(
new File("D:/someFolder/emp.xml"));
for (String s : emps.getMyList().getList()) {
System.out.println(s);
}
System.out.println(emps);
System.out.println(" ");
}
}