【问题标题】:How can i unmarshal with JAXB我如何使用 JAXB 解组
【发布时间】:2012-03-25 18:05:21
【问题描述】:

我在使用 JAXB 在 Web 服务上解组 .xml 时遇到了一些问题。

这是从某个客户端发送到 Web 服务的 .xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<PERSON xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file:/D:/MyWorkSpace/JAVA%20WEB%20Services%20DEVELOPER/XML%20Workspace/Persons.xsd">
    <NAME>Michael</NAME>
    <AGE>12</AGE>
    <ADRESS>
        <STREET>Somewhere in Spain</STREET>
        <ZIP>47015</ZIP>
    </ADRESS>
    <HOBY indoorHoby="true"/>
</PERSON>

这是进行解组的方法

    @POST
    @Path("/XMLArrivalBeacon")
    @Consumes(MediaType.APPLICATION_XML)
    public Response methodI(String content) {
        System.out.print(content);
        try {
            //Unmarshaling            
            JAXBContext context = JAXBContext.newInstance(Person.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            //Unmarshal the String
            Person person = (Person) unmarshaller.unmarshal(new StreamSource(new StringReader(content)));
            //Checking that the values were recieved ok
            System.out.print(person.getName());
            return Response.ok("XML recieved from client!!!").build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

这里是使用 JAXB 注释的 person 类(从 Schema 中自动生成)

package bindedschemas;

import java.math.BigInteger;
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="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="age">
 *           &lt;simpleType>
 *             &lt;restriction base="{http://www.w3.org/2001/XMLSchema}integer">
 *               &lt;minInclusive value="18"/>
 *             &lt;/restriction>
 *           &lt;/simpleType>
 *         &lt;/element>
 *         &lt;element name="address">
 *           &lt;complexType>
 *             &lt;complexContent>
 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                 &lt;sequence>
 *                   &lt;element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                   &lt;element name="zip" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                 &lt;/sequence>
 *                 &lt;attribute name="country" type="{http://www.w3.org/2001/XMLSchema}string" />
 *               &lt;/restriction>
 *             &lt;/complexContent>
 *           &lt;/complexType>
 *         &lt;/element>
 *         &lt;element name="hobie">
 *           &lt;simpleType>
 *             &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
 *             &lt;/restriction>
 *           &lt;/simpleType>
 *         &lt;/element>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "name",
    "age",
    "address",
    "hobie"
})
@XmlRootElement(name = "Person")
public class Person {

    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected BigInteger age;
    @XmlElement(required = true)
    protected Person.Address address;
    @XmlElement(required = true)
    protected String hobie;

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

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

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

    /**
     * Gets the value of the address property.
     * 
     * @return
     *     possible object is
     *     {@link Person.Address }
     *     
     */
    public Person.Address getAddress() {
        return address;
    }

    /**
     * Sets the value of the address property.
     * 
     * @param value
     *     allowed object is
     *     {@link Person.Address }
     *     
     */
    public void setAddress(Person.Address value) {
        this.address = value;
    }

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

    /**
     * Sets the value of the hobie property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setHobie(String value) {
        this.hobie = 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="street" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         &lt;element name="zip" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *       &lt;/sequence>
     *       &lt;attribute name="country" type="{http://www.w3.org/2001/XMLSchema}string" />
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "street",
        "zip"
    })
    public static class Address {

        @XmlElement(required = true)
        protected String street;
        @XmlElement(required = true)
        protected String zip;
        @XmlAttribute(name = "country")
        protected String country;

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

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

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

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

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

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

    }

}

这是我得到的例外

严重:javax.xml.bind.UnmarshalException:意外元素(uri:“”, 本地:“人”)。预期元素是 在 com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662) 在 com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258) 在 com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)

知道如何解决吗?

【问题讨论】:

    标签: java web-services jakarta-ee rest jaxb


    【解决方案1】:

    您的 xml 与您的架构不匹配。 xml 元素名称​​区分大小写。 “PERSON”和“Person”不是同一个元素。

    【讨论】:

    • 是的,就是这样。愚蠢的错误:) 谢谢
    猜你喜欢
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 2012-10-01
    • 1970-01-01
    • 2013-01-17
    相关资源
    最近更新 更多