JAXB - Move dynamically generated namespaces to document root - java

I've got this POJO, encapsulating a dynamic, non-nested element of an Atom entry:
public class SimpleElement {
private Namespace namespace;
private String tagName;
private String value;
private Collection<Attribute> attributes;
/* getters/setters/... */
And for completeness, Attribute
public class Attribute {
private String name;
private String value;
private Namespace namespace;
/* getters/setters/... */
And Namespace:
public class Namespace {
private final String uri;
private final String prefix;
/* getters/setters/... */
SimpleElementAdapter serializes a SimpleElement into its org.w3c.dom.Element counterpart.
The only problem with this approach is that namespaces always end up at element level, never at document root.
Is there a way to dynamically declare namespaces at document root?

MY RECOMMENDATION
My recommendation is to let the JAXB implementation write the namespace declarations as it sees fit. As long as the elements are properly namespace qualified it does not really matter where the namespace declarations occur.
If you ignore my recommendation, below is an approach you can use.
ORIGINAL ANSWER
Specify the Namespaces to Include on Root Element
You can use the NamespacePrefixMapper extension to add extra namespace declarations to the root element (see: https://jaxb.java.net/nonav/2.2.11/docs/ch05.html#prefixmapper). You will need to derive from your own object model what namespaces should be declared at the root.
Note: NamespacePrefixMapper is in the com.sun.xml.bind.marshaller package. This means you will need the JAXB refereince implementation jar on your classpath (see: https://jaxb.java.net/).
import com.sun.xml.bind.marshaller.*;
public class MyNamespacePrefixMapper extends NamespacePrefixMapper {
#Override
public String getPreferredPrefix(String arg0, String arg1, boolean arg2) {
return null;
}
#Override
public String[] getPreDeclaredNamespaceUris2() {
return new String[] {"ns1", "http://www.example.com/FOO", "ns2", "http://www.example.com/BAR"};
}
}
Specify the NamespacePrefixMapper on the Marshaller
The com.sun.xml.bind.namespacePrefixMapper property is used to specify the NamespacePrefixMapper on the Marshaller.
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new MyNamespacePrefixMapper());
Demo Code
Java Model (Foo)
import javax.xml.bind.annotation.*;
#XmlRootElement
public class Foo {
private Object object;
#XmlAnyElement
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
}
Demo
import javax.xml.bind.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.w3c.dom.Element;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Foo foo = new Foo();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element element = document.createElementNS("http://www.example.com/FOO", "ns1:foo");
foo.setObject(element);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new MyNamespacePrefixMapper());
marshaller.marshal(foo, System.out);
}
}
Output
Below is sample output that will be produced:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns:ns1="http://www.example.com/FOO" xmlns:ns2="http://www.example.com/BAR">
<ns1:foo/>
</foo>
UPDATE
Clear answer, thanks. However, I need access to the NSMapper from
SimpleElementAdapter. What do you suggest? The only way I see right
now is making the NSMapper a mutable singleton so that
SimpleElementAdapter can add namespaces if needed.
I forgot about your XmlAdapter.
Java Model
Below is a more complicated iteration of the model, where instead of Foo holding an instance of a DOM element, it holds and instance of Bar that gets adapted into an instance of a DOM element.
Foo
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
#XmlRootElement
public class Foo {
private Bar bar;
#XmlAnyElement
#XmlJavaTypeAdapter(BarAdapter.class)
public Bar getBar() {
return bar;
}
public void setBar(Bar bar) {
this.bar = bar;
}
}
Bar
public class Bar {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
BarAdapter
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class BarAdapter extends XmlAdapter<Object, Bar>{
#Override
public Object marshal(Bar bar) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element element = document.createElementNS("http://www.example.com/BAR", "ns:bar");
element.setTextContent(bar.getValue());
return element;
}
#Override
public Bar unmarshal(Object arg0) throws Exception {
// TODO Auto-generated method stub
return null;
}
}
Grab Namespace Declarations
Since your object model does not hold the DOM elements directly you can't traverse it to get the namespace declarations. Instead we could do a marshal to a ContentHandler to collect them. Below are the reasons for marshalling to a ContentHandler:
It gives us an easy event which we can use to collection the namespace declarations.
It doesn't actually produce anything so it is the lightest marshal target we can use.
NsContentHandler contentHandler = new NsContentHandler();
marshaller.marshal(foo, contentHandler);
NsContentHandler
The implementation of ContentHandler will look something like:
import java.util.*;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class NsContentHandler extends DefaultHandler {
private Map<String, String> namespaces = new TreeMap<String, String>();
#Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
if(!namespaces.containsKey(prefix)) {
namespaces.put(prefix, uri);
}
}
public Map<String, String> getNamespaces() {
return namespaces;
}
}
Specify the Namespaces to Include on Root Element
The implementation of MyNamespacePrefixMapper changes a little to use the namrespaces captured from our ContentHandler.
import java.util.Map;
import java.util.Map.Entry;
import com.sun.xml.bind.marshaller.*;
public class MyNamespacePrefixMapper extends NamespacePrefixMapper {
private String[] namespaces;
public MyNamespacePrefixMapper(Map<String, String> namespaces) {
this.namespaces = new String[namespaces.size() * 2];
int index = 0;
for(Entry<String, String> entry : namespaces.entrySet()) {
this.namespaces[index++] = entry.getKey();
this.namespaces[index++] = entry.getValue();
}
}
#Override
public String getPreferredPrefix(String arg0, String arg1, boolean arg2) {
return null;
}
#Override
public String[] getPreDeclaredNamespaceUris2() {
return namespaces;
}
}
Demo Code
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Bar bar = new Bar();
bar.setValue("Hello World");
Foo foo = new Foo();
foo.setBar(bar);
Marshaller marshaller = jc.createMarshaller();
// Marshal First Time to Get Namespace Declarations
NsContentHandler contentHandler = new NsContentHandler();
marshaller.marshal(foo, contentHandler);
// Marshal Second Time for Real
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new MyNamespacePrefixMapper(contentHandler.getNamespaces()));
marshaller.marshal(foo, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns:ns="http://www.example.com/BAR">
<ns:bar>Hello World</ns:bar>
</foo>

Related

Unmarshall with JAXB doesn't work

I have a simpe XML that I want to unmarshall into a model class. I have annotated the class with JAXB annotations for defining the access type (FIELD):
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
#XmlAccessorType(XmlAccessType.FIELD)
public class DtoTest {
private String name;
public DtoTest() {}
public DtoTest(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "DtoTest [name=" + name + "]";
}
}
This is my main class where I run an unmarshal method against a simple XML saved in a String variable:
public class Test {
public static void main(String[] args) throws Exception {
Object obj = new DtoTest();
String testXML = "<dtoTest><name>example</name></dtoTest>";
obj = unmarshal(obj, testXML);
System.out.println(obj);
}
/* This is a generic unmarshall method which I've already used with success with other XML*/
public static <T> T unmarshal(T obj, String xml) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
Class<? extends Object> type = obj.getClass();
JAXBContext jc = JAXBContext.newInstance(type);
Unmarshaller unmarshaller = jc.createUnmarshaller();
obj = (T)unmarshaller.unmarshal(xsr, type).getValue();
xsr.close();
return obj;
}
}
Whenever I run the code I get the same output:
DtoTest [name=null]
I don't understand what I'm doing wrong.
I've just run your code on jdk1.7.0_67 and it works.
DtoTest [name=example]
Maybe you have some problem with included libraries? I've run it with just plain java.
What you have in your question runs perfectly fine for me. One optimization you could make to it is to create an StreamSource instead of an XMLStreamReader.
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
public class Test {
public static void main(String[] args) throws Exception {
Object obj = new DtoTest();
String testXML = "<dtoTest><name>example</name></dtoTest>";
obj = unmarshal(obj, testXML);
System.out.println(obj);
}
public static <T> T unmarshal(T obj, String xml) throws Exception {
StreamSource source = new StreamSource(new StringReader(xml));
Class<? extends Object> type = obj.getClass();
JAXBContext jc = JAXBContext.newInstance(type);
Unmarshaller unmarshaller = jc.createUnmarshaller();
obj = (T)unmarshaller.unmarshal(source, type).getValue();
return obj;
}
}
Debugging Tip
When unmarshalling is not working as expected, populate your JAXB model and marshal it to XML to see what the expected XML looks like.

JAXB get raw content of element [duplicate]

I use REST and i was wondering if i can tell jaxb to insert a string field "as-it-is" into the outgoing xml.
Certainly i count unpack it before returning, but i would like to save this step.
#XmlRootElement(name="unnestedResponse")
public class Response{
#Insert annotation here ;-)
private String alreadyXml;
private int otherDate; ...
}
Is there a possability to tell JAXB to just use the String as it is without escapting? I want that the client does not have to parse my response and then parse this field.
greetings,
m
You can use the #XmlAnyElement and specify a DomHandler to keep a portion of the XML document as a String.
Customer
import javax.xml.bind.annotation.*;
#XmlRootElement
public class Customer {
private String bio;
#XmlAnyElement(BioHandler.class)
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
}
BioHandler
import java.io.*;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.annotation.DomHandler;
import javax.xml.transform.Source;
import javax.xml.transform.stream.*;
public class BioHandler implements DomHandler<String, StreamResult> {
private static final String BIO_START_TAG = "<bio>";
private static final String BIO_END_TAG = "</bio>";
private StringWriter xmlWriter = new StringWriter();
public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
return new StreamResult(xmlWriter);
}
public String getElement(StreamResult rt) {
String xml = rt.getWriter().toString();
int beginIndex = xml.indexOf(BIO_START_TAG) + BIO_START_TAG.length();
int endIndex = xml.indexOf(BIO_END_TAG);
return xml.substring(beginIndex, endIndex);
}
public Source marshal(String n, ValidationEventHandler errorHandler) {
try {
String xml = BIO_START_TAG + n.trim() + BIO_END_TAG;
StringReader xmlReader = new StringReader(xml);
return new StreamSource(xmlReader);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
For More Information
http://blog.bdoughan.com/2011/04/xmlanyelement-and-non-dom-properties.html
Following bdoughan's answer did not work for me as I encountered errors during marshalling when the text contained the '& character (e.g. in URLs or when using HTML entities such as e.g. " ").
I was able to resolve this by changing the custom DomHandler's marshal method to
public Source marshal(String et, ValidationEventHandler veh) {
Node node = new SimpleTextNode(et);
return new DOMSource(node);
}
where SimpleTextNode implements the Node interface as follows:
class SimpleTextNode implements Node {
String nodeValue = "";
#Override
public SimpleTextNode(String nodeValue) {
this.nodeValue = nodeValue;
}
#Override
public short getNodeType() {
return TEXT_NODE;
}
// the remaining methods of the Node interface are not needed during marshalling
// you can just use the code template of your IDE...
...
}
PS: I would have loved to leave this as a comment to bdoughan's answer, but unfortunately I have way too little reputation :-(

xml:base in JAXB

I have some XML like this:
<root xml:base="http://www.example.com/foo">
<childElement someAttribute="bar/blort.html"/>
<childElement someAttribute="bar/baz/foo.html"/>
</root>
The schema for my XML defines someAttribute as being of type xs:anyURI
I want to use JAXB to unmarshall the XML into an object model a bit like this:
#XmlRootElement(name="root")
class Root {
#XmlElement(name="childElement")
private List<Child> _children;
}
class Child {
#XmlAttribute(name="someAttribute")
private URI _someAttribute;
}
I would like values of someAttribute to be resolved according to XML base, i.e. when I unmarshall the XML given above, I want the childrens' attributes to be resolved to java.net.URI instances with values http://www.example.com/foo/bar/blort.html and so on.
I was hoping a custom XmlAdapter would allow me to achieve the right result, but the XmlAdapter has no access to the surrounding context, in particular, the value of xml:base in effect at that point (note that this is not as simple as the most recent enclosing value of xml:base as xml:base can appear anywhere in the tree, and relative xml:bases must be resolved against their ancestors).
I'm using EclipseLink's MOXY implementation of JAXB, if it matters.
You can leverage an XMLStreamReader and an XmlAdapter to implement this use case:
UriAdapter
The UriAdapter is both an XmlAdapter for handling the URI property, and a StreamFilter that we will use to detect the xml:base attribute.
package forum9906642;
import java.net.URI;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.stream.*;
public class UriAdapter extends XmlAdapter<String, URI> implements StreamFilter {
private String base = "";
public UriAdapter() {
}
public UriAdapter(String base) {
this.base = base;
}
public URI unmarshal(String string) throws Exception {
return new URI(base + '/' + string);
}
public String marshal(URI uri) throws Exception {
if("".equals(base)) {
return uri.toString();
} else {
URI baseURI = new URI(base);
return baseURI.relativize(uri).toString();
}
}
public boolean accept(XMLStreamReader reader) {
if(reader.isStartElement()) {
String newBase = reader.getAttributeValue("http://www.w3.org/XML/1998/namespace", "base");
if(null != newBase) {
base = newBase;
}
}
return true;
}
}
Demo
The code below demonstrates how to use all the pieces together:
package forum9906642;
import java.io.*;
import javax.xml.bind.*;
import javax.xml.stream.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
UriAdapter uriAdapter = new UriAdapter();
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("src/forum9906642/input.xml"));
xsr = xif.createFilteredReader(xsr, uriAdapter);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(uriAdapter);
Root root = (Root) unmarshaller.unmarshal(xsr);
for(Child child : root.getChildren()) {
System.out.println(child.getSomeAttribute().toString());
}
Marshaller marshaller = jc.createMarshaller();
marshaller.setAdapter(uriAdapter);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
Child
package forum9906642;
import java.net.URI;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
#XmlAccessorType(XmlAccessType.FIELD)
class Child {
#XmlAttribute(name="someAttribute")
#XmlJavaTypeAdapter(UriAdapter.class)
private URI _someAttribute;
public URI getSomeAttribute() {
return _someAttribute;
}
public void setSomeAttribute(URI _someAttribute) {
this._someAttribute = _someAttribute;
}
}
Output
http://www.example.com/foo/bar/blort.html
http://www.example.com/foo/bar/baz/foo.html
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<childElement someAttribute="bar/blort.html"/>
<childElement someAttribute="bar/baz/foo.html"/>
</root>
I had a similar problem, but with nested xml:base (because of XInclude), so I ended up having to do this:
public class URIFixingUnmarshaller {
private final JAXBContext jaxb;
public URIFixingUnmarshaller(JAXBContext jaxb) {
this.jaxb = jaxb;
}
public <T> JAXBElement<T> unmarshal(SAXSource in, Class<T> as)
throws JAXBException {
CurrLocation curr = new CurrLocation(in.getXMLReader());
Unmarshaller u = jaxb.createUnmarshaller();
u.setListener(new URIUpdater(curr));
return u.unmarshal(new SAXSource(curr, in.getInputSource()), as);
}
private static class CurrLocation extends XMLFilterImpl {
private Locator curr;
public CurrLocation(XMLReader actual) {
setParent(actual);
}
#Override
public void setDocumentLocator(Locator to) {
super.setDocumentLocator(to);
this.curr = to;
}
String resolve(String uri) {
try {
URL base = new URL(curr.getSystemId());
URL absolute = new URL(base, uri);
return absolute.toString();
} catch (MalformedURLException probablyAlreadyAbsolute) {
return uri;
}
}
}
private static class URIUpdater extends Unmarshaller.Listener {
private final CurrLocation curr;
URIUpdater(CurrLocation curr) {
this.curr = curr;
}
#Override
public void afterUnmarshal(Object target, Object parent) {
if (target instanceof SomethingWithRelativeURI) {
SomethingWithRelativeURI casted = (SomethingWithRelativeURI) target;
casted.setPath(curr.resolve(casted.getPath()));
}
}
}
}

JAXB + Enums + Showing Multiple Values

I have an enum:
#XmlEnum
#XmlRootElement
public enum Product {
POKER("favourite-product-poker"),
SPORTSBOOK("favourite-product-casino"),
CASINO("favourite-product-sportsbook"),
SKILL_GAMES("favourite-product-skill-games");
private static final String COULD_NOT_FIND_PRODUCT = "Could not find product: ";
private String key;
private Product(final String key) {
this.key = key;
}
/**
* #return the key
*/
public String getKey() {
return key;
}
that I output in a REST service like so:
GenericEntity<List<Product>> genericEntity = new GenericEntity<List<Product>>(products) {
};
return Response.ok().entity(genericEntity).build();
and it outputs like this:
<products>
<product>POKER</product>
<product>SPORTSBOOK</product>
<product>CASINO</product>
<product>SKILL_GAMES</product>
</products>
I want it to output with both the enum name (i.e, POKER) and the key (i.e, "favourite-product-poker").
I have tried a number of different ways of doing this including using #XmlElement, #XmlEnumValue and #XmlJavaTypeAdapter, without getting both out at the same time.
Does anyone know how to achieve this, as you would for a normal JAXB annotated bean?
Thanks.
You could create a wrapper object for this, something like:
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
#XmlRootElement(name="product")
public class ProductWrapper {
private Product product;
#XmlValue
public Product getValue() {
return product;
}
public void setValue(Product value) {
this.product = value;
}
#XmlAttribute
public String getKey() {
return product.getKey();
}
}
This would correspond to the following XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<product key="favourite-product-poker">POKER</product>
You would need to pass instances of ProductWrapper to JAXB instead of Product.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(ProductWrapper.class);
ProductWrapper pw = new ProductWrapper();
pw.setValue(Product.POKER);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(pw, System.out);
}
}
You can use an adapter:
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class XmlEnumTest{
public static void main(String...str) throws Exception{
JAXBContext jc = JAXBContext.newInstance(ProductList.class);
StringWriter sw = new StringWriter();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(new ProductList(),sw);
System.out.println(sw.toString());
}
}
class ProductTypeAdaper extends XmlAdapter<ProductAdapter, Product> {
#Override
public Product unmarshal(ProductAdapter v) throws Exception {
return Product.valueOf(v.value);
}
#Override
public ProductAdapter marshal(Product v) throws Exception {
ProductAdapter result = new ProductAdapter();
result.key = v.getKey();
result.value = v.name();
return result;
}
}
#XmlType
class ProductAdapter{
#XmlAttribute
public String key;
#XmlValue
public String value;
}
#XmlJavaTypeAdapter(ProductTypeAdaper.class)
enum Product{
POKER("favourite-product-poker"),
SPORTSBOOK("favourite-product-casino"),
CASINO("favourite-product-sportsbook"),
SKILL_GAMES("favourite-product-skill-games");
private static final String COULD_NOT_FIND_PRODUCT = "Could not find product: ";
private String key;
private Product(final String key) {
this.key = key;
}
/**
* #return the key
*/
public String getKey() {
return key;
}
}
#XmlRootElement
#XmlSeeAlso({Product.class})
class ProductList{
#XmlElementWrapper(name="products")
#XmlElement(name="product")
private List<Product> list = new ArrayList<Product>(){{add(Product.POKER);add(Product.SPORTSBOOK);add(Product.CASINO);}};
}
You need to remove the #XmlEnum from your enum value, if you want it to be serialized into XML like a normal object. An enum (by definition) is represented in the XML by a single string symbol. This allows combining it with #XmlList, for example, to create an efficient, whitespace-separated list of items.

Validation using JAXB and Stax to marshal XML document

I have created an XML schema (foo.xsd) and used xjc to create my binding classes for JAXB. Let's say the root element is collection and I am writing N document objects, which are complex types.
Because I plan to write out large XML files, I am using Stax to write out the collection root element, and JAXB to marshal document subtrees using Marshaller.marshal(JAXBElement, XMLEventWriter). This is the approach recommended by jaxb's unofficial user's guide.
My question is, how can I validate the XML while it's being marshalled? If I bind a schema to the JAXB marshaller (using Marshaller.setSchema()), I get validation errors because I am only marshalling a subtree (it's complaining that it's not seeing the collection root element"). I suppose what I really want to do is bind a schema to the Stax XMLEventWriter or something like that.
Any comments on this overall approach would be helpful. Basically I want to be able to use JAXB to marshal and unmarshal large XML documents without running out of memory, so if there's a better way to do this let me know.
Some Stax implementations seem to be able to validate output. See the following answer to a similar question:
Using Stax2 with Woodstox
You can make your root collection lazy and instantiate items only when the Marshaller calls Iterator.next(). Then a single call to marshal() will produce a huge validated XML. You won't run out of memory, because the beans that are already serialized get collected by GC.
Also, it's OK to return null as a collection element if it needs to be conditionally skipped. There won't be NPE.
The XML schema validator itself seems to consume little memory even on huge XMLs.
See JAXB's ArrayElementProperty.serializeListBody()
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "TestHuge")
public class TestHuge {
static final boolean MISPLACE_HEADER = true;
private static final int LIST_SIZE = 20000;
static final String HEADER = "Header";
static final String DATA = "Data";
#XmlElement(name = HEADER)
String header;
#XmlElement(name = DATA)
List<String> data;
#XmlAnyElement
List<Object> content;
public static void main(final String[] args) throws Exception {
final JAXBContext jaxbContext = JAXBContext.newInstance(TestHuge.class);
final Schema schema = genSchema(jaxbContext);
final Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setSchema(schema);
final TestHuge instance = new TestHuge();
instance.content = new AbstractList<Object>() {
#Override
public Object get(final int index) {
return instance.createChild(index);
}
#Override
public int size() {
return LIST_SIZE;
}
};
// throws MarshalException ... Invalid content was found starting with element 'Header'
marshaller.marshal(instance, new Writer() {
#Override
public void write(final char[] cbuf, final int off, final int len) throws IOException {}
#Override
public void write(final int c) throws IOException {}
#Override
public void flush() throws IOException {}
#Override
public void close() throws IOException {}
});
}
private JAXBElement<String> createChild(final int index) {
if (index % 1000 == 0) {
System.out.println("serialized so far: " + index);
}
final String tag = index == getHeaderIndex(content) ? HEADER : DATA;
final String bigStr = new String(new char[1000000]);
return new JAXBElement<String>(new QName(tag), String.class, bigStr);
}
private static int getHeaderIndex(final List<?> list) {
return MISPLACE_HEADER ? list.size() - 1 : 0;
}
private static Schema genSchema(final JAXBContext jc) throws Exception {
final List<StringWriter> outs = new ArrayList<>();
jc.generateSchema(new SchemaOutputResolver() {
#Override
public Result createOutput(final String namespaceUri, final String suggestedFileName)
throws IOException {
final StringWriter out = new StringWriter();
outs.add(out);
final StreamResult streamResult = new StreamResult(out);
streamResult.setSystemId("");
return streamResult;
}
});
final StreamSource[] sources = new StreamSource[outs.size()];
for (int i = 0; i < outs.size(); i++) {
final StringWriter out = outs.get(i);
sources[i] = new StreamSource(new StringReader(out.toString()));
}
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = sf.newSchema(sources);
return schema;
}
}

Categories

Resources