JAXB2 unmarshal 报javax.xml.bind.UnmarshalException: unexpected element (uri:"", local...

dr2tr 2009-02-12 05:43:38
使用JAXB unmarshal时,在以下最后一行代码抛异常;
JAXBContext jc = JAXBContext
.newInstance("org.example.xmlschema");
Unmarshaller u = jc.createUnmarshaller();
DispatcherConfiguration dispatcherConfiguration = (DispatcherConfiguration) u
.unmarshal(new File(configFileName));

异常为:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"DispatcherConfiguration"). Expected elements are <{http://www.example.org/XMLSchema}DispatcherConfiguration>


Schema代码为:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.org/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/XMLSchema">



<xs:complexType name="DriverList">
<xs:sequence>
<xs:element name="Driver" type="tns:Driver" maxOccurs="unbounded" minOccurs="1"></xs:element>
</xs:sequence>
</xs:complexType>


<xs:simpleType name="Environment">
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>

<xs:complexType name="Report">
<xs:sequence>
<xs:element name="ReportType" type="xs:string"></xs:element>
<xs:element name="Location" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>

<xs:simpleType name="ReportType">
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>

<xs:simpleType name="Location">
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>

<xs:complexType name="DispatcherConfiguration">
<xs:sequence>
<xs:element name="DriverList" type="tns:DriverList"></xs:element>
<xs:element name="Environment" type="tns:Environment"></xs:element>
<xs:element name="Report" type="tns:Report"></xs:element>
</xs:sequence>
</xs:complexType>

<xs:element name="DispatcherConfiguration" type="tns:DispatcherConfiguration"></xs:element>

<xs:complexType name="Driver">
<xs:sequence>
<xs:element name="Configuration" type="tns:Configuration"></xs:element>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" />
</xs:complexType>

<xs:simpleType name="Configuration">
<xs:restriction base="xs:string"></xs:restriction>
</xs:simpleType>
</xs:schema>


生成的Java类:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b26-ea3
// 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: 2009.02.12 at 05:02:40 PM CST
//


package org.example.xmlschema;

import javax.xml.bind.annotation.AccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import org.example.xmlschema.DispatcherConfiguration;
import org.example.xmlschema.DriverList;
import org.example.xmlschema.Report;


/**
* <p>Java class for DispatcherConfiguration complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="DispatcherConfiguration">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="DriverList" type="{http://www.example.org/XMLSchema}DriverList"/>
* <element name="Environment" type="{http://www.example.org/XMLSchema}Environment"/>
* <element name="Report" type="{http://www.example.org/XMLSchema}Report"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(AccessType.FIELD)
@XmlType(name = "DispatcherConfiguration", propOrder = {
"driverList",
"environment",
"report"
})
public class DispatcherConfiguration {

@XmlElement(name = "DriverList")
protected DriverList driverList;
@XmlElement(name = "Environment")
protected String environment;
@XmlElement(name = "Report")
protected Report report;

/**
* Gets the value of the driverList property.
*
* @return
* possible object is
* {@link DriverList }
*
*/
public DriverList getDriverList() {
return driverList;
}

/**
* Sets the value of the driverList property.
*
* @param value
* allowed object is
* {@link DriverList }
*
*/
public void setDriverList(DriverList value) {
this.driverList = value;
}

/**
* Gets the value of the environment property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getEnvironment() {
return environment;
}

/**
* Sets the value of the environment property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setEnvironment(String value) {
this.environment = value;
}

/**
* Gets the value of the report property.
*
* @return
* possible object is
* {@link Report }
*
*/
public Report getReport() {
return report;
}

/**
* Sets the value of the report property.
*
* @param value
* allowed object is
* {@link Report }
*
*/
public void setReport(Report value) {
this.report = value;
}

}
...全文
4625 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lbhreadme 2011-06-23
  • 打赏
  • 举报
回复
也遇到了这个问题
PostX 2009-02-14
  • 打赏
  • 举报
回复
生成的Helper类不会有任何问题,请注意参照JAXB的文档获得类的使用情况.
你的使用中的:
DispatcherConfiguration dispatcherConfiguration = (DispatcherConfiguration) u
.unmarshal(new File(configFileName));
应该为:
JAXBElement<?> je = (JAXBElement<?>) um.unmarshal(new File("D:\\temp\\temp.xml"));

下面是我使用的测试数据文件和测试方法,
测试数据文件 D:\temp\temp.xml

<?xml version="1.0" encoding="UTF-8"?>
<tns:DispatcherConfiguration
xmlns:tns="http://www.example.org/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/XMLSchema temp.xsd ">
<DriverList>
<Driver Name="Diver">
<Configuration>Configuration</Configuration>
</Driver>
</DriverList>
<Environment>Environment</Environment>
<Report>
<ReportType>ReportType</ReportType>
<Location>Location</Location>
</Report>
</tns:DispatcherConfiguration>


下面是测试用例:

public class TestJAXB extends TestCase {

public void testUnmarshall() {
DispatcherConfiguration dispatcherConfiguration = null;
try {
JAXBContext jc = JAXBContext.newInstance("org.example.xmlschema");
Unmarshaller um = jc.createUnmarshaller();
JAXBElement<?> je = (JAXBElement<?>) um.unmarshal(new File(
"D:\\temp\\temp.xml"));
dispatcherConfiguration = (DispatcherConfiguration) je.getValue();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertNotNull(dispatcherConfiguration);
for (Driver driver : dispatcherConfiguration.getDriverList()
.getDriver()) {
System.out.println(driver.getConfiguration());
System.out.println(driver.getName());
}
System.out.println(dispatcherConfiguration.getEnvironment());
System.out.println(dispatcherConfiguration.getReport().getReportType());
System.out.println(dispatcherConfiguration.getReport().getLocation());
}
}


最后是测试结果
Configuration
Diver
Environment
ReportType
Location


67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧