Serialize to / Deserialize from DOM

8 views
Skip to first unread message

Thomas Vogler

unread,
May 8, 2020, 11:10:25 AM5/8/20
to jackson-user
Hi all,

I have to (de-)serialize a complex POJO (in-)to a DOM tree.

Persisting this DOM tree is not my business and i cannot control it.

The structure of the generated DOM tree is my (Jackson's) decision, I get an Element and have to get/put my stuff from there
.
I found a way to serialize my POJO by implementing the required methods in a XMLStreamWriter2 and using this in an ToXmlGenerator.

What I don't know is how to deserialize an Element to my POJO.

All help welcome...

Thomas

Tatu Saloranta

unread,
May 9, 2020, 2:49:46 PM5/9/20
to jackson-user
Jackson-dataformat-xml uses Stax API for input/output, and does not
have similar input/output integration with DOM.
However, there are probably Stax-based tools for making
XMLStreamReader read from DOM Document or Element, and XMLStreamWriter
"write" a DOM document. In fact I think Woodstox might have something
like that; and perhaps StaxMate.

Alternative simple brute force solution is to write DOM as
byte[]/String, then feed that to Jackson via readValue().
Similarly for Jackson writing XML, parse that as DOM document.

On first approach... I think `javax.xml.transform.dom.DOMSource`
(https://docs.oracle.com/javase/8/docs/api/javax/xml/transform/dom/DOMSource.html)
can be given to `XMLInputFactory` as `StaxSource`, and this is how you
can do cleanly. Woodstox does support it.
There is similar `DOMResult` which may work as well.

-+ Tatu +-

Tatu Saloranta

unread,
May 9, 2020, 2:53:40 PM5/9/20
to jackson-user
One thing to add: so, Woodstox:

https://github.com/FasterXML/woodstox

has some unit tests that may be useful in showing usage; especially:

src/test/java/stax2/dom/TestDomCompat.java (reading)
src/test/java/stax2/dom/TestDomWrite.java (writing)

which test functionality wrt DOMSource / DOMResult.

I hope this helps,

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages