2013-12-14 7 views
39

मुझे यह त्रुटि मिल रही है, जबकि एक स्ट्रिंग unmarshalling। मैंने जेएक्सबी का उपयोग कर जावा फाइलें बनाई हैं।javax.xml.bind.UnmarshalException: अप्रत्याशित तत्व। अपेक्षित तत्व हैं (कोई भी नहीं)

JAXBContext jaxbContext = JAXBContext.newInstance(DocumentType.class); 
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 
// Input string posted below 
DocumentType dType = (DocumentType) unmarshaller.unmarshal(new StringReader("input string")); 

स्टैक ट्रेस: ​​

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd", local:"document"). Expected elements are (none) 
    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:214) 
    at JaxbTest.main(JaxbTest.java:19) 

XSD:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema targetNamespace="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" 
    elementFormDefault="qualified"> 

    <xs:element name="document" type="tns:documentType"></xs:element> 

    <xs:complexType name="documentType"> 
     <xs:sequence> 
      <xs:element name="businessCard" type="tns:businessCardType" minOccurs="0" maxOccurs="1"></xs:element> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="businessCardType"> 
     <xs:sequence> 
      <xs:element name="field" type="tns:fieldType" minOccurs="0" maxOccurs="unbounded"></xs:element> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="fieldType"> 
     <xs:sequence> 
      <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"> 
      </xs:element> 
      <xs:element name="characters" type="tns:charactersType" minOccurs="0" maxOccurs="1"></xs:element> 
      <xs:element name="fieldComponents" type="tns:fieldComponentsType" minOccurs="0" maxOccurs="1"></xs:element> 
     </xs:sequence> 
     <xs:attribute name="type" type="tns:typeType"></xs:attribute> 
    </xs:complexType> 

    <xs:simpleType name="typeType"> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="Name"></xs:enumeration> 
      <xs:enumeration value="Phone"></xs:enumeration> 
      <xs:enumeration value="Mobile"></xs:enumeration> 
      <xs:enumeration value="Fax"></xs:enumeration> 
      <xs:enumeration value="Company"></xs:enumeration> 
      <xs:enumeration value="Job"></xs:enumeration> 
      <xs:enumeration value="Address"></xs:enumeration> 
      <xs:enumeration value="Email"></xs:enumeration> 
      <xs:enumeration value="Web"></xs:enumeration> 
      <xs:enumeration value="Text"></xs:enumeration> 
     </xs:restriction> 
    </xs:simpleType> 

    <xs:complexType name="charactersType"> 
     <xs:sequence> 
      <xs:element name="char" type="tns:charType" minOccurs="0" maxOccurs="unbounded"></xs:element> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="charType"> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
       <xs:attribute name="suspicious" type="xs:boolean" use="optional"></xs:attribute> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <xs:complexType name="fieldComponentsType"> 
     <xs:sequence> 
     <xs:element name="fieldComponent" type="tns:fieldComponentType" minOccurs="0" maxOccurs="unbounded"></xs:element> 
     </xs:sequence> 
    </xs:complexType> 


    <xs:complexType name="fieldComponentType"> 
     <xs:sequence> 
     <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"> 
     </xs:element> 
     </xs:sequence> 
     <xs:attribute name="type" type="tns:fieldComponentTypeType"></xs:attribute> 
    </xs:complexType> 

    <xs:simpleType name="fieldComponentTypeType"> 
     <xs:restriction base="xs:string"> 
     <xs:enumeration value="FirstName"></xs:enumeration> 
     <xs:enumeration value="MiddleName"></xs:enumeration> 
     <xs:enumeration value="LastName"></xs:enumeration> 
     <xs:enumeration value="ExtraName"></xs:enumeration> 
     <xs:enumeration value="PhonePrefix"></xs:enumeration> 
     <xs:enumeration value="PhoneCountryCode"></xs:enumeration> 
     <xs:enumeration value="PhoneCode"></xs:enumeration> 
     <xs:enumeration value="PhoneBody"></xs:enumeration> 
     <xs:enumeration value="PhoneExtension"></xs:enumeration> 
     <xs:enumeration value="ZipCode"></xs:enumeration> 
     <xs:enumeration value="Country"></xs:enumeration> 
     <xs:enumeration value="City"></xs:enumeration> 
     <xs:enumeration value="StreetAddress"></xs:enumeration> 
     <xs:enumeration value="JobPosition"></xs:enumeration> 
     <xs:enumeration value="JobDepartment"></xs:enumeration> 
     </xs:restriction> 
    </xs:simpleType> 

</xs:schema> 

इनपुट स्ट्रिंग:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd"> 
<businessCard> 
<field type="Phone"> 
    <value>783-37-00</value> 
</field> 
<field type="Phone"> 
    <value>783-37-01</value> 
</field> 
<field type="Fax"> 
    <value>783-26-63</value> 
</field> 
<field type="Email"> 
    <value>[email protected]</value> 
</field> 
<field type="Web"> 
    <value>www.abbyy.com</value> 
</field> 
<field type="Address"> 
    <value>Otradnaya str., 2b, bld.6, 127273, Moscow, Russia</value> 
</field> 
<field type="Name"> 
    <value>John Smith</value> 
</field> 
<field type="Company"> 
    <value>ABBYY Headquarters</value> 
</field> 
<field type="Job"> 
    <value>Product Analyst</value> 
</field> 
<field type="Text"> 
    <value>ABBYY ABBYY Headquarters John Smith Product Analyst ABBYY Headquarters Otradnaya str., 2b, bld.6, 127273, Moscow, Russia Tel: 783-37-00 Fax: 783-26-63 [email protected] www.abbyy.com </value> 
</field> 
</businessCard> 
</document> 

उत्तर

85

जब आप किसी XML स्कीमा से एक JAXB मॉडल उत्पन्न वैश्विक तत्व नामित जटिल ty के अनुरूप है पेस में ObjectFactory कक्षा में एक विधि विधि पर मेटाडाटा @XmlElementDecl एनोटेशन के रूप में कब्जा कर लिया जाएगा। चूंकि आप कक्षा पर JAXBContext बना रहे हैं, इसलिए इस मेटाडेटा को संसाधित नहीं किया जा रहा है। यदि आपने अपने जेएक्सबी मॉडल को एक्सएमएल स्कीमा से जेनरेट किया है तो आपको जेनरेट किए गए पैकेज नाम या ObjectFactory कक्षा पर JAXBContext बनाना चाहिए ताकि यह सुनिश्चित किया जा सके कि सभी आवश्यक मेटाडेटा संसाधित हो जाएं।

उदाहरण समाधान:

JAXBContext jaxbContext = JAXBContext.newInstance(my.generatedschema.dir.ObjectFactory.class); 
DocumentType documentType = ((JAXBElement<DocumentType>) jaxbContext.createUnmarshaller().unmarshal(inputStream)).getValue(); 
+6

वर्क्स ठीक। unmarshaller.unmarshal() दस्तावेज़ प्रकार के बजाय JAXBElement वापस कर देगा। लपेटा ऑब्जेक्ट प्राप्त करने के लिए getValue() पर कॉल करें। – rwitzel

+0

मेरे मामले में, आयातित रूट तत्व को कॉल करने के लिए 'unmarshaller = context.createUnmarshaller() आवश्यक है; अंतिम ऑब्जेक्ट ऑब्जेक्ट = unmarshaller.unmarshal (नई फ़ाइल ("पथ/से/data.xml")); ; अंतिम DocumentType सीडी = (DocumentType) वस्तु ' – CodeMed

+4

दूसरा कदम इस तरह दिखता है:।' DocumentType डॉक = (। (JAXBElement ) jaxbContext.createUnmarshaller() unmarshal ( InputStream)) getValue(); ' –

19

वैकल्पिक रूप से, अगर आप DocumentType वर्ग का उपयोग करने में बनाए रखना चाहते। फिर आप अपने दस्तावेज़ टाइप श्रेणी के शीर्ष पर निम्न एनोटेशन जोड़ सकते हैं।

@XmlRootElement(name="document") 

नोट: स्ट्रिंग मान "document" एक्सएमएल संदेश की जड़ टैग के नाम का उल्लेख है।

+0

धन्यवाद, दोस्त। इस स्थिति पर समाधान के लिए घंटों के शिकार के बाद, @XmlRootElement ने समस्या को ठीक किया। मैं सोच रहा हूं कि यह व्यवहार इस तरह क्यों होना चाहिए। क्या यह वास्तव में अनौपचारिक प्रक्रिया के तहत इस एनोटेशन को मजबूर करने के लिए समझ में आता है? – Miere

+0

@Miere अब मैं इसके बारे में सोचता हूं। Webservices की दुनिया में, आरपीसी-शैली स्वचालित रूप से एक रैपिंग नोड प्रदान करती है, जहां दस्तावेज़-शैली वेबसाइसेस को स्पष्ट घोषणा की आवश्यकता होती है। तो, यह हमेशा चर्चा का क्षेत्र रहा है। – bvdb

+0

मैं एक्सएसडी से पीओजेओ उत्पन्न करने के लिए jdk1.6.0_45 में xjc का उपयोग कर रहा हूं। Xjc द्वारा जेनरेट की गई रूट POJO में @XmlRootElement (name = "रूटनाम") गुम है।इस लाइन को मैन्युअल रूप से शीर्ष पर जोड़ें और अनारशल सफल है। – oraclesoon

1

इस त्रुटि के कारणों में से एक jdk से jaxb कार्यान्वयन का उपयोग है। मुझे यकीन नहीं है कि ऐसी समस्या क्यों सरल सरल एक्सएमएल पार्सिंग स्थितियों में दिखाई दे सकती है। आप किसी सार्वजनिक Maven रिपोजिटरी से JAXB पुस्तकालय के नवीनतम संस्करण का उपयोग कर सकते हैं:

http://mvnrepository.com

<dependency> 
    <groupId>javax.xml.bind</groupId> 
    <artifactId>jaxb-api</artifactId> 
    <version>2.2.12</version> 
</dependency> 
संबंधित मुद्दे