如何在JXBI中发生错误后继续解析以收集所有JAXB Unmarshalling错误,直到xml文件结束

时间:2017-07-05 06:11:50

标签: java xml xml-parsing jaxb unmarshalling

对于样本,我有xml和xsd以下。

当我尝试在xml下解析时,我希望JAXB解析器应该解析到文件的末尾并且一次性获得所有错误。

XML出现多个错误如下所示:它缺少</purchasedOn</itemName的结束标记。

如果我的输入xml有多个错误,它将抛出异常并在JXBI解析器遇到第一个错误时停止解析。

我的问题是:有没有办法继续使用JAXB对任何输入xml进行解析,即使遇到第一个错误,期待更多错误收集所有错误直到文件结尾

Exception in thread "main" javax.xml.bind.UnmarshalException
     - with linked exception:
    [org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 7; The end-tag for element type "purchasedOn" must end with a '>' delimiter.]
        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:204)
        at com.user.ezpense.processor.JAXBProcessor.main(JAXBProcessor.java:36)
    Caused by: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 7; The end-tag for element type "purchasedOn" must end with a '>' delimiter.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)

错误输入xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <expenseReport>
      <user>
        <userName>Sanaulla</userName>
      </user>
      <items>
        <item>
          <itemName>Seagate External HDD</itemName>
          <purchasedOn>August 24, 2010</purchasedOn
          <amount>6776.5</amount>
        </item>
        <item>
          <itemName>Benq HD Monitor</itemName
          <purchasedOn>August 24, 2012</purchasedOn>
          <amount>15000</amount>
        </item>
      </items>
    </expenseReport> 

xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="expenseReport" type="ExpenseT" />
  <xs:complexType name="ExpenseT">
    <xs:sequence>
      <xs:element name="user" type="UserT" />
      <xs:element name="items" type="ItemListT" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="UserT">
    <xs:sequence>
      <xs:element name="userName" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ItemListT">
    <xs:sequence>
      <xs:element name="item" type="ItemT" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ItemT">
    <xs:sequence>
      <xs:element name="itemName" type="xs:string" />
      <xs:element name="purchasedOn" type="xs:string" />
      <xs:element name="amount" type="xs:decimal" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

更正XML expense.xml

<?xml version="1.0"?>
<x:catalog xmlns:x="urn:books">
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with
      XML.</description>
   </book>
</x:catalog>

我为使用JAXB解析输入xml而编写的Java代码: Java解析代码:

package com.user.ezpense.processor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.user.ezpense.report.jaxb.ExpenseT;
import com.user.ezpense.report.jaxb.ItemListT;
import com.user.ezpense.report.jaxb.ItemT;
import com.user.ezpense.report.jaxb.ObjectFactory;
import com.user.ezpense.report.jaxb.UserT;

public class JAXBProcessor {

  /**
   * @param args
   * @throws JAXBException 
 * @throws FileNotFoundException 
   */
  public static void main(String[] args) throws JAXBException, FileNotFoundException {
    //1. We need to create JAXContext instance
    JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);

    //2. Use JAXBContext instance to create the Unmarshaller.
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    //3. Use the Unmarshaller to unmarshal the XML document to get an instance of JAXBElement.
    FileInputStream in = new FileInputStream(new File("expense.xml"));

    JAXBElement<ExpenseT> unmarshalledObject = 
        (JAXBElement<ExpenseT>)unmarshaller.unmarshal(in);

    //4. Get the instance of the required JAXB Root Class from the JAXBElement.
    ExpenseT expenseObj = unmarshalledObject.getValue();
    UserT user = expenseObj.getUser();
    ItemListT items = expenseObj.getItems();

    //Obtaining all the required data from the JAXB Root class instance.
    System.out.println("Printing the Expense for: "+user.getUserName());
    for ( ItemT item : items.getItem()){
      System.out.println("Name: "+item.getItemName());
      System.out.println("Value: "+item.getAmount());
      System.out.println("Date of Purchase: "+item.getPurchasedOn());
    }   
  }

}

0 个答案:

没有答案