I have an issue while un-marshalling simple XML (a subset of CSDL) using JAXB.
Someone already tried to assist me in the past (here), however it is partially worked and I don't know what to do...
Please consider the following XML:
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="3.0">
<Schema xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:sap="http://www.sap.com/Protocols/SAPData" xmlns="http://schemas.microsoft.com/ado/2009/11/edm" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" Namespace="myNS">
</Schema>
</edmx:DataServices>
</edmx:Edmx>
As I was told, I have a package-info.java file that looks like (in the same package):
#XmlSchema(
namespace="http://schemas.microsoft.com/ado/2007/06/edmx",
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns={
#XmlNs(prefix="edmx", namespaceURI="http://schemas.microsoft.com/ado/2007/06/edmx"),
#XmlNs(prefix="", namespaceURI="http://schemas.microsoft.com/ado/2009/11/edm"),
#XmlNs(prefix="m", namespaceURI="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")
}
)
#XmlAccessorType(XmlAccessType.FIELD)
package com.sap.ndb.studio.rdl.csdlparser.jaxb.objects;
import javax.xml.bind.annotation.*;
In addition, I have the following data structure:
Edmx.java
package com.sap.ndb.studio.rdl.csdlparser.jaxb.objects;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "Edmx")
public class Edmx {
#XmlElement(name = "DataServices")
private DataService dataService;
public DataService getDataService() {
return dataService;
}
}
DataService.java
package com.sap.ndb.studio.rdl.csdlparser.jaxb.objects;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class DataService {
#XmlElement(name = "Schema")
private Schema schema;
#XmlAttribute(name = "DataServiceVersion")
private double version;
public Schema getSchema() {
return schema;
}
}
Schema.java
package com.sap.ndb.studio.rdl.csdlparser.jaxb.objects;
#XmlRootElement
public class Schema {
....
}
Notice: in Schema.java I have some implementation which does not related to the XML so I have took it off (internal logic).
After un-marshalling the XML using the JAXB, The returned Edmx object contains null values both in 'schema' and 'version' members, although I have mentioned all xmlns parameters in my package-info.java.
Anyone? :(
UPDATE
In my answer to one of your previous questions I provider a mapping for the model from this question.
Error while trying to unmarshal EDMX with JAXB
I have updated this answer to address the following comment that you made:
Why should I declare
'namespace=schemas.microsoft.com/ado/2009/11/edm'; in my #XmlElement?
sorry for being annoying (this is my first experience with JAXB) but I
just gonna have a long XML with many #XmlElement nodes similar to the
'Schema' and I would like to declare the namespace for them only
once...
You can reduce the number of times you need to declare a namespace by organizing your model classes into different packages based on the namespace that they correspond to.
Package 1 for Namespace http://schemas.microsoft.com/ado/2007/06/edmx
package-info
For each package we will use the #XmlSchema annotation to specify the namespace qualification. In this example we only need to specify the namespace for this particular package.
#XmlSchema(
namespace="http://schemas.microsoft.com/ado/2007/06/edmx",
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns={
#XmlNs(
prefix="edmx",
namespaceURI="http://schemas.microsoft.com/ado/2007/06/edmx"
),
}
)
#XmlAccessorType(XmlAccessType.FIELD)
package forum14875956.edmx;
import javax.xml.bind.annotation.*;
Edmx
The XML elements corresponding to the Edmx class will be namespace qualified according to what we defined on the #XmlSchema annotation for this package.
package forum14875956.edmx;
import javax.xml.bind.annotation.*;
#XmlRootElement(name = "Edmx")
public class Edmx {
#XmlElement(name = "DataServices")
private DataService dataService;
public DataService getDataService() {
return dataService;
}
}
DataService
The DataService class contains a reference to a class corresponding to a different XML namespace. If the Schema class was in the same package we could use the #XmlElement annotation to override the namespace qualification. Since Schema is in a different package we can use the #XmlElementRef annotation. This tells JAXB to derive the element information from the root element configured for that class.
package forum14875956.edmx;
import javax.xml.bind.annotation.*;
import forum14875956.schema.Schema;
public class DataService {
//#XmlElement(namespace="http://schemas.microsoft.com/ado/2009/11/edm", name="Schema")
#XmlElementRef
private Schema schema;
public Schema getSchema() {
return schema;
}
}
Package 2 for Namespace http://schemas.microsoft.com/ado/2009/11/edm
Again we will use the #XmlSchema to declare the namespace information for the second package.
package-info
#XmlSchema(
namespace="http://schemas.microsoft.com/ado/2009/11/edm",
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns={
#XmlNs(
prefix="",
namespaceURI="http://schemas.microsoft.com/ado/2009/11/edm"
)
}
)
#XmlAccessorType(XmlAccessType.FIELD)
package forum14875956.schema;
import javax.xml.bind.annotation.*;
Schema
The elements in the Schema class will be namespace qualified based namespace information in the #XmlSchema annotation for its package.
package forum14875956.schema;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="Schema")
public class Schema {
}
ORIGINAL ANSWER
You need to include the namespace URI on the mapping for the schema property:
#XmlRootElement
public class DataService {
#XmlElement(name = "Schema" , namespace="http://schemas.microsoft.com/ado/2009/11/edm")
private Schema schema;
#XmlAttribute(name = "DataServiceVersion")
private double version;
public Schema getSchema() {
return schema;
}
}
Full Example
A little while I ago I answered one of your questions providing a complete mapping for this model:
Error while trying to unmarshal EDMX with JAXB
Related
I am trying to create a Sitemap index file with JAXB. Following the requirements for creating the sitemap, I have to add the namespace attribute in the root element:
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
I would like to have a simple way to sort this out. Since this seems to be a standard procedure I would like to not do a complex workaround or add more dependencies to my project in order to solve this issue
The current output is the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:sitemapindex xmlns:ns2="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://www.example.com/sitemap-1.xml</loc>
<lastmod>2017-05-01T15:41:17.561+01:00</lastmod>
</sitemap>
</ns2:sitemapindex>
My SitemapIndex model is the following:
#XmlRootElement(name = "sitemapindex", namespace="http://www.sitemaps.org/schemas/sitemap/0.9")
#XmlAccessorType(XmlAccessType.FIELD)
public class SitemapIndex {
#XmlElement(name = "sitemap")
private List<Sitemap> sitemaps;
public void setSitemaps(List<Sitemap> sitemaps) {
this.sitemaps = sitemaps;
}
public List<Sitemap> getSitemaps() {
return sitemaps;
}
}
I have also tried to add the namespace field manually and it works for generating the file but I an exception is thrown when I try to read the file.
#XmlAttribute(name="xmlns")
private final String namespace ="http://www.sitemaps.org/schemas/sitemap/0.9";
You can use #XmlSchmea [1] package level annotation in order to set prefix. In your case, we just set empty prefix.
Simply create package-info.java file in your package e.g. com.stackoverflow.jaxb, with similar content.
package-info.java
#XmlSchema(namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
xmlns = {#XmlNs(prefix = "",
namespaceURI = "http://www.sitemaps.org/schemas/sitemap/0.9")},
elementFormDefault = XmlNsForm.QUALIFIED)
package com.stackoverflow.jaxb;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Note, that you can remove namespace attribute from #XmlRootElement annotation.
[1] https://jaxb.java.net/nonav/2.2.4/docs/api/javax/xml/bind/annotation/XmlSchema.html
i'm trying to unmarshall an xml like this:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:b2bHotelSOAP" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getAvailableHotelResponse>
<return xsi:type="ns1:getAvailableHotelResponse">
<responseId xsi:type="xsd:integer">1</responseId>
<searchId xsi:type="xsd:string">HR-47754204</searchId>
<totalFound xsi:type="xsd:integer">20</totalFound>
<availableHotels SOAP-ENC:arrayType="ns1:hotel[20]" xsi:type="ns1:hotelArray">
...
</availableHotels>
</return>
</ns1:getAvailableHotelResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
i'm using that package-info.java to specify the namespace used into soap response:
#XmlSchema(
namespace="http://schemas.xmlsoap.org/soap/envelope/",
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns={
#XmlNs(namespaceURI = "http://schemas.xmlsoap.org/soap/envelope/", prefix = "SOAP-ENV"),
#XmlNs(namespaceURI = "urn:b2bHotelSOAP", prefix = "ns1"),
#XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema", prefix = "xsd"),
#XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"),
#XmlNs(namespaceURI = "http://schemas.xmlsoap.org/soap/encoding/", prefix = "SOAP-ENC")
}
)
#XmlAccessorType(XmlAccessType.FIELD)
package com.giove.viaggi.hsw.provider.hotelspro;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
what i need is to unmarshall that soap xml into this bean:
package myPackage;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="Envelope")
public class MyBean {
#XmlPath("return/availableHotels/item")
private List<Hotel> hotels;
public List<Hotel> getHotels(){
return this.hotels==null?new ArrayList<Hotel>():this.hotels;
}
}
When i try to unmarshall it using jaxbUnmarshaller it gives me always null for the hotels attribute even if they are present into soap response.
Do i make any mistake?
Can please someone give me an help?
Thanks!
MyBean
Below is what the #XmlPath annotation on your MyBean class should look like. Some things to note:
Your domain class must be in the same package as the package-info class you want applied to it.
When you use #XmlPath you need to include each step in the path, you had left some out.
Namespace qualfifcation in the #XmlPath corresponds to the prefixes you have defined in the #XmlSchema annotation (see: http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html).
package com.giove.viaggi.hsw.provider.hotelspro;
import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="Envelope")
public class MyBean {
#XmlPath("SOAP-ENV:Body/ns1:getAvailableHotelResponse/return/availableHotels/item")
private List<Hotel> hotels;
public List<Hotel> getHotels(){
return this.hotels==null?new ArrayList<Hotel>():this.hotels;
}
}
Note
Instead of mapping the MyBean class to the Envelope element I would map it to the local root return and pass it that element to unmarshal instead.
http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
I have a class which contains an ArrayList(SuperClass) property. Now I wish to unmarshall the following XML file which contains different element names in that collection because these are subclasses of the Superclass. Is there a way of doing this with Moxy?
<?xml version="1.0" encoding="UTF-8"?>
<SmMessageSet xmlns:nav="urn:ccsds:recommendation:navigation:schema:ndmxml:R1.5"
xmlns="urn:ccsds:recommendation:service_management:schema:sccs:R1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:ccsds:recommendation:service_management:schema:sccs:R1.0 file:/C:/CCSDS-910.11-B-1_XML_schemas/CCSDS-910.11-B-1_XML_schemas/SmSchema-v1.0.0.xsd">
<sccsSmVersionRef>sccsSmVersionRef0</sccsSmVersionRef>
<smSource>smSource0</smSource>
<smDestination>smDestination0</smDestination>
<serviceAgreementRef>serviceAgreementRef0</serviceAgreementRef>
<smMessages>
<querySpaceCommunicationServiceProfileFailedReturn>
<messageSequenceNumber>50</messageSequenceNumber>
<messageTimestamp>2006-05-04T18:13:51.0</messageTimestamp>
<invocationMessageSequenceNumber>50</invocationMessageSequenceNumber>
<spaceCommunicationServiceProfileRef>spaceCommunicationServiceProfileRef0
</spaceCommunicationServiceProfileRef>
<qscspError>
<erroredItem>erroredItem0</erroredItem>
<diagnostic>operation timeout</diagnostic>
</qscspError>
<qscspError>
<erroredItem>erroredItem1</erroredItem>
<diagnostic>operation timeout</diagnostic>
</qscspError>
</querySpaceCommunicationServiceProfileFailedReturn>
<createUserAccountInvocation1>
<messageSequenceNumber>50</messageSequenceNumber>
<messageTimestamp>2006-05-04T18:13:51.0</messageTimestamp>
<username>createdUser</username>
<password>createdPassword</password>
<firstname>Test</firstname>
<lastname>User</lastname>
<email>test.user#host.de</email>
<role>SCHEDULING_OFFICER</role>
<superuser>0</superuser>
</createUserAccountInvocation1>
</smMessages>
</SmMessageSet>
The querySpaceCommunicationServiceProfileFailedReturn and createUserAccountInvocation are in my java object model subclasses of SmMessage base class, which is held by the SmMessageSet class in an, as above described, ArrayList of SmMessage classes.
I would also like to not change the current XML structure (i.e. create a wrapper element around the SmMessages in the XML file).
Any help would be appreciated :)
You could do the following leveraging #XmlElementWrapper and #XmlElementRef:
Java Model
SmMessageSet
You can use the #XmlElementWrapper annotation to add a grouping element around the collection (see: http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html). You can also use the #XmlElementRef annotation to model the element name as the inheritance indicator (substitution groups in XML Schema, see: http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html).
package forum20745762;
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlRootElement(name="SmMessageSet")
#XmlAccessorType(XmlAccessType.FIELD)
public class SmMessageSet {
#XmlElementWrapper
#XmlElementRef
private List<SmMessage> smMessages;
}
SmMessage
JAXB/MOXy won't automatically pull in all subclasses of a class, so you can use the #XmlSeeAlso annotation to have them pulled in.
package forum20745762;
import javax.xml.bind.annotation.XmlSeeAlso;
#XmlSeeAlso({CreateUserAccountInvocation.class, QuerySpaceCommunicationServiceProfileFailedReturn1.class})
public class SmMessage {
}
CreateUserAccountInvocation
One each of the subclasses you need to annotate with #XmlRootElement. This is the element name that the #XmlElementRef annotation will match on.
package forum20745762;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class CreateUserAccountInvocation extends SmMessage {
}
QuerySpaceCommunicationServiceProfileFailedReturn1
package forum20745762;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class QuerySpaceCommunicationServiceProfileFailedReturn1 extends SmMessage {
}
package-info
We will use the package level #XmlSchema annotation to map the namespaces (see: http://blog.bdoughan.com/2010/08/jaxb-namespaces.html).
#XmlSchema(
namespace="urn:ccsds:recommendation:service_management:schema:sccs:R1.0",
elementFormDefault=XmlNsForm.QUALIFIED
)
package forum20745762;
import javax.xml.bind.annotation.*;
Demo Code
Demo
The following demo code will read the XML from your question, and then write it back out.
package forum20745762;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(SmMessageSet.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum20745762/input.xml");
SmMessageSet smMessageSet = (SmMessageSet) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(smMessageSet, System.out);
}
}
Output
The output below corresponds to just the subset of your XML document that I had mapped to:
<?xml version="1.0" encoding="UTF-8"?>
<SmMessageSet xmlns="urn:ccsds:recommendation:service_management:schema:sccs:R1.0">
<smMessages>
<querySpaceCommunicationServiceProfileFailedReturn1/>
<createUserAccountInvocation/>
</smMessages>
</SmMessageSet>
So, lets say I have this xml with several namespaces.
<Envelope xmlns:pdi="http://www.mypage.com/schemas/pdi" xmlns:ib="http://www.mypage.com/schemas/ib" xmlns="http://www.mypage.com/schemas/envelope">
<Product>
<pdi:number>123456</pdi:number>
</Product>
<Instance>
<ib:serial>abcdefg</ib:serial>
</Instance>
</Envelope>
I'm trying to build a client for it. I have an Envelope POJO that's declared like this
#XmlRootElement(name ="Envelope", namespace = "http://www.mypage.com/schemas/envelope")
public class Envelope
and inside, it has these attributes
#XmlElement(name="Product", namespace = "http://www.mypage.com/schemas/pdi")
public Product getProduct(){...}
#XmlElement(name="Instance", namespace = "http://www.mypage.com/schemas/ib")
public Instance getInstance(){...}
Also, the Product POJO looks like this:
#XmlRootElement(name="Product", namespace = "http://www.mypage.com/schemas/pdi")
public class Product
and attribute
#XmlElement(name="pdi:number", namespace = "http://www.mypage.com/schemas/pdi")
public int getNumber(){...}
For some reason, I can't get the product number. I keep getting a request error. Am I handling the namespaces correctly, or am I missing something?
For this use case I would recommend leveraging the package level #XmlSchema annotation to specify the namespace qualification.
package-info (forum14651918/package-info.java)
#XmlSchema(
namespace="http://www.mypage.com/schemas/envelope",
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns={
#XmlNs(namespaceURI = "http://www.mypage.com/schemas/envelope", prefix = ""),
#XmlNs(namespaceURI = "http://www.mypage.com/schemas/ib", prefix = "ib"),
#XmlNs(namespaceURI = "http://www.mypage.com/schemas/pdi", prefix = "pdi")
}
)
#XmlAccessorType(XmlAccessType.FIELD)
package forum14651918;
import javax.xml.bind.annotation.*;
Envelope (forum14651918/Envelope.java)
Since we have specified a namespace and elementFormDefault on the #XmlSchema annotation, all the elements corresponding to the Envelope class will be automatically qualified with the http://www.mypage.com/schemas/envelope namespace.
package forum14651918;
import javax.xml.bind.annotation.*;
#XmlRootElement(name="Envelope")
public class Envelope {
#XmlElement(name="Product")
private Product product;
#XmlElement(name="Instance")
private Instance instance;
}
Product (forum14651918/Product.java)
You can override the namespace for the Product class using the #XmlType annotation.
package forum14651918;
import javax.xml.bind.annotation.*;
#XmlType(namespace="http://www.mypage.com/schemas/pdi")
public class Product {
private int number;
}
Instance (forum14651918/Instance.java)
You can override the namespace for the Instance class using the #XmlType annotation.
package forum14651918;
import javax.xml.bind.annotation.XmlType;
#XmlType(namespace="http://www.mypage.com/schemas/ib")
public class Instance {
private String serial;
}
Demo (forum14651918/Demo.java)
Below is some code you can run to prove that everything works.
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Envelope.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum14651918/input.xml");
Envelope envelope = (Envelope) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(envelope, System.out);
}
}
For More Information
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
Try replacing name="pdi:number", namespace = "http://www.mypage.com/schemas/pdi" with name="number", namespace = "http://www.mypage.com/schemas/pdi". Prefix is not needed.
What is more looking at the XML it seems that namespace for both Product and Instance is http://www.mypage.com/schemas/envelope.
You should not need #XmlRootElement annotation for Product class. It is not a root element and is already configured on getProduct().
Full configuration that should be OK is:
#XmlRootElement(name ="Envelope", namespace = "http://www.mypage.com/schemas/envelope")
public class Envelope {
#XmlElement(name="Product", namespace = "http://www.mypage.com/schemas/envelope")
public Product getProduct(){...}
#XmlElement(name="Instance", namespace = "http://www.mypage.com/schemas/envelope")
public Instance getInstance(){...}
}
public class Product {
#XmlElement(name="number", namespace = "http://www.mypage.com/schemas/pdi")
public int getNumber(){...}
}
public class Instance {
#XmlElement(name="serial", namespace = "http://www.mypage.com/schemas/ib")
public String getSerial(){...}
}
I'm currently attempting to unmarshal some existing XML into a few classes I have created by hand. Problem is, I always get an error that tells me, JaxB expects a weather element but finds a weather element. (?)
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.aws.com/aws", local:"weather"). Expected elements are <{}api>,<{}location>,<{}weather>
I tried with and without "aws:" in the elements' name.
Here's my weather class:
#XmlRootElement(name = "aws:weather")
public class WeatherBugWeather
{
private WeatherBugApi api;
private List<WeatherBugLocation> locations;
private String uri;
#XmlElement(name="aws:api")
public WeatherBugApi getApi()
{
return this.api;
}
#XmlElementWrapper(name = "aws:locations")
#XmlElement(name = "aws:location")
public List<WeatherBugLocation> getLocations()
{
return this.locations;
}
public void setApi(WeatherBugApi api)
{
this.api = api;
}
public void setLocations(List<WeatherBugLocation> locations)
{
this.locations = locations;
}
#XmlAttribute(name="xmlns:aws")
public String getUri()
{
return this.uri;
}
public void setUri(String uri)
{
this.uri = uri;
}
}
And that's the XML I try to parse:
<?xml version="1.0" encoding="utf-8"?>
<aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0" />
<aws:locations>
<aws:location cityname="Jena" statename="" countryname="Germany" zipcode="" citycode="59047" citytype="1" />
</aws:locations>
</aws:weather>
I'm not quite sure what I'm doing wrong. Any hints? I suspect the problem to be the xmlns definition, but I have no idea what to do about it. (You can see that by looking at the uri-property. That was one unsuccessful idea. ^^) And yes, I did try to set the namespace but that rather set's the namespace's uri instead of it's ... name.
I would recommend adding a package-info class in with your domain model with the #XmlSchema annotation to specify the namespace qualification:
package-info
#XmlSchema(
namespace = "http://www.aws.com/aws",
elementFormDefault = XmlNsForm.QUALIFIED)
package com.example.foo;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Note
Your XmlRootElement and #XmlElement annotation should not contain the namespace prefix. You should have #XmlRootElement(name = "weather") instead of #XmlRootElement(name = "aws:weather")
For More Information
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
you need namespaces in your code. namespace prefixes are meaningless, you need the actual namespace (i.e. "http://www.aws.com/aws").
#XmlRootElement(name = "weather", namespace="http://www.aws.com/aws")