简体   繁体   中英

exception unmarshalling xml file using jaxb

I'm trying to unmarshall an xml file, but I get a binding exception, I'm new to using xml and schemas with java, but from what I can tell, it looks like a problem with the namespaces when it tries to bind the xml to an xsd schema I'm using.

I've been trying to work this out and I can't seem to find the issue. Thanks in advance.

This is the exception im getting:

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

This is the namespaces declaration on the 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 ">

This is the namespaces declaration on the 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>

This is the pojo with the XML annotations:

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

}

This is the method where I'm trying to unmarshall the file:

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

Your XML is in the default namespace:

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

I suspect you might need either

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM