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?
Asked
Active
Viewed 1,496 times
8

hertzsprung
- 9,445
- 4
- 42
- 77
1 Answers
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);

Rick Riemer
- 74
- 5