Module to convert xml to json and viceversa
Sample code to convert from XML to JSON :
try ( Reader reader = StreamHelper.resolveReader( path ) ) {
XmlToJsonHandler handler = new XmlToJsonHandler();
JsonNode node = handler.convertToJsonNode(reader);
handler.getMapper().writerWithDefaultPrettyPrinter().writeValue( outputFile , node );
}
For instance a xml like this :
<config default-list="list1">
<test-list id="list1">
<test-entry id="entry1">Entry 1</test-entry>
<test-entry id="entry2">Entry 2</test-entry>
</test-list>
</config>
Will be converted to :
{
"default-list" : "list1",
"_t" : "config",
"_e" : [ {
"id" : "list1",
"_t" : "test-list",
"_e" : [ {
"id" : "entry1",
"_t" : "test-entry",
"_v" : "Entry 1"
}, {
"id" : "entry2",
"_t" : "test-entry",
"_v" : "Entry 2"
} ]
} ]
}
try ( Reader reader = StreamHelper.resolveReader( path );
FileWriter writer = new FileWriter(outputFile) ) {
XmlToJsonHandler handler = new XmlToJsonHandler();
Element root = handler.convertToElement( reader );
DOMIO.writeDOMIndent( root , writer );
}
See conversion conventions below for more info.
- xml to json conversion conventions (see xml conversion)
- yaml support documentation