解组xml时的Unmarshal Exception

时间:2014-01-21 09:05:47

标签: java xml jaxb

异常javax.xml.bind.UnmarshalException:意外元素(uri:“http://services.forddirect.fordvehicles.com/searchTest”,local:“ClassName”)。预期的元素是(无)

我正在粘贴代码部分,我正在使用xml

try {

        File file = new File("ReflectionInput.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(SearchTest.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        SearchTest className = (SearchTest) jaxbUnmarshaller
                .unmarshal(file);
        System.out.println(className);

    } catch(JAXBException e) {
        e.printStackTrace();
    }

}

从xml解组时出现异常。

Exception javax.xml.bind.UnmarshalException: unexpected element (uri:"http://services.forddirect.fordvehicles.com/searchTest", local:"ClassName"). Expected elements are (none)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
 at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1052)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
 at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:464)
 at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:152)
 at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)

xsd文件

<?xml version="1.0" encoding="utf-8"?>
 <xs:schema xmlns="http://services.forddirect.fordvehicles.com/searchTest"
  attributeFormDefault="unqualified" elementFormDefault="qualified"
  targetNamespace="http://services.forddirect.fordvehicles.com/searchTest"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="SearchTest" type="searchTest">
  </xs:element>
  <xs:complexType name="searchTest">
   <xs:sequence>
    <xs:element name="ClassName" type="className" minOccurs="0"
     maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType>

  <xs:complexType name="className">
   <xs:sequence>
    <xs:element name="param" type="xs:string" minOccurs="0"
     maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType> 
 </xs:schema>

xml文件

     <?xml version="1.0" encoding="UTF-8"?>
     <ClassName xmlns="http://services.forddirect.fordvehicles.com/searchTest">
     <param name ="make">Ford</param>
       <param name ="model">Mustang</param>
       <param name ="year">2011</param>
       <param name ="dealerPACode">2011</param>
       <param name ="segment">car</param>
       <param name ="segmentdefiner">NGP_ModelInfo_VehicleType</param>
       <param name           ="configtoken">Config[|Ford|Mustang|2014|1|1.|100A.P8A.....COU.~YZKAA.V6.LESS.]</param>
  <param name ="vin">3FA6P0H79DR338449</param>
     </ClassName>

1 个答案:

答案 0 :(得分:0)

问题是什么:

由于SearchTest是具有命名复杂类型的全局元素,因此不会为@XmlRootElement类生成SearchTest注释。而是在@XmlElementDecl类上为其生成ObjectFactory注释。这是因为在JAXB类中对应于复杂类型,并且命名复杂类型可能具有多于一个与其对应的全局元素。

  <xs:element name="SearchTest" type="searchTest">
  </xs:element>
  <xs:complexType name="searchTest">
   <xs:sequence>
    <xs:element name="ClassName" type="className" minOccurs="0"
     maxOccurs="unbounded" />
   </xs:sequence>
  </xs:complexType>

如何修复它:

在从XML Schema生成的模型上创建JAXBContext时,您应该在ObjectFactory类或生成的模型的包名称上创建它而不是单个模型类来获取所有必要的元数据。

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);

更多信息