0
我想解开一个xml文件,但我得到一个绑定异常,我是新来使用XML和模式与Java,但从我可以告诉,它看起来像一个问题当它尝试将XML绑定到我正在使用的xsd模式时使用命名空间。异常unmarshalling xml文件使用jaxb
我一直在努力工作,我似乎无法找到问题。提前致谢。
这是例外即时得到:
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>
* <complexType name="ProgramacioAula">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="resum" type="{http://www.xtec.cat/ProgramacioAula}Resum"/>
* <element name="unitatsFormatives" type="{http://www.xtec.cat/ProgramacioAula}UnitatsFormatives"/>
* </sequence>
* </restriction>
* </complexContent>
* </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;
}
第二种解决方案解决了这个问题,谢谢。现在孩子们的元素都是空的,所以要战斗一下。 ; d – Nagarz