All Projects â†’ digitalheir â†’ java-xml-to-json

digitalheir / java-xml-to-json

Licence: MIT license
👾 convert XML to a structure-preserving JSON representation

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to java-xml-to-json

Markdown Pdf
📄 Markdown to PDF converter
Stars: ✭ 2,365 (+15666.67%)
Mutual labels:  converter
Density Converter
A multi platform image density converting tool converting single or batches of images to Android, iOS, Windows or CSS specific formats and density versions given the source scale factor or width/height in dp. It has a graphical and command line interface and supports many image types (svg, psd, 9-patch, etc.) aswell as some lossless compressors like pngcrush.
Stars: ✭ 222 (+1380%)
Mutual labels:  converter
cpc
Text calculator with support for units and conversion
Stars: ✭ 89 (+493.33%)
Mutual labels:  converter
Currency Converter Swift3.0 Viper
Calculates money quick and easy way to see live foreign exchange rates (Based on swift 4.2, viper architecture and special thanks to https://github.com/hakanensari/fixer-io for conversion API)
Stars: ✭ 198 (+1220%)
Mutual labels:  converter
Dataset Serialize
JSON to DataSet and DataSet to JSON converter for Delphi and Lazarus (FPC)
Stars: ✭ 213 (+1320%)
Mutual labels:  converter
Kepubify
Fast, standalone EPUB to KEPUB converter CLI app / library (and a few other utilities).
Stars: ✭ 225 (+1400%)
Mutual labels:  converter
Mayo
3D CAD viewer and converter based on Qt + OpenCascade
Stars: ✭ 192 (+1180%)
Mutual labels:  converter
guepard
flash to html5 converter, as3 to javascript translator
Stars: ✭ 58 (+286.67%)
Mutual labels:  converter
Jpsxdec
jPSXdec: cross-platform PlayStation 1 audio and video converter
Stars: ✭ 219 (+1360%)
Mutual labels:  converter
BatchEncoder
BatchEncoder is an audio files conversion software.
Stars: ✭ 145 (+866.67%)
Mutual labels:  converter
Jsonsubtypes
Discriminated Json Subtypes Converter implementation for .NET
Stars: ✭ 201 (+1240%)
Mutual labels:  converter
Htmr
Simple and lightweight (< 2kB) HTML string to React element conversion library
Stars: ✭ 214 (+1326.67%)
Mutual labels:  converter
Bigbash
A converter that generates a bash one-liner from an SQL Select query (no DB necessary)
Stars: ✭ 230 (+1433.33%)
Mutual labels:  converter
Evernote2md
Convert Evernote .enex files to Markdown
Stars: ✭ 193 (+1186.67%)
Mutual labels:  converter
mdconv
A tool to convert markdown to html.
Stars: ✭ 38 (+153.33%)
Mutual labels:  converter
Swagger Toolbox
💡 Swagger schema model (in yaml, json) generator from json data
Stars: ✭ 194 (+1193.33%)
Mutual labels:  converter
Retrofit Logansquare
A Converter implementation using LoganSquare JSON serialization for Retrofit 2.
Stars: ✭ 224 (+1393.33%)
Mutual labels:  converter
csv2vcf
🔧 Simple script in python to convert CSV files to VCF
Stars: ✭ 66 (+340%)
Mutual labels:  converter
objectify-css
CLI for converting CSS rules to JavaScript style objects
Stars: ✭ 46 (+206.67%)
Mutual labels:  converter
Mpv Webm
Simple WebM maker for mpv, with no external dependencies.
Stars: ✭ 234 (+1460%)
Mutual labels:  converter

XML to terse JSON

GitHub version Build Status

This is a project to convert XML documents to JSON. It does this by taking a disciplined approach to create a terse JSON structure. The library has support for node types such as comments and processing instructions.

DTDs are experimentally supported (a node will be created for them), but entity references are unpacked, i.e., &lt; in XML becomes < in JSON.

Motivation

There are a bunch of libraries out there that convert, most notably in org.json.XML. The problem with most XML-to-JSON implementations is that we lose a lot of information about ordering / attributes / whatever.

Another motivation was to have a more light-weight JSON serialization of XML, since lossless transformations tend to be wordy. In this library, we trade off some readability for a reduction in bytes. JSON output for this library is typically only slightly larger in bytesize than the XML input.

Usage

Download the latest JAR or grab from Maven:

<dependencies>
        <dependency>
            <groupId>org.leibnizcenter</groupId>
            <artifactId>xml-to-json</artifactId>
            <version>0.9.2</version>
        </dependency>
</dependencies>

or Gradle:

compile 'org.leibnizcenter:xml-to-json:0.9.2'
import com.google.gson.Gson;
import org.leibnizcenter.xml.helpers.DomHelper;
import org.leibnizcenter.xml.NotImplemented;
import org.leibnizcenter.xml.TerseJson;
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 = ("<root>" +
                "<!-- thïs ïs à cómmënt. -->" +
                "  <el ampersand=\"    a &amp;    b\">" +
                "    <selfClosing/>" +
                "  </el>" +
                "</root>");

        // 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);
    }
}

produces

[9,[{"1":"root","3":[[8," thïs ïs à cómmënt. "]," ",{"1":"el","2":[["ampersand","    a \u0026    b"]],"3":[" ",{"1":"selfClosing"}," "]}]}]]

Details

JavaDoc

JavaDoc is available at https://digitalheir.github.io/java-xml-to-json/

Node types

var nodeTypes = { 
    1: "element",
    2: "attribute",
    3: "text",
    4: "cdata_section",
    5: "entity_reference",
    6: "entity",
    7: "processing_instruction",
    8: "comment",
    9: "document",
    10: "document_type",
    11: "document_fragment",
    12: "notation"
}

Utility functions

var getChildren = function (node) {
    if (node[0].match(/element|document/) {
        return node[1];
    } else {
        return undefined;
    }
};

var getName = function (node) {
    if (node[0].match(/element/)){
      return node[2];
    } else if(typeof node[0]==="string"){
        return node[0];
    } else{
        return undefined;
    }
}

Document

Translates to a fixed length JSON array:

index type description
0 int 9
1 array Children; can be any JSON element

Element

Translates to a variable length JSON array:

index type description
0 int 1
1 String tag name
2 array children as any JSON element; may be missing if element does not have children and no attributes
3 array attributes as [key, value]; may be missing if element does not have attributes

Attribute

Translates to a fixed length JSON array:

index type description
0 String attribute name
1 String attribute value

Text

Text nodes are converted to JSON strings

Comment

Translates to a fixed length JSON array:

index type description
0 int 8
1 String Comment text

CDATA

Translates to a fixed length JSON array:

index type description
0 int 4
1 String attribute value

Processing instruction

Translates to a fixed length JSON array:

index type description
0 targ 7
1 String target
2 String content

Entity Reference

Not yet implemented

Entity

Not yet implemented

Document type

Not yet implemented

Document fragment

Not yet implemented

Notation

Not yet implemented

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].