8

I know it's possible to serialize directly to a String using XmlMapper.writeValueAsString(), but I would like to serialize a DOM tree. Either a new Document or, preferably, serialize directly to an existing DOM Node. Can this be done with Jackson?

hertzsprung
  • 9,445
  • 4
  • 42
  • 77

1 Answers1

2

I think I found the solution by using an XMLStreamWriter.

Try the following snippet:

XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.createXMLStreamWriter(new DOMResult(yourNode));

XmlMapper mapper = new XmlMapper();
ToXmlGenerator xmlGenerator = mapper .getFactory().createGenerator(sw);
mapper.writerFor(YourClass.class).writeValue(xmlGenerator, yourInstance);