users@jaxb.java.net

RE: namespace query

From: Ernst, Matthias <matthias.ernst_at_coremedia.com>
Date: Wed, 11 Apr 2007 14:02:55 +0200

The error does indeed not match your schema. I have four interpretations to offer:

* you compiled against a different schema (maybe with elementFormDefault="unqualified")
* you missed a generated package-info.java that states the namespace for all classes
* you wrote the annotations yourself and forgot to add a namespace to the XmlElement annotation on the header field
* it's a bug in JAXB

Matthias
-- 
matthias.ernst_at_coremedia.com
software architect
+49.40.32 55 87.503
CoreMedia AG
Executive Board: Sören Stamer (CEO), Dr. Klemens Kleiminger (CFO)
Supervisory Board: Prof. Dr. Joachim Schmidt (Chairman)
Trade Register: Amtsgericht Hamburg, HR B 76277
 
> -----Original Message-----
> From: Donohue Sean [mailto:SGDONOHUE_at_qinetiq.com] 
> Sent: Tuesday, April 10, 2007 4:22 PM
> To: 'users_at_jaxb.dev.java.net'
> Subject: namespace query
> 
> I'm trying to unmarshal an xml file with jaxb 2.1, and 
> getting the error:-
> 
> "unexpected element 
> (uri:"http://www.T45PCO.com/combat_system/encyclopaedic_data",
>  local:"Header"). Expected elements are<{}Header,{}Odc>, 
> ignoring this node."
> 
> The code looks like:-
> 
> JAXBContext jc            = ...//create JAXBContext
> 
> Unmarshaller unmarshaller = jc.createUnmarshaller();
> 
> unmarsaller.setEventHandler(new DefaultValidationEventHandler());
> 
> Object o                  = unmarshaller.unmarshal(new 
> FileInputStream(filename)); // fails here
> 
> OdcStoreDataT data        = 
> ((JAXBElement<OdcStoreDataT>)o).getValue();
> 
> //OdcStoreDataT
> 
> @XMLRootElement(name="ODC_Store_Root", 
> namespace=http://www.T45PCO.com/combat_system/encyclopaedic_data)
> 
> @XMLAccessorType(XMLAccessType.FIELD)
> 
> @XMLType(name="Odc_Store_Data_T", propOrder = {"header","odc"})
> 
> public class OdcStoreDataT{
> 
>    @XMLElement(name="Header", required=true)
> 
>    protected HeaderT header;
> 
>    @XMLElement(name="Odc", required=true)
> 
>    protected List<G600dcStoreDataT> odc;
> 
>    // generated get/set methods
> 
> }
> 
> I had hoped that using jaxb would be the simple solution 
> since I'm not an XML expert.  I'm guessing its something to 
> do with the namespace that Header belongs to.  I can marshal 
> a new set of data out, and then read that in, but the 
> marshaled file has inserted ns2: namespace declarations in 
> front of Header (and standalone="yes"), so I guess that 
> allows it to find/create the appropriate automatically 
> generated java type, I don't know.  I can't edit all the xml 
> files, what changes do I need to make to the schema file to 
> fix this please?  Or do I need to somehow put the namespace 
> in the java code? 
> 
> I've tried looking at the FAQ and articles, but can't find 
> anything explaining clearly how XML namespaces are handled by 
> jaxb.  I have the Java 6 download, which I think included 
> jaxb 2.1(?) but not, AFAIK, the namespace-prefix sample?
> 
> Thanks,
> 
> Sean
> 
> xml file:-
> 
> <ODC_Store_Root 
> xmlns="http://www.T45PCO.com/combat_system/encyclopaedic_data"
>  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> xsi:schemaLocation="http://www.T45PCO.com/combat_system/encycl
> opaedic_data ODC_Store_Schema.xsd">
> 
>         <Header>
> 
>                 ...
> 
>         </Header>
> 
> ...
> 
> </ODC_Store_Root>
> 
> schema:-
> 
> <xs:schema 
> targetNamespace="http://www.T45PCO.com/combat_system/encyclopa
> edic_data" 
> xmlns="http://www.T45PCO.com/combat_system/encyclopaedic_data"
>  
> xmlns:ed="http://www.T45PCO.com/combat_system/encyclopaedic_da
> ta" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
> elementFormDefault="qualified" 
> attributeFormDefault="unqualified" version="4.0" id="ODC_Store">
> 
>         <xs:element name="ODC_Store_Root" type="Odc_Store_Data_T">
> 
>         </xs:element>
> 
>         <xs:complexType name="Header_T">
> 
>             ...
> 
>         </xs:complexType>
> 
>         <xs:complexType name="Odc_Store_Data_T">
> 
>                 <xs:sequence>
> 
>                         <xs:element name="Header" type="Header_T"/>
> 
>                         <xs:element name="Odc" 
> type="G60_Odc_Store_Data_T" minOccurs="0" maxOccurs="200"/>
> 
>                 </xs:sequence>
> 
>         </xs:complexType>
> 
> </xs:schema>