使用 jaxb 异常解组 xml 文件

exception unmarshalling xml file using jaxb

我正在尝试解组一个 xml 文件,但我遇到了一个绑定异常,我刚开始使用 xml 和带有 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;
    }

您的 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"