/fj-xml-to-json

Module to convert xml to json and viceversa

Primary LanguageJavaApache License 2.0Apache-2.0

fj-xml-to-json

Module to convert xml to json and viceversa

Keep a Changelog v1.1.0 badge Maven Central license code of conduct Quality Gate Status Coverage

Java runtime version Java build version Apache Maven Fugerit Github Project Conventions

Quickstart

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.

Documentation