UnmarshalException:意外元素

时间:2015-07-29 16:01:54

标签: java xml xsd jaxb unmarshalling

我得到了这个例外:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"obj"). 

预期元素是< {{{}}} obj>

当我尝试解析XML时:

String msg = "<obj is=\"Message\">  
                <str  name=\"missionId\" val=\"25\"/>   
                <str name=\"agentId\" val=\"seif\"/>
                <str name=\"properties\"  val=\"latitude=2525;longitude=9571;report=undefined\"/>   
             </obj> "

解析代码是:

 public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Obj.class);

        String receivedMessage = "<obj is=\"Message\">  <str  name=\"missionId\" val=\"25\"/>   <str name=\"agentId\" val=\"seif\"/><str name=\"properties\"  val=\"latitude=2525;longitude=9571;report=undefined\"/>   </obj> ";
        StringReader reader = new StringReader(receivedMessage);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Obj msg = (Obj) unmarshaller.unmarshal(reader);
        System.out.println(msg.getIs());
    }

这是为Obj生成的JAXB类:

@XmlRootElement() 
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "obj", propOrder = {
    "objGroup"
})
public class Obj {

    @XmlElements({
        @XmlElement(name = "obj", type = Obj.class),
        @XmlElement(name = "bool", type = Bool.class),
        @XmlElement(name = "int", type = Int.class),
        @XmlElement(name = "real", type = Real.class),
        @XmlElement(name = "str", type = Str.class),
        @XmlElement(name = "enum", type = Enum.class),
        @XmlElement(name = "abstime", type = Abstime.class),
        @XmlElement(name = "reltime", type = Reltime.class),
        @XmlElement(name = "date", type = Date.class),
        @XmlElement(name = "time", type = Time.class),
        @XmlElement(name = "uri", type = Uri.class),
        @XmlElement(name = "list", type = org.bull.a2nets.MyInterwokringProxy.obix.List.class),
        @XmlElement(name = "ref", type = Ref.class),
        @XmlElement(name = "err", type = Err.class),
        @XmlElement(name = "op", type = Op.class),
        @XmlElement(name = "feed", type = Feed.class)
    })
    protected java.util.List<Object> objGroup;
    @XmlAttribute(name = "name")
    protected String name;
    @XmlAttribute(name = "href")
    protected String href;
    @XmlAttribute(name = "is")
    protected String is;
    @XmlAttribute(name = "null")
    protected Boolean _null;

    /**
     * Gets the value of the objGroup 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 objGroup property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getObjGroup().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Obj }
     * {@link Bool }
     * {@link Int }
     * {@link Real }
     * {@link Str }
     * {@link Enum }
     * {@link Abstime }
     * {@link Reltime }
     * {@link Date }
     * {@link Time }
     * {@link Uri }
     * {@link org.bull.a2nets.MyInterwokringProxy.obix.List }
     * {@link Ref }
     * {@link Err }
     * {@link Op }
     * {@link Feed }
     * 
     * 
     */
    public java.util.List<Object> getObjGroup() {
        if (objGroup == null) {
            objGroup = new ArrayList<Object>();
        }
        return this.objGroup;
    }

    /**
     * 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 href property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getHref() {
        return href;
    }

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

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

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

    /**
     * Gets the value of the null property.
     * 
     * @return
     *     possible object is
     *     {@link Boolean }
     *     
     */
    public Boolean isNull() {
        return _null;
    }

    /**
     * Sets the value of the null property.
     * 
     * @param value
     *     allowed object is
     *     {@link Boolean }
     *     
     */
    public void setNull(Boolean value) {
        this._null = value;
    }

}

我不知道究竟是什么错。有什么想法吗?

感谢。

0 个答案:

没有答案