Reputation: 17621
How can I generate XML with the following schema using JAXB.
<NS1:getRatesResponse xmlns:NS1="http://mynamespaceTypes">
<response>
<NS2:rates xmlns:NS2="http://mynamespace">
<currency>USD</currency>
</NS2:rates>
<NS3:rates xmlns:NS3="http://mynamespace">
<currency>EUR</currency>
</NS3:rates>
<NS4:rates xmlns:NS4="http://mynamespace">
... etc
</response>
</NS1:getRatesResponse>
I don't know how to tell JAXB that every new item should be NS(n+1) with the same namespace. Changing XML format is not an option, because it's external.
JAXB parses this XML correctly, but when producing using same classes it produces it like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:getRatesResponse
xmlns:ns2="http://mynamespaceTypes"
xmlns:ns3="http://mynamespace">
<response>
<ns2:rates>
<currency>EUR</currency>
</ns2:rates>
<ns2:rates>
<currency>USD</currency>
</ns2:rates>
</response>
</ns3:getRatesResponse>
Upvotes: 2
Views: 6711
Reputation: 11
Adding these config to my scheme solved my issue attributeFormDefault="unqualified" elementFormDefault="qualified"
Upvotes: 0
Reputation: 149017
For this use case I would do the following:
XMLStreamWriter
getRatesResponse
and response
elements directly to the XMLStreamWriter
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
to prevent the header from being written on each marshal call.Rate
objects to the XMLStreamWriter
individually.NamespacePrefixMapper
on it to control the namespace prefix (this currently requires the JAXB RI, support for this extension is currently being added to EclipseLink JAXB (MOXy)).For More Information
Upvotes: 6