简体   繁体   中英

Using Jackson XmlMapper to serialize to an XML DOM

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM