67,515
社区成员
发帖
与我相关
我的任务
分享
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>
<?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>
//
// 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;
}
}
<?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());
}
}