org.leibnizcenter.Main Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xml-to-json Show documentation
Show all versions of xml-to-json Show documentation
Library for converting XML to JSON
The newest version!
package org.leibnizcenter;
import com.google.gson.Gson;
import org.leibnizcenter.xml.NotImplemented;
import org.leibnizcenter.xml.TerseJson;
import org.leibnizcenter.xml.helpers.DomHelper;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
public class Main {
private static final TerseJson.WhitespaceBehaviour COMPACT_WHITE_SPACE = TerseJson.WhitespaceBehaviour.Compact;
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, NotImplemented {
String xml = ("" +
"" +
" " +
" " +
" " +
" ");
// Parse XML to DOM
Document doc = DomHelper.parse(xml);
// Convert DOM to terse representation, and convert to JSON
TerseJson.Options opts = TerseJson.Options
.with(COMPACT_WHITE_SPACE)
.and(TerseJson.ErrorBehaviour.ThrowAllErrors);
Object terseDoc = new TerseJson(opts).convert(doc);
String json = new Gson().toJson(terseDoc);
System.out.println(json);
}
}