Giter VIP home page Giter VIP logo

xml-xsd-json's Introduction

xml-xsd-json

XML Schema visualization and XML Schema based XML <-> JSON conversion

XML Schema Visualization

The com.mcsuka.xml.xsd package implements an XML Schema parser. It converts an XSD file (or a number of related XSD files) to a simple tree of 'SchemaNode' objects. Each SchemaNode represents one of the following:

  • an element: xsd:element or xsd:any
  • an attribute: xsd:attribute or xsd:anyAttribute
  • an indicator: xsd:sequence xsd:all or xsd:choice

Each SchemaNode has the following properties:

  • elementName - the localName of an element or attribute, '' for an indicator, '*' for an any element or attribute
  • namespace - name space of the element or attribute
  • indicator - type of indicator, for indicator nodes
  • defaultValue default value of the element or attribute, null if there is no default value. If no default value is defined then a dummy default value is assigned to every simple type, to allow auto-generation of the XML element
  • fixedValue fixed value of the element or attribute, null if there is no default value
  • minOccurs minimum occurrence of the element, attribute or indicator
  • maxOccurs maximum occurrence of the element, attribute or indicator. Integer.MAX_VALUE is used to define 'unbounded'
  • flags to define a boolean property of the SchemaNode:
    • qualified - whether the element or attribute should be placed in its name space
    • attribute - whether it is an attribute
    • any - whether it is an "any" type
  • w3cType - the basic type of the element or attribute, or COMPLEX/MIXED for complex types
  • customType - the localName of the explicitly defined type
  • documentation - annotation/documentation of the element or type
  • restrictions - restrictions applied on the type, in human-readable format

The XML schema can be visualized as a text document using the dumpTree() convenience function of the SchemaNode class. Other visualizations (HTML, graphic, ...) can be easily implemented as separate classes.

XML to JSON translation

The com.mcsuka.xml.json package implements an XML/JSON translator, using the GSON, Xerces and Saxon-HE libraries. Translation may or may not use an XML Schema. Using an XML schema has the following advantages:

  • keep types: the resulting JSON will contain boolean and numeric values without quotes
  • keep arrays: the resulting JSON will contain array for keys that are defined as repeatable elements in the XSD, even if there is only one element in the input XML.
    • please note, you may also use the _jsonarray="true" attribute in the input XML to force array creation

Disadvantage of using an XML Schema:

  • slightly slower translation

JSON to XML translation

The com.mcsuka.xml.json package implements a JSON/XML translator, using the GSON, Xerces and Saxon-HE libraries. Translation may or may not use an XML Schema. Using an XML schema has the following advantages:

  • add correct name space to the elements and attributes
  • correct the order of the elements for 'sequence' indicator
  • create missing, mandatory elements and attributes:
    • missing elements and attributes are created with their fixed or default values. If there is no default value defined, a dummy value is chosen, according to the data type
  • remove invalid elements:
    • not in the schema
    • over the 'maxOccurs' limit
    • multiple elements inside a choice indicator

Disadvantage of using an XML Schema:

  • slightly slower translation

JSON to XML translation will modify the JSON keys that are not XML element-name compatible. All non-compatible characters will be replaced with an underscore character. Please note, there is no guarantee that the XML produced by the translator is valid (to the schema).

xml-xsd-json's People

Contributors

dependabot[bot] avatar mcsuka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.