users@jaxb.java.net

Re: namespace query

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Thu, 12 Apr 2007 11:47:21 -0700

See https://jaxb.dev.java.net/guide/Unmarshalling_is_not_working__Help_.html

Donohue Sean wrote:
> 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/encyclopaedic_data
> ODC_Store_Schema.xsd">
> <Header>
> ...
> </Header>
> ..
> </ODC_Store_Root>
>
> schema:-
>
> <xs:schema
> targetNamespace="http://www.T45PCO.com/combat_system/encyclopaedic_data"
> xmlns="http://www.T45PCO.com/combat_system/encyclopaedic_data"
> xmlns:ed="http://www.T45PCO.com/combat_system/encyclopaedic_data"
> 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>
>
>
>
> The information contained in this E-Mail and any subsequent
> correspondence is private and is intended solely for the intended
> recipient(s). The information in this communication may be
> confidential and/or legally privileged. Nothing in this e-mail is
> intended to conclude a contract on behalf of QinetiQ or make QinetiQ
> subject to any other legally binding commitments, unless the e-mail
> contains an express statement to the contrary or incorporates a formal Purchase Order.
>
> For those other than the recipient any disclosure, copying,
> distribution, or any action taken or omitted to be taken in reliance
> on such information is prohibited and may be unlawful.
>
> Emails and other electronic communication with QinetiQ may be
> monitored and recorded for business purposes including security, audit
> and archival purposes. Any response to this email indicates consent
> to this.
>
> Telephone calls to QinetiQ may be monitored or recorded for quality
> control, security and other business purposes.
>
> QinetiQ Limited
> Registered in England & Wales: Company Number:3796233
> Registered office: 85 Buckingham Gate, London SW1E 6PD, United Kingdom
> Trading address: Cody Technology Park, Cody Building, Ively Road, Farnborough, Hampshire, GU14 0LX, United Kingdom
> http://www.QinetiQ.com/home/legal.html
>


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com