Hello,

I'm not too clear on what you are trying to accomplish.  So, let me
try to give a couple of answers, so that you can possibly find a
solution.

I seems that you want a JSON representation of the XML schema, that
is, the XML in a schema document.  You want to convert the schema
into JSON, right.

gDS does not provide that capability.  However, writing a python
script that does a simple conversion and writes JSON to, for
example, standard output is fairly easy to do.  I've a attached a
script (`xml_to_json.py`) that does this.  Perhaps you could adapt
that to your needs.  What it does, basically, is use Lxml to parse
the schema, then walks the tree of elements, generating Python data
structures as it goes, and then uses `json.dumps` (from the Python
standard library) to convert it to JSON.  (You could also consider
using `lxml.etree.iterwalk` to walk the XML element tree, I
suppose.)

You may also want to be aware that gDS has the ability to generate
methods to export "literal" Python data structures as opposed to the
usual XML (`exportLiteral`).  Calling `obj.exportLiteral` (rather
than `obj.export`) writes out Python data structures (instead of
XML).  I have not worked on that capability for a long time, and I
suspect that it may be currently broken.  Let me know if this is
actually what you need, and I'll try to give it some attention.  It
would convert an XML instance (data?) document to something that I
believe could be used as JSON.  Take look at the "--export" command
line option.  If you run `generateDS.py` with --export="write
literal", then you can call the `parseLiteral` function in the
generated file to produce Python data structures.  Possibly that
could be converted to JSON with the `json` module.

Hope this helps.  If you think I could answer any more questions,
please send them.

Dave

On Wed, Mar 06, 2019 at 04:02:15PM +0000, VRL L wrote:
> Hi Dave,
> 
> Many Thanks for your wonderful work on "generateDS". I used it to generate
> python data structure from a very complex xsd (xml schema) file. Now, I
> would like to if there is way we can convert this data structure file into
> json format.
> 
> basically, what ever the output  "generateDS"  produced, need to convert
> them into json format as it is ? ie, from xsd --> generateDS --> python
> file --> json schema/format ?
> 
> Do you know if this possible ? if so, can you please shed some ideas/light
> on how to do this ?
> 
> Much appreciated your help.
> 
> Thanks,
> Vrleducation

-- 

Dave Kuhlman
http://www.davekuhlman.org
#!/usr/bin/env python

"""
synopsis:
    Convert an XML to JSON text.
    Read input from file or from stdin.
    Write output to stdout.
usage:
    python xml_to_json.py <xml-file-name>
    python xml_to_json.py -                  # convert from stdin
more info:
    Also see: https://pypi.python.org/pypi/xmljson
"""

from __future__ import print_function
import sys
from lxml import etree
import json


def convert(infile):
    doc = etree.parse(infile)
    root = doc.getroot()
    result = convert_element(root)
    return result


def convert_element(element):
    structure = {}
    structure['tag'] = element.tag
    structure['text'] = element.text
    structure['tail'] = element.tail
    structure['attrib'] = dict(element.attrib)
    children = []
    for child in element:
        children.append(convert_element(child))
    structure['children'] = children
    return structure


def main():
    args = sys.argv[1:]
    infilename = args[0]
    if infilename == '-':
        result = convert(sys.stdin)
    else:
        result = convert(infilename)
    # No formatting
    #result = json.dumps(result)
    # Or, indented formatting
    result = json.dumps(result, indent=4)
    print(result)


if __name__ == '__main__':
    main()
_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users

Reply via email to