As per link: Why has AnnotationReader been removed from JAXB reference implementation?, you need to simply use below maven dependencies:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
You need to refactor code a bit. Also looks like you've not created same name fields of model class, it should be like below: Person.java
@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement
private String first;
@XmlElement
private String last;
@XmlElement
private String age;
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Person [first=" + first + ", last=" + last + ", age=" + age + "]";
}
}
Book.java
@XmlRootElement(name="book")
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
private List<Person> person = new ArrayList<Person>();
public List<Person> getPerson() {
return person;
}
public void setPerson(List<Person> person) {
this.person = person;
}
}
ReadXMLFileJaxb.java
public class ReadXMLFileJaxb {
public static void main(String[] args) {
File file = new File(ReadXMLFileDOM.class.getClassLoader().getResource("book.xml").getFile());
try {
JAXBContext context = JAXBContext.newInstance(Book.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Book book = (Book) unmarshaller.unmarshal(file);
System.out.println(book.getPerson().size());
for (int i = 0; i < book.getPerson().size(); i++) {
System.out.println("------------");
System.out.println(book.getPerson().get(i).getFirst());
System.out.println(book.getPerson().get(i).getLast());
System.out.println(book.getPerson().get(i).getAge());
}
} catch (JAXBException e) {
System.out.println(e.getMessage());
}
}
}
The below output I see:
3
------------
Kiran
Pai
22
------------
Bill
Gates
46
------------
Steve
Jobs
40
Although Issue is very old but still answering. Root cause is that com.sun.xml.bind is obsolete now. org.glassfish.jaxb is latest reference implementation of JAXB API. Using below JAXB RI maven dependency would solve the issue.
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.2.11</version>
</dependency>
The best solution that I found is using maven-jaxb2-plugin, then the only dependency that you need is:
<!-- https://mvnrepository.com/artifact/org.jvnet.jaxb2.maven2/maven-jaxb2-plugin -->
<dependency>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With