2015-01-13 44 views
16

Puoi trovare un sacco di domande come questa in questo sito ma nessuna ha risolto il mio problema.javax.xml.bind.UnmarshalException: elemento inatteso (uri: "", locale: ""). Gli elementi previsti sono

Questo è il mio XML:

<?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> 

Ho creato la classe JAXB usando il compilatore XJC e il file creato è come:

// 
// 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; 
      } 

     } 

    } 

} 

Codice 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; 
    } 

Ho ricevuto questo errore:

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

Per favore aiuto.

risposta

1

Sembra che la classe creato da JAXB non contiene le informazioni namespace:

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

Ho il sospetto che causa questo errore, semplicemente perché non si aspetta namespace:

javax. xml.bind.UnmarshalException: elemento inatteso (uri: "http://www.pqr.com/awd/rest/v1", locale: "lookupInstances"). elementi attesi sono < {} lookupInstances>

io non sono sicuro perché la classe generata manca di questa informazione, ma vorrei iniziare con l'aggiunta manualmente per verificare che questo è il problema:

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

Grazie per una rapida risposta @Magnilex. Come hai suggerito, ho aggiunto lo spazio dei nomi nella dichiarazione della classe java e l'errore è scomparso. Ma mentre unmarshaling la classe java, non contiene tutti gli elementi o non correttamente inizializzato. Ho cambiato la classe jaxb che ora contiene List per acquisire ** lookupParameter ** e funziona ora. – Rana

+0

@Rana Felice di aiutare, e buono che la mia risposta ti ha spinto nella giusta direzione. – Magnilex

2

Ho cambiato la classe jaxb cambiando xsd e ora funziona.

// 
// 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

seguito alcune informazioni che dovrebbe aiutare:

XML

seguito è una parte del vostro XML. Una cosa da notare è l'attributo xmlns. Questo è un attributo speciale e fa riferimento alla qualifica dello spazio dei nomi all'interno del documento XML.

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

Di seguito è un'altra versione del XML con la stessa qualifica namespace:

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

Quando si rimuove il xmlns attribuiscono la qualifica di namespace viene rimosso. Il documento sottostante non è equivalente ai due sopra.

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

tuo errore

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

Questo errore indica che non è stato mappato in modo corretto la qualificazione dello spazio dei nomi.

Mappatura del Namesapce qualificazione in JAXB

Namespace qualifica in JAXB è fatto con il livello del pacchetto @XmlSchema annotazione. Le annotazioni a livello di pacchetto vanno a package-info.java. Di seguito è la fonte completa per questa classe. Se hai già un file sorgente package-info.java assicurati che sia compilato e pacchettizzato con il resto delle tue classi.

@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 Schema

Se si sta generando il vostro modello da uno schema XML assicurarsi questa qualifica spazio dei nomi sia correttamente definire lì. Sembrerà:

<?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

Questo ha aiutato molto Blaise. Mi chiedevo come funzionasse l'app senza menzionare la qualifica dello spazio dei nomi in classe Java, ma come hai spiegato era package-info.java che aveva queste informazioni. Nel mio primo tentativo ho sbagliato il file xsd che mi ha trascinato in una classe Jaxb errata. Molte grazie. – Rana

+0

Esiste un modo per fornire l'URI xmlns da un file delle proprietà? Per me l'URI xmlns deve cambiare dinamicamente per gli ambienti di test e produzione – faizi

+0

@XmlSchema (namespace = "http://www.pqr.com/awd/rest/v1", elementFormDefault = XmlNsForm.QUALIFIED) pacchetto your_package; import javax.xml.bind.annotation.XmlNsForm; import javax.xml.bind.annotation.XmlSchema; Questa parte è molto importante per le classi generate da JAXB, in particolare 'elementFormDefault = XmlNsForm.QUALIFIED' non verrà automaticamente aggiunto da JAXB. Grazie Blaise, la tua risposta mi ha aiutato. – ClickBright

0

Prima di tutto è necessario controllare il file XML di input. Assicurarsi che l'XML di input dovrebbe avere lo stesso elemento radice del file XSD (che è stato utilizzato per creare le classi JAXB) Quindi riprova di seguito XML, E funziona bene :)

<?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

Change

@XmlRootElement(name = "lookupInstances") 

a

@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; 

: questo componente e ve RY importante per le classi generate da JAXB, in particolare lo elementFormDefault = XmlNsForm.QUALIFIED non verrà automaticamente aggiunto da JAXB.

Grazie Blaise, la tua risposta mi ha aiutato.