Giter Club home page Giter Club logo

python-module-jk-xmljsonconv's Introduction

jk_xmljsonconv

Introduction

This python module provides possibilities to convert XML to JSON vice versa through a conversion manager.

Information about this module can be found here:

How to use this module

Requirements

This module will require the following python modules to operate:

Import

To import this module use the following statement:

import jk_xmljsonconv

Initializing the format converter main object

It is very simple to prepare the main converter object for use:

c = jk_xmljsonconv.FormatConverter(tempDirPath)

The variable tempDirPath must provide an exsting and empty directory. Here temporary files required during conversion will be stored.

Files created in this temporary directory will be provided to the caller. If you request a file from the converter it will create one for you. It is your resposibility to move the file to whichever place you want it to be after the conversion process. (Or delete the file if you decide to not use the file any more.)

List the formats and conversions supported

To get an overview about what kind of conversions are supported, run the following code:

print("The following formats are supported: " + str(c.listFormats()) + "\n")
print("The following format conversions are supported: " + str(c.listConversions()) + "\n")

This will output something like this:

The following formats are supported: ['json', 'jsonfile', 'jsonstr', 'xml', 'xmlfile', 'xmlstr']

The following format conversions are supported: ['json->jsonfile', 'json->jsonstr', 'json->xml',
'json->xmlfile', 'json->xmlstr', 'jsonfile->json', 'jsonfile->jsonstr', 'jsonfile->xml', 'jsonfile->xmlfile',
'jsonfile->xmlstr', 'jsonstr->json', 'jsonstr->jsonfile', 'jsonstr->xml', 'jsonstr->xmlfile',
'jsonstr->xmlstr', 'xml->json', 'xml->jsonfile', 'xml->jsonstr', 'xml->xmlfile', 'xml->xmlstr',
'xmlfile->json', 'xmlfile->jsonfile', 'xmlfile->jsonstr', 'xmlfile->xml', 'xmlfile->xmlstr',
'xmlstr->json', 'xmlstr->jsonfile', 'xmlstr->jsonstr', 'xmlstr->xml', 'xmlstr->xmlfile']

The file formats supported are:

File Format ID Data Type Description
json json-object An object that can be a used as a JSON data structure in memory.
jsonfile string The file path of a JSON file.
jsonstr string The JSON data provided as a string.
xml xml-object An object that can be a used as an XML data structure in memory.
xmlfile string The file path of an XML file.
xmlstr string The XML data provided as a string.

The file conversion IDs are built from these IDs.

Perform a conversion

It is very easy to perform a conversion:

resultData = str(c.convert1(convID, inputData))

F.e. if you provide an XML string this could look like this:

SOME_XML_INPUT = "<?xml version=\"1.0\"?><html><body><h1>Some heading</h1><div><p>Some text.</p></div></body></html>"

resultData = str(c.convert1("xmlstr->jsonstr", SOME_XML_INPUT))

With resultData containing the following string after conversion:

{"html":{"body":{"h1":"Some heading","div":{"p":"Some text."}}}}

Methods of class FormatConverter

The next lines declare the methods the FormatConverter class will provide:

#
# Constructor method.
#
# @param		object tempDir			Either provide a directory path here for temporary files or a <c>TempDir</c> object.
#
def __init__(self, tempDir)
#
# Get a list of formats supported.
#
# @return		string[]		Returns a list of IDs.
#
def listFormats()
#
# Get a list of all conversions supported.
#
# @return		string[]		Returns a list of IDs.
#
def listConversions()
#
# Convert the input data.
#
# @param		string convID			A conversion ID such as f.e. "xmlstr->jsonstr".
# @param		object inputData		The input data. The type of this data must correlate with the source format type specified.
# @return		object					The result data. The type of this data will depend on the conversion specified.
#
def convert1(convID, inputData)
#
# Convert the input data.
#
# @param		string fromFormat		The source format of the conversion.
# @param		string toFormat			The target format desired.
# @param		object inputData		The input data. The type of this data must correlate with the source format type specified.
# @return		object					The result data. The type of this data will depend on the conversion specified.
#
def convert2(fromFormat, toFormat, inputData)
#
# Get a converter by conversion ID.
#
# @param		string convID			A conversion ID such as f.e. "xmlstr->jsonstr".
# @return		AbstractConverter		Eiter returns <c>None</c> if no converter is available or the converter object that performs the conversion.
#
def getConverter1(convID)
#
# Get a converter by conversion ID.
#
# @param		string fromFormat		The source format of the conversion.
# @param		string toFormat			The target format desired.
# @return		AbstractConverter		Eiter returns <c>None</c> if no converter is available or the converter object that performs the conversion.
#
def getConverter2(fromFormat, toFormat)

Contact Information

This is Open Source code. That not only gives you the possibility of freely using this code it also allows you to contribute. Feel free to contact the author(s) of this software listed below, either for comments, collaboration requests, suggestions for improvement or reporting bugs:

License

This software is provided under the following license:

  • Apache Software License 2.0

python-module-jk-xmljsonconv's People

Contributors

jkpubsrc avatar

Watchers

 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.