2015-01-13 7 views
16

आप इस साइट में इस तरह के कई प्रश्न पा सकते हैं लेकिन किसी ने भी मेरी समस्या का समाधान नहीं किया है।javax.xml.bind.UnmarshalException: अप्रत्याशित तत्व (यूरी: "", स्थानीय: "")। अपेक्षित तत्व

यह मेरा एक्सएमएल है:

<?xml version='1.0' encoding='UTF-8' standalone='yes'?> 
<lookupInstances xmlns='http://www.pqr.com/awd/rest/v1' name='LKIMGR'> 
<lookupParameters> 
<lookupParameter name='businessArea'>PQAA</lookupParameter> 
<lookupParameter name='MEMBERNUMBER'>ANTHONY1900</lookupParameter> 
</lookupParameters> 
</lookupInstances> 

मैं XJC संकलक और बनाई गई फ़ाइल का उपयोग कर JAXB वर्ग बनाया है प्रकार है:

// 
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2015.01.13 at 12:15:41 PM IST 
// 


package com.dsths.ga.awd.main; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 


/** 
* <p>Java class for anonymous complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="lookupParameters"> 
*   &lt;complexType> 
*    &lt;complexContent> 
*    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*     &lt;sequence> 
*     &lt;element name="lookupParameter"> 
*      &lt;complexType> 
*      &lt;complexContent> 
*       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*       &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*       &lt;/restriction> 
*      &lt;/complexContent> 
*      &lt;/complexType> 
*     &lt;/element> 
*     &lt;/sequence> 
*    &lt;/restriction> 
*    &lt;/complexContent> 
*   &lt;/complexType> 
*   &lt;/element> 
*  &lt;/sequence> 
*  &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "lookupParameters" 
}) 
@XmlRootElement(name = "lookupInstances") 
public class LookupInstances { 

    @XmlElement(required = true) 
    protected LookupInstances.LookupParameters lookupParameters; 
    @XmlAttribute 
    protected String name; 

    /** 
    * Gets the value of the lookupParameters property. 
    * 
    * @return 
    *  possible object is 
    *  {@link LookupInstances.LookupParameters } 
    *  
    */ 
    public LookupInstances.LookupParameters getLookupParameters() { 
     return lookupParameters; 
    } 

    /** 
    * Sets the value of the lookupParameters property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link LookupInstances.LookupParameters } 
    *  
    */ 
    public void setLookupParameters(LookupInstances.LookupParameters value) { 
     this.lookupParameters = value; 
    } 

    /** 
    * Gets the value of the name property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * Sets the value of the name property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setName(String value) { 
     this.name = value; 
    } 


    /** 
    * <p>Java class for anonymous complex type. 
    * 
    * <p>The following schema fragment specifies the expected content contained within this class. 
    * 
    * <pre> 
    * &lt;complexType> 
    * &lt;complexContent> 
    *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *  &lt;sequence> 
    *   &lt;element name="lookupParameter"> 
    *   &lt;complexType> 
    *    &lt;complexContent> 
    *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *     &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
    *    &lt;/restriction> 
    *    &lt;/complexContent> 
    *   &lt;/complexType> 
    *   &lt;/element> 
    *  &lt;/sequence> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "", propOrder = { 
     "lookupParameter" 
    }) 
    public static class LookupParameters { 

     @XmlElement(required = true) 
     protected LookupInstances.LookupParameters.LookupParameter lookupParameter; 

     /** 
     * Gets the value of the lookupParameter property. 
     * 
     * @return 
     *  possible object is 
     *  {@link LookupInstances.LookupParameters.LookupParameter } 
     *  
     */ 
     public LookupInstances.LookupParameters.LookupParameter getLookupParameter() { 
      return lookupParameter; 
     } 

     /** 
     * Sets the value of the lookupParameter property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link LookupInstances.LookupParameters.LookupParameter } 
     *  
     */ 
     public void setLookupParameter(LookupInstances.LookupParameters.LookupParameter value) { 
      this.lookupParameter = value; 
     } 


     /** 
     * <p>Java class for anonymous complex type. 
     * 
     * <p>The following schema fragment specifies the expected content contained within this class. 
     * 
     * <pre> 
     * &lt;complexType> 
     * &lt;complexContent> 
     *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
     *  &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
     *  &lt;/restriction> 
     * &lt;/complexContent> 
     * &lt;/complexType> 
     * </pre> 
     * 
     * 
     */ 
     @XmlAccessorType(XmlAccessType.FIELD) 
     @XmlType(name = "") 
     public static class LookupParameter { 

      @XmlAttribute 
      protected String name; 

      /** 
      * Gets the value of the name property. 
      * 
      * @return 
      *  possible object is 
      *  {@link String } 
      *  
      */ 
      public String getName() { 
       return name; 
      } 

      /** 
      * Sets the value of the name property. 
      * 
      * @param value 
      *  allowed object is 
      *  {@link String } 
      *  
      */ 
      public void setName(String value) { 
       this.name = value; 
      } 

     } 

    } 

} 

unmarshal कोड:

public LookupInstances unmarshallXmlRequest(String xmlReq) 
    { 
     LookupInstances instances = null; 
      try { 
      JAXBContext jc = JAXBContext.newInstance(LookupInstances.class); 
       Unmarshaller u = jc.createUnmarshaller(); 
       StringBuffer xmlStr = new StringBuffer(xmlReq); 
       StringReader strReader = new StringReader(xmlStr.toString()); 
       StreamSource strSource = new StreamSource(strReader); 
       Object o = u.unmarshal(strSource); 
       instances = (LookupInstances)o; 
     } catch (JAXBException e) { 

      e.printStackTrace(); 
     } 
     return instances; 
    } 

मुझे यह त्रुटि मिली:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances> 

कृपया मदद करें।

उत्तर

1

ऐसा लगता है कि वर्ग JAXB द्वारा बनाई नाम स्थान जानकारी शामिल नहीं है:

@XmlType(name = "", propOrder = { 
    "lookupParameters" 
}) 

मुझे लगता है कि यह इस त्रुटि का कारण बनता है, क्योंकि यह कोई नाम स्थान उम्मीद:

javax। xml.bind.UnmarshalException: अप्रत्याशित तत्व (यूरी: "http://www.pqr.com/awd/rest/v1", स्थानीय: "लुकअप इंस्टेंस")। अपेक्षित तत्वों < {} lookupInstances हैं>

मुझे यकीन है कि क्यों उत्पन्न वर्ग इस जानकारी का अभाव है नहीं कर रहा हूँ, लेकिन मैं यह है कि सत्यापित करने के लिए मैन्युअल रूप से इसे जोड़ने के साथ शुरू होगा समस्या है:

@XmlType(name = "", namespace= "http://www.pqr.com/awd/rest/v1", propOrder = { 
    "lookupParameters" 
}) 
+0

त्वरित उत्तर @Magnilex के लिए धन्यवाद। जैसा कि आपने सुझाव दिया था कि मैंने जावा क्लास घोषणा में नामस्थान जोड़ा और त्रुटि गायब हो गई। लेकिन जावा वर्ग को अनारक्षित करते समय, इसमें सभी तत्व शामिल नहीं होते हैं या ठीक से शुरू नहीं होते हैं। मैंने जैक्सबी क्लास को बदल दिया है जिसमें अब कैप्चर करने के लिए सूची शामिल है ** लुकअप पैरामीटर ** और यह अब काम कर रहा है। – Rana

+0

@ राणा ग्लेड मदद करने के लिए, और अच्छा है कि मेरे जवाब ने आपको सही दिशा में धक्का दिया। – Magnilex

2

मैंने xsd को बदलकर जैक्सबी क्लास बदल दिया है और यह अब काम कर रहा है।

// 
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2015.01.13 at 03:49:52 PM IST 
// 


package com.dsths.ga.awd.main; 

import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 


/** 
* <p>Java class for anonymous complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="lookupParameters"> 
*   &lt;complexType> 
*    &lt;complexContent> 
*    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*     &lt;sequence> 
*     &lt;element name="lookupParameter" maxOccurs="unbounded" minOccurs="0"> 
*      &lt;complexType> 
*      &lt;simpleContent> 
*       &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string"> 
*       &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*       &lt;/extension> 
*      &lt;/simpleContent> 
*      &lt;/complexType> 
*     &lt;/element> 
*     &lt;/sequence> 
*    &lt;/restriction> 
*    &lt;/complexContent> 
*   &lt;/complexType> 
*   &lt;/element> 
*  &lt;/sequence> 
*  &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "lookupParameters" 
}) 
@XmlRootElement(name = "lookupInstances") 
public class LookupInstances { 

    @XmlElement(required = true) 
    protected LookupInstances.LookupParameters lookupParameters; 
    @XmlAttribute(name = "name") 
    protected String name; 

    /** 
    * Gets the value of the lookupParameters property. 
    * 
    * @return 
    *  possible object is 
    *  {@link LookupInstances.LookupParameters } 
    *  
    */ 
    public LookupInstances.LookupParameters getLookupParameters() { 
     return lookupParameters; 
    } 

    /** 
    * Sets the value of the lookupParameters property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link LookupInstances.LookupParameters } 
    *  
    */ 
    public void setLookupParameters(LookupInstances.LookupParameters value) { 
     this.lookupParameters = value; 
    } 

    /** 
    * Gets the value of the name property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * Sets the value of the name property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setName(String value) { 
     this.name = value; 
    } 


    /** 
    * <p>Java class for anonymous complex type. 
    * 
    * <p>The following schema fragment specifies the expected content contained within this class. 
    * 
    * <pre> 
    * &lt;complexType> 
    * &lt;complexContent> 
    *  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *  &lt;sequence> 
    *   &lt;element name="lookupParameter" maxOccurs="unbounded" minOccurs="0"> 
    *   &lt;complexType> 
    *    &lt;simpleContent> 
    *    &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string"> 
    *     &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
    *    &lt;/extension> 
    *    &lt;/simpleContent> 
    *   &lt;/complexType> 
    *   &lt;/element> 
    *  &lt;/sequence> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "", propOrder = { 
     "lookupParameter" 
    }) 
    public static class LookupParameters { 

     protected List<LookupInstances.LookupParameters.LookupParameter> lookupParameter; 

     /** 
     * Gets the value of the lookupParameter property. 
     * 
     * <p> 
     * This accessor method returns a reference to the live list, 
     * not a snapshot. Therefore any modification you make to the 
     * returned list will be present inside the JAXB object. 
     * This is why there is not a <CODE>set</CODE> method for the lookupParameter property. 
     * 
     * <p> 
     * For example, to add a new item, do as follows: 
     * <pre> 
     * getLookupParameter().add(newItem); 
     * </pre> 
     * 
     * 
     * <p> 
     * Objects of the following type(s) are allowed in the list 
     * {@link LookupInstances.LookupParameters.LookupParameter } 
     * 
     * 
     */ 
     public List<LookupInstances.LookupParameters.LookupParameter> getLookupParameter() { 
      if (lookupParameter == null) { 
       lookupParameter = new ArrayList<LookupInstances.LookupParameters.LookupParameter>(); 
      } 
      return this.lookupParameter; 
     } 


     /** 
     * <p>Java class for anonymous complex type. 
     * 
     * <p>The following schema fragment specifies the expected content contained within this class. 
     * 
     * <pre> 
     * &lt;complexType> 
     * &lt;simpleContent> 
     *  &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string"> 
     *  &lt;attribute name="name" type="{http://www.w3.org/2001/XMLSchema}string" /> 
     *  &lt;/extension> 
     * &lt;/simpleContent> 
     * &lt;/complexType> 
     * </pre> 
     * 
     * 
     */ 
     @XmlAccessorType(XmlAccessType.FIELD) 
     @XmlType(name = "", propOrder = { 
      "value" 
     }) 
     public static class LookupParameter { 

      @XmlValue 
      protected String value; 
      @XmlAttribute(name = "name") 
      protected String name; 

      /** 
      * Gets the value of the value property. 
      * 
      * @return 
      *  possible object is 
      *  {@link String } 
      *  
      */ 
      public String getValue() { 
       return value; 
      } 

      /** 
      * Sets the value of the value property. 
      * 
      * @param value 
      *  allowed object is 
      *  {@link String } 
      *  
      */ 
      public void setValue(String value) { 
       this.value = value; 
      } 

      /** 
      * Gets the value of the name property. 
      * 
      * @return 
      *  possible object is 
      *  {@link String } 
      *  
      */ 
      public String getName() { 
       return name; 
      } 

      /** 
      * Sets the value of the name property. 
      * 
      * @param value 
      *  allowed object is 
      *  {@link String } 
      *  
      */ 
      public void setName(String value) { 
       this.name = value; 
      } 

     } 

    } 

} 
17

नीचे कुछ जानकारी है कि मदद करनी चाहिए है:

एक्सएमएल

नीचे अपनी एक्सएमएल के एक हिस्से है। ध्यान देने योग्य एक बात यह है कि xmlns विशेषता है। यह एक विशेष विशेषता है और एक्सएमएल दस्तावेज़ के भीतर नेमस्पेस योग्यता को संदर्भित करता है।

<abc:lookupInstances xmlns:abc='http://www.pqr.com/awd/rest/v1' name='LKIMGR'> 
    <abc:lookupParameters/> 
</abc:lookupInstances> 

जब आप xmlns नाम स्थान योग्यता विशेषता को दूर हटा दिया जाता है:

<lookupInstances xmlns='http://www.pqr.com/awd/rest/v1' name='LKIMGR'> 
    <lookupParameters/> 
</lookupInstances> 

नीचे एक ही नाम स्थान योग्यता के साथ एक्सएमएल का एक और संस्करण है। नीचे दिया गया दस्तावेज़ उपरोक्त दो के बराबर नहीं है।

<lookupInstances name='LKIMGR'> 
    <lookupParameters/> 
</lookupInstances> 

आपका त्रुटि

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.pqr.com/awd/rest/v1", local:"lookupInstances"). Expected elements are <{}lookupInstances> 

यह त्रुटि इंगित करता है कि आप नाम स्थान योग्यता सही ढंग से मैप नहीं है।

JAXB में Namesapce योग्यता का मिलान JAXB में

नामस्थान योग्यता पैकेज स्तर @XmlSchema एनोटेशन का उपयोग किया जाता है। पैकेज स्तर एनोटेशन package-info.java में जाते हैं। नीचे इस वर्ग के लिए पूरा स्रोत है। यदि आपके पास पहले से package-info.java स्रोत फ़ाइल है, तो सुनिश्चित करें कि इसे आपके शेष वर्गों के साथ संकलित और पैक किया जा रहा है।

@XmlSchema( 
    namespace = "http://www.pqr.com/awd/rest/v1", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package your_package; 

import javax.xml.bind.annotation.XmlNsForm; 
import javax.xml.bind.annotation.XmlSchema; 

XML स्कीमा

आप एक XML स्कीमा से अपने मॉडल पैदा कर रहे हैं, तो यकीन है कि यह नाम स्थान योग्यता ठीक से वहाँ परिभाषित किया गया है। यह इस तरह दिखेगा:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.pqr.com/awd/rest/v1" 
    xmlns="http://www.pqr.com/awd/rest/v1" 
    elementFormDefault="qualified"> 
</xs:schema> 
+1

इससे बहुत मदद मिली। मैं सोच रहा था कि ऐप जावा क्लास में नेमस्पेस योग्यता का उल्लेख किए बिना कैसे काम कर रहा है, लेकिन जैसा कि आपने समझाया था कि यह पैकेज-info.java था जिसमें वह जानकारी थी। मेरे पहले प्रयास में मुझे एक्सएसडी फाइल गलत मिली जिसने मुझे गलत जैक्सबी कक्षा में खींच लिया। बहुत बहुत धन्यवाद। – Rana

+0

क्या गुण फ़ाइल से xmlns URI को आपूर्ति करने का कोई तरीका है? मेरे लिए xmlns यूआरआई परीक्षण और उत्पादन वातावरण – faizi

+0

@XmlSchema (नेमस्पेस = "http://www.pqr.com/awd/rest/v1", elementFormDefault = XmlNsForm.QUALIFIED) के लिए गतिशील रूप से बदलने की आवश्यकता है your_package पैकेज; आयात javax.xml.bind.annotation.XmlNsForm; आयात javax.xml.bind.annotation.XmlSchema; यह हिस्सा जेएक्सबी जेनरेटेड कक्षाओं के लिए बहुत महत्वपूर्ण है, विशेष रूप से 'elementFormDefault = XmlNsForm.QUALIFIED' स्वचालित रूप से JAXB द्वारा नहीं जोड़ा जाएगा। धन्यवाद ब्लेज़, आपके उत्तर ने मेरी मदद की। – ClickBright

0

सबसे पहले आपको इनपुट XML फ़ाइल जांचनी होगी। यकीन है कि इनपुट XML अपने XSD फ़ाइल के रूप में एक ही मूल तत्व (जो आप JAXB कक्षाएं बनाने के लिए इस्तेमाल किया है) तो एक्सएमएल नीचे प्रयास करें होना चाहिए बनाओ, यह ठीक काम करेंगे :)

<?xml version='1.0' encoding='UTF-8' standalone='yes'?> 
<lookupInstances name='LKIMGR'> 
<lookupParameters> 
<lookupParameter name='businessArea'>PQAA</lookupParameter> 
<lookupParameter name='MEMBERNUMBER'>ANTHONY1900</lookupParameter> 
</lookupParameters> 
</lookupInstances> 
6

बदलें

@XmlRootElement(name = "lookupInstances") 

@XmlRootElement(namespace = "http://www.pqr.com/awd/rest/v1", name = "lookupInstances") 
0
@XmlSchema(namespace = "http://www.pqr.com/awd/rest/v1", 
elementFormDefault = XmlNsForm.QUALIFIED) package your_package; 

     import javax.xml.bind.annotation.XmlNsForm; 
     import javax.xml.bind.annotation.XmlSchema; 

के इस भाग लिया है जेएक्सबी जेनरेटेड क्लास के लिए महत्वपूर्ण है, खासकर elementFormDefault = XmlNsForm.QUALIFIED स्वचालित रूप से जेएक्सबी द्वारा नहीं जोड़ा जाएगा।

धन्यवाद ब्लेज़, आपके उत्तर ने मेरी मदद की।

संबंधित मुद्दे