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

}
...全文
4861 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lbhreadme 2011-06-23
  • 打赏
  • 举报
回复
也遇到了这个问题
奇点交易 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


源码链接: https://pan.quark.cn/s/a4b39357ea24 微信小程序是由腾讯公司设计的一种轻量级应用开发平台,主要面向移动设备,旨在为用户带来无需下载安装即可直接使用的应用服务体验。在微信小程序的开发实践中,"模仿美团菜单 swiper分类菜单"是一项常见的技术应用,其目的是达成与美团外卖应用相似的交互模式,使用户能够轻松地浏览和挑选各种商品或服务。 在微信小程序的技术体系中,`swiper`组件属于轮播图功能模块,通常用于呈现多张图片或卡片式的切换界面,能够达成动态滑动展示不同分类菜单的功能。在模拟美团菜单的应用情境下,`swiper`组件一般与`swiper-item`组件协同工作,每一个`swiper-item`对应一个分类,用户可通过左右滑动来切换不同的分类菜单。 为了达成这样的功能,首先需要在微信小程序的`json`配置文档中引入`swiper`组件,并在`wxml`结构文档中构建相应的布局代码,将每个分类以`swiper-item`的形式进行组织。同时,需要在`wxss`样式文档中定义恰当的样式,保证分类菜单的视觉呈现符合设计规范。 接下来,我们将在`js`逻辑文档中处理`swiper`的切换逻辑,通过监听`bindchange`事件来识别当前选中的分类,并根据这一信息动态获取对应的子菜单信息。为了达成类似美团的层级菜单功能,可能还需要借助`picker`或`navigator`组件来展示二级分类或商品详情。 在开发阶段,需要关注以下关键点: 1. 数据绑定:保证数据与界面之间的双向链接准确无误,以便在选择分类时即时更新显示内容。 2. 动态加载:针对大量数据,可能需要采取异步加载机制,防止一次性加载过多内容引发性能瓶颈...

67,535

社区成员

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

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