使用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>
 * &lt;complexType name="DispatcherConfiguration">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="DriverList" type="{http://www.example.org/XMLSchema}DriverList"/>
 *         &lt;element name="Environment" type="{http://www.example.org/XMLSchema}Environment"/>
 *         &lt;element name="Report" type="{http://www.example.org/XMLSchema}Report"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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;
    }}

解决方案 »

  1.   

    生成的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