2016-07-20 64 views
0

以前可能會問這個問題,但我已經嘗試過幾乎所有以前發佈的解決方案。我很新的這一點,其中的問題是我無法弄清楚:使用JAXB的Unmarshalle xml文件

以下是我的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<TaxAddressFileDeliveryConfirmation> 
    <ReturnInfo> 
    <ErrorCode>FAILURE</ErrorCode> 
    <ErrorMessage>abc. 
    </ErrorMessage> 
</ReturnInfo> 
<TaxAddressFileDeliveryNotification> 
    <FileNameInput>abc 
    </FileNameInput> 
    <FileNameOutput>abc 
    </FileNameOutput> 
    <MessageCount>20000</MessageCount> 
    <ReplyMessageSubject>abc 
    </ReplyMessageSubject> 
    <ProcessingOption NAME="TAR">Y</ProcessingOption> 
</TaxAddressFileDeliveryNotification> 
</TaxAddressFileDeliveryConfirmation> 

以下是Java代碼:

public class CheckSampleXMLUnmarshall { 
    public static void main(String[] args) { 
     try { 

      File file = new File(
        "Sample.xml"); 
      JAXBContext jaxbContext = JAXBContext 
        .newInstance(TaxAddressFileDeliveryConfirmation.class); 

      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
      Object resposeObj = JAXBIntrospector.getValue(jaxbUnmarshaller 
        .unmarshal(file)); 
      System.out.println(resposeObj); 

     } catch (JAXBException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我得到以下錯誤:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"TaxAddressFileDeliveryConfirmation"). Expected elements are <{http://www.example.org/TaxAddressFileDeliveryNotification}TaxAddressDeliveryNotification>,<{http://www.example.org/TaxAddressFileDeliveryConfirmation}TaxAddressFileDeliveryConfirmation> 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1048) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3065) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:881) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764) 
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123) 
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210) 
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203) 
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175) 
    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 CheckSampleXMLUnmarshall.main(CheckSampleXMLUnmarshall.java:20) 

我到底需要檢查什麼?

+0

看起來你的xml不符合它的模式。特別是,它看起來像命名空間已關閉。你有架構嗎? – bradimus

回答

1

您的XML不符合您的模式(XSD),或者至少它不符合從XSD(或您以某種其他方式創建的與XSD匹配的方式創建的)生成的JAXB類。

的XSD在http://www.example.org/TaxAddressFileDeliveryConfirmation命名空間期待的元素:{http://www.example.org/TaxAddressFileDeliveryConfirmation}TaxAddressFileDeliveryConfirmation但XML文檔顯示了一個不合格的(沒有命名空間)TaxAddressFileDeliveryConfirmation元素

如果你手工創建這個XML文件,你可能需要限定像這樣的頂級元素:

<?xml version="1.0" encoding="UTF-8"?> 
<tns:TaxAddressFileDeliveryConfirmation xmlns:tns="http://www.example.org/TaxAddressFileDeliveryConfirmation"> 
... 

但是完整的答案取決於您的XSD。

這是一個非常簡單的問題,所以你應該在StackOverflow上找到很多類似的問題/答案。