2015-01-13 21 views
16

Bạn có thể tìm thấy rất nhiều câu hỏi như thế này trong trang này nhưng không có câu nào giải quyết được vấn đề của tôi.javax.xml.bind.UnmarshalException: yếu tố không mong muốn (uri: "", local: ""). Các yếu tố mong đợi là

Đây là XML của tôi:

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

Tôi đã tạo ra lớp JAXB sử dụng trình biên dịch XJC và tập tin được tạo ra là như sau:

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

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

Tôi gặp lỗi này:

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

Vui lòng trợ giúp.

Trả lời

1

Dường như lớp được tạo ra bởi JAXB không chứa các thông tin không gian tên:

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

Tôi nghi ngờ rằng nó gây ra lỗi này, đơn giản chỉ vì họ hy vọng không gian tên:

javax. xml.bind.UnmarshalException: phần tử không mong muốn (uri: "http://www.pqr.com/awd/rest/v1", local: "lookupInstances"). yếu tố dự kiến ​​là < {} lookupInstances>

Tôi không chắc chắn lý do tại sao các lớp được tạo ra thiếu thông tin này, nhưng tôi sẽ bắt đầu với việc thêm nó bằng tay để xác minh rằng đây là vấn đề:

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

Cảm ơn bạn đã trả lời nhanh @Magnilex. Như bạn đã đề xuất, tôi đã thêm không gian tên trong khai báo lớp java và lỗi biến mất. Nhưng trong khi unmarshaling lớp java, nó không chứa tất cả các phần tử hoặc không được khởi tạo đúng cách. Tôi đã thay đổi lớp jaxb mà bây giờ chứa List để capture ** lookupParameter ** và nó hiện đang hoạt động. – Rana

+0

@Rana Vui mừng được giúp đỡ, và tốt là câu trả lời của tôi đã đẩy bạn đi đúng hướng. – Magnilex

2

Tôi đã thay đổi lớp jaxb bằng cách thay đổi xsd và nó đang làm việc ngay bây giờ.

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

Dưới đây là một số thông tin rằng sẽ giúp:

XML

Dưới đây là một phần của XML của bạn. Một điều cần lưu ý là thuộc tính xmlns. Đây là thuộc tính đặc biệt và đề cập đến trình độ không gian tên trong tài liệu XML.

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

Dưới đây là một phiên bản của XML với trình độ không gian tên giống nhau:

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

Khi bạn loại bỏ thuộc tính xmlns trình độ không gian tên được lấy ra. Tài liệu dưới đây không tương đương với hai bên trên.

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

Lỗi của bạn

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

Lỗi này chỉ ra rằng bạn đã không ánh xạ trình độ namespace một cách chính xác.

Lập bản đồ Namesapce Giấy chứng nhận trong JAXB

Namespace trình độ chuyên môn trong JAXB được thực hiện bằng cách sử dụng mức khoán @XmlSchema chú thích. Chú thích cấp gói đi theo số package-info.java. Dưới đây là nguồn hoàn chỉnh cho lớp này. Nếu bạn đã có tệp nguồn package-info.java, hãy đảm bảo tệp đang được biên dịch và đóng gói cùng với các lớp còn lại của bạn.

@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

Nếu bạn đang tạo ra mô hình của bạn từ một XML Schema đảm bảo trình độ chuyên môn không gian tên này đúng định nghĩa đó. Nó sẽ trông giống như:

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

Điều đó đã giúp rất nhiều Blaise. Tôi đã tự hỏi làm thế nào các ứng dụng đang làm việc mà không đề cập đến trình độ không gian tên trong lớp Java, nhưng khi bạn giải thích nó là package-info.java có thông tin đó. Trong lần thử đầu tiên của tôi, tôi đã nhận được tệp xsd sai đã kéo tôi vào một lớp Jaxb không chính xác. Cảm ơn rất nhiều. – Rana

+0

Có cách nào để cung cấp các xmlns URI từ một tập tin thuộc tính? Đối với tôi, xmlns URI cần thay đổi động cho môi trường thử nghiệm và sản xuất – faizi

+0

@XmlSchema (namespace = "http://www.pqr.com/awd/rest/v1", elementFormDefault = XmlNsForm.QUALIFIED) gói your_package; nhập javax.xml.bind.annotation.XmlNsForm; nhập javax.xml.bind.annotation.XmlSchema; Phần này rất quan trọng đối với các lớp được tạo bởi JAXB, đặc biệt là 'elementFormDefault = XmlNsForm.QUALIFIED' sẽ không tự động được JAXB thêm vào. Cảm ơn Blaise, câu trả lời của bạn đã giúp tôi. – ClickBright

0

Trước hết bạn phải kiểm tra tệp XML đầu vào. Hãy chắc chắn rằng XML đầu vào nên có cùng một nguyên tố gốc như tập tin XSD của bạn (mà bạn đã sử dụng để tạo ra các lớp JAXB) Vì vậy, hãy cố gắng bên dưới XML, Nó sẽ làm việc tốt :)

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

Thay đổi

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

phần này là đã ry quan trọng đối với các lớp được tạo JAXB, đặc biệt là elementFormDefault = XmlNsForm.QUALIFIED sẽ không tự động được JAXB thêm vào.

Cảm ơn Blaise, câu trả lời của bạn đã giúp tôi.

Các vấn đề liên quan