I am using SimpleXML to parse small XML files used in a communication protocol. This all works fine, but now I am implementing a part of the protocol which includes a kind of free-form XML.
For example, an XML like this:
<telegram>
<config>
<foo>yes</foo>
<bar>no</bar>
</config>
</telegram>
Where foo
and bar
might change in the future, or an element baz
might be added, without the need to touch the parsing code. I would like to access these elements in Java using a construct like
tree.getConfig().get("bar"); // returns "no"
Can I use SimpleXML to parse that? I looked into the documentation, but couldn't find what I need.
Not out of the box - but writing a Converter will do it.
Usage:
Result: