javax.xml.bind.UnmarshalException:意外元素(uri:"",local:"")。预期的要素是

时间:2015-01-13 09:55:18

标签: 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;
            }

        }

    }

}

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

我收到了这个错误:

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

请帮忙。

6 个答案:

答案 0 :(得分:19)

以下是一些应该有用的信息:

<强> 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属性后,将删除命名空间限定。以下文件与上述两份文件不同。

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

答案 1 :(得分:8)

更改

@XmlRootElement(name = "lookupInstances")

@XmlRootElement( namespace = "http://www.pqr.com/awd/rest/v1", name = "lookupInstances")

答案 2 :(得分:2)

似乎JAXB创建的类不包含名称空间信息:

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

我怀疑它会导致此错误,因为它不需要命名空间:

  

javax.xml.bind.UnmarshalException:意外元素(uri:“http://www.pqr.com/awd/rest/v1”,local:“lookupInstances”)。 预期元素为&lt; {} lookupInstances&gt;

我不确定为什么生成的类缺少这些信息,但我会先手动添加它来验证这是问题所在:

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

答案 3 :(得分:2)

我已经通过更改xsd更改了jaxb类,现在它正在运行。

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

        }

    }

}

答案 4 :(得分:0)

首先,您必须检查输入XML文件。 确保输入XML应与XSD文件具有相同的根元素(您已用于创建JAXb类) 所以请尝试下面的XML,它会工作正常:)

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

答案 5 :(得分: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; 

这部分对于JAXB生成的类非常重要,尤其是JAXB不会自动添加elementFormDefault = XmlNsForm.QUALIFIED

谢谢Blaise,你的回答帮助了我。