There is an XML file that needs to be parsed strictly using JAXB. In the file for one of the objects all fields are empty. UnmarshalException crashes. How to handle this exception so that the remaining valid objects remain? XML example: `

<?xml version="1.1" encoding="UTF-8" ?> <books> <book id="1" category="Java"> <title>Изучаем Java</title> <authors> <author>Кэти Сьерра</author> <author>Берт Бейтс</author> </authors> <description>Мировой компьютерный бестселлер</description> </book> <book id="2" category ="Алгоритмы"> <title>Алгоритмы. Теория и практика</title> <authors> <author>Род Стивенс</author> </authors> </book> <book> <title></title> <authors> <author></author> </authors> <descritption></descritption> </book> </books> 

`Wrap class:

 @XmlRootElement public class Books { private List<Book> bookList = new ArrayList<>(); public Books() { super(); for (Book b : bookList) if (b.isBookNull()) bookList.remove(b); } public void setBookList(List<Book> bookList) { this.bookList = bookList; } public boolean add(Book b) { return bookList.add(b); } @XmlElement(name = "book") public List<Book> getBookList() { return bookList; } } 

Book class:

  @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "book") public class Book { @XmlAttribute(required = true) @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlID private String id; @XmlElement(name = "title") private String title = ""; @XmlAttribute(required = false) private String category = ""; @XmlElement(required = true) private String description = ""; @XmlElementWrapper(name = "authors") @XmlElement(name = "author") private List<Author> authors = new ArrayList<>(); public Book() {} public void setId(String id) { this.id = id; } public void setTitle(String title) { this.title = title; } public void setCategory(String category) { this.category = category; } public void setDescription(String description) { this.description = description; } public void setAuthors(Author author) { authors.add(author); } public String getId() { return id; } public String getTitle() { return title; } public String getCategory() { return category; } public String getDescription() { return description; } public List<Author> getAuthor() { return authors; } @Override public String toString() { return "Книга №" + getId() + ": " + '\"' + getTitle() + '\"' + ", авторы - " + getAuthor() + ", категория - " + getCategory() + ", серия - " + getDescription(); } public int compareToAuthors(Book b) { return this.getAuthor().get(0).compareTo(b.getAuthor().get(0)); } public int compareToBookName(Book b) { return this.getTitle().compareTo(b.getTitle()); } public boolean isBookNull() { return (this.getId() == null || this.getId().equals("") && this.getTitle() == null || this.getTitle().equals("") && this.getCategory() == null || this.getCategory().equals("") && this.getDescription() == null || this.getDescription().equals("") && this.isListAuthorsEmpty()); } private boolean isListAuthorsEmpty() { for (Author a : this.getAuthor()) { if (a.isAuthorAbsence()) this.getAuthor().remove(a); } return this.getAuthor().size() == 0; } } 

Class Author:

 @XmlType(name = "author") @XmlAccessorType(XmlAccessType.FIELD) public class Author { @XmlValue private String name; public Author() {} public Author(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } public int compareTo(Author author) { return this.name.compareTo(author.name); } public boolean isAuthorAbsence() { return this.getName() == null || this.equals(""); } } 

Partially, the parser:

 @Override public void parsingFile(String filePath, String fileName) { try { JAXBContext context = JAXBContext.newInstance(Books.class); Unmarshaller um = context.createUnmarshaller(); FileReader reader = new FileReader(filePath + fileName); Books books = (Books) um.unmarshal(reader); } catch (UnmarshalException ue) { ue.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } 

But the text of the error:

  javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.] 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:214) at by.zarudniy.alexander.parsers.JAXBParserBook.parsingFile(JAXBParserBook.java:28) at by.zarudniy.alexander.parsers.ParserBook.<init>(ParserBook.java:11) at by.zarudniy.alexander.parsers.JAXBParserBook.<init>(JAXBParserBook.java:14) at JAXBTest.testAllFieldsEmpty(JAXBTest.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. 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) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:994) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243) ... 29 more 
  • You gave an example of normal XML. Give an example of the one that breaks - Chubatiy
  • This one breaks, the last <book> tag contains only empty fields, I think therefore. - Alexander Zarudniy
  • Bring pozh. error text + code as you unmarshall the Books class. Because I just tried and everything worked fine for me with the given classes and the above XML - Chubatiy
  • tried with your parser. Works. Maybe the problem is due to any characters specific to the OS. What do you have? I checked on bubunt - Chubatiy
  • Check the contents of the files. Exactly there they are not broken? Those. I, for example, received such an exception when I copied your example into an XML file and copied not everything - Chubatiy

0