详细报错如下:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"sean:person"). Expected elements are <{http://sean.com}person>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1048)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:229)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:112)
at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:95)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:312)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:292)
at com.sean.JAXBHelper.getObjFromDoc(JAXBHelper.java:22)
at com.sean.Test.main(Test.java:19)
期待的元素为person,其相应的命名空间为http://sean.com
获取到的元素为person,其相应的命名空间为空
XML Schema文件:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://sean.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:unsignedShort"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
通过Eclipse的JAXB插件,使用XML Schema生成的代码:
代码中一定要包含XmlRootElement标签才能使用JAXB进行正转、反转
package com.sean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"age"
})
@XmlRootElement(name = "person")
public class Person {
@XmlElement(required = true)
protected String name;
@XmlSchemaType(name = "unsignedShort")
protected int age;
@XmlAttribute(name = "id")
protected String id;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public int getAge() {
return age;
}
public void setAge(int value) {
this.age = value;
}
public String getId() {
return id;
}
public void setId(String value) {
this.id = value;
}
}
进行测试的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<sean:person xmlns:sean="http://sean.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sean:name>abc</sean:name>
<sean:age>11</sean:age>
</sean:person>
测试方法类:
问题的原因是下面代码中被注释掉的那一行
解析XML文件并构建其对应的Document对象时,默认忽略元素对应的命令空间,JAXB在进行反转时,无法从Document对象中找到元素对应的命令空间,就会包标题的错
package com.sean;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class Test {
public static void main(String[] args) throws Exception {
String path = Test.class.getResource("/person.xml").getFile();
File file = new File(path);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
Object obj = JAXBHelper.getObjFromDoc(doc, Person.class);
System.out.println(obj.toString());
}
}