javax.xml.bind.UnmarshalException : unexpected element (uri :"", 本地 :"")。预期的元素是

标签 java xml jaxb xjc

你可以在这个网站上找到很多这样的问题,但没有一个能解决我的问题。

这是我的 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>

我使用 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;
            }

        }

    }

}

解码代码:

    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>

请帮忙。

最佳答案

以下是一些应该有所帮助的信息:

XML

以下是您的 XML 的一部分。需要注意的一点是 xmlns 属性。这是一个特殊属性,指的是 XML 文档中的命名空间限定。

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

以下是具有相同命名空间限定的 XML 的另一个版本:

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

当您删除 xmlns 属性时, namespace 限定被删除。下面的文档不等同于上面的两个。

<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 中映射命名空间限定

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 Schema 生成模型,请确保在此处正确定义此命名空间限定。它看起来像:

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

关于javax.xml.bind.UnmarshalException : unexpected element (uri :"", 本地 :"")。预期的元素是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27919323/

相关文章:

java - 使用java将html转换为xml

java - Hibernate在持久化到数据库之前通过id获取保存的对象

java - 匹配字符串第一次出现的正则表达式是匹配最后一次出现的

java - 如何使用 Hibernate\HQL 返回 Map<Key, Value>

javascript - 播放 HTML5 视频时无法加载 XML 文件

c# - SandcaSTLe 如何知道 vs-project 成员的可见性

java - 为原语自定义 jaxb 绑定(bind)

java - JAXB:解码异构数组

java - 使用日历设置日期的问题