异常使用jaxb解组xml文件

时间:2015-06-05 12:08:38

标签: java xml xsd jaxb

我正在尝试解组一个xml文件,但是我得到了一个绑定异常,我是新手使用xml和schemas与java,但从我可以看出,它看起来像命名空间的问题将xml绑定到我正在使用的xsd模式。

我一直在尝试解决这个问题,而我似乎无法找到问题。提前谢谢。

这是我的例外:

javax.xml.bind.UnmarshalException: unexpected element (URI:"", local:"programacioAula"). Expected elements are <{http://www.xtec.cat/ProgramacioAula}programacioAula>

这是xml上的名称空间声明:

<programacioAula
    xmlns:tns="http://www.xtec.cat/ProgramacioAula"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.xtec.cat/ProgramacioAula ProgramacioAula.xsd ">

这是xsd:

上的名称空间声明
<schema
    xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.xtec.cat/ProgramacioAula"
    xmlns:tns="http://www.xtec.cat/ProgramacioAula"
    elementFormDefault="unqualified"

    attributeFormDefault="unqualified">

    <element name="programacioAula" type="tns:ProgramacioAula"></element>

这是带有XML注释的pojo:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b52-fcs 
// 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.06.04 at 10:32:59 PM CEST 
//


package dani.java.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for ProgramacioAula complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="ProgramacioAula">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="resum" type="{http://www.xtec.cat/ProgramacioAula}Resum"/>
 *         &lt;element name="unitatsFormatives" type="{http://www.xtec.cat/ProgramacioAula}UnitatsFormatives"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "programacioAula",namespace="http://www.xtec.cat/ProgramacioAula", propOrder = {
    "resum",
    "unitatsFormatives"
})
@XmlRootElement(name = "programacioAula")
public class ProgramacioAula {

    @XmlElement(required = true)
    protected Resum resum;
    @XmlElement(required = true)
    protected UnitatsFormatives unitatsFormatives;

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

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

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

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

}

这是我试图解组文件的方法:

    public static ProgramacioAula readXML(File file) {
        ProgramacioAula programacioAula = null;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(dani.java.model.ObjectFactory.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            programacioAula = (ProgramacioAula) jaxbUnmarshaller.unmarshal(file);

        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return programacioAula;
    }

1 个答案:

答案 0 :(得分:0)

您的XML位于默认命名空间中:

<programacioAula
    xmlns:tns="http://www.xtec.cat/ProgramacioAula"

我怀疑你可能需要

<tns:programacioAula
    xmlns:tns="http://www.xtec.cat/ProgramacioAula"

<programacioAula
    xmlns="http://www.xtec.cat/ProgramacioAula"