Yes, I believe you are right.  There are problems in that export to
Python literal data objects.  I'll try to give it some attention.  I
was able to make it work with one XML schema and an XML instance
document.  But, I suspect you are right and that there are others
that it does not handle.

However, if I understand you correctly, that's not what you want
anyway.  You want to convert a schema to JSON.  You do *not* want to
convert an instance document that validates against the schema.

So, I've attached a new version of `xml_to_json.py`.  The previous
version that I sent did not handle XML comments.  This one does.

Note that if the schema is spread across several files, some which
are included or imported by others, then you will need to combine
them somehow into a single document.  You can use
`process_includes.py` from the `generateDS` distribution to do that

Here is an example that shows (1) use of `process_includes.py` to
combine a schema; (2) use of `xml_to_json.py` to read that schema
and write out JSON; and finally, (3) loading that file into a python
object (within the Python interactive prompt):

    $ python process_includes.py mailbox.xsd tmp.xsd
    $ python xml_to_json.py tmp.xsd > tmp.json
    $ python
    Python 3.6.8 |Anaconda custom (64-bit)| (default, Dec 30 2018, 01:22:34)
    [GCC 7.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>> import json
    >>> infile = open('tmp.json', 'r')
    >>> obj = json.load(infile)

By the way, if you were able to use the export literal feature,
you'd need to use the following command line option:

    --export="write literal"

Notice that it is lower case and that there is a blank between the
words "write" and "literal".

Hope this helps.

Dave


On Fri, Mar 08, 2019 at 09:41:09AM +0000, VRL L wrote:
>    Hi Dave,
>    Many Thanks for coming back to me. 
> 
>    Yes, you are right ie, I am trying to convert xml schema (xsd file) into
>    json schema.
> 
>    now, I tried all your suggestions ie,
> 
>    a. used export="writeLiteral" option
>    C:\Python36\Scripts>python generateDS.py -o
>    C:\Users\Downloads\RR_1\content\output.py --export="writeliteral"
>    C:\Users\Downloads\RR_1\content\RR_1.xsd
> 
>    b. used process_includes.py script
>    C:\Python36\Scripts>python process_includes.py -f
>    C:\Users\Downloads\RR_1\content\RR_1.xsd
>    C:\Users\Downloads\RR_1\content\newRR_1.xsd
> 
>    c. and then called "parseLiteral" in the newly generated output.py (after
>    export="writeliteral" step a), am getting below error :
>    line added in output.py :
>    def main():
>        args = sys.argv[1:]
>        if len(args) == 1:
>            parse(args[0])
>            parseLiteral(args[0])
>        else:
>            usage()
>    if __name__ == '__main__':
> 
>    error am getting : am not sure if something needs to added or fixed in
>    output.py
> 
>    Traceback (most recent call last):
>      File "C:\Users\Downloads\RR_1\content\output_1.py", line 1557, in
>    buildAttributes
>        self.version = int(value)
>    ValueError: invalid literal for int() with base 10: '1.6.2'
>    During handling of the above exception, another exception occurred:
>    Traceback (most recent call last):
>      File "C:\Users\Downloads\RR_1\content\output_1.py", line 39487, in
>    <module>
>        main()
>      File "C:\Users\Downloads\RR_1\content\output_1.py", line 39479, in main
>        parse(args[0])
>      File "C:\Users\Downloads\RR_1\content\output_1.py", line 39393, in
>    parse
>        rootObj.build(rootNode)
>      File "C:\Users\Downloads\RR_1\content\output_1.py", line 1478, in build
>        self.buildAttributes(node, node.attrib, already_processed)
>      File "C:\Users\Downloads\RR_1\content\output_1.py", line 1559, in
>    buildAttributes
>        raise_parse_error(node, 'Bad integer attribute: %s' % exp)
>      File "C:\Users\Downloads\RR_1\content\output_1.py", line 563, in
>    raise_parse_error
>        raise GDSParseError(msg)
>    __main__.GDSParseError: Bad integer attribute: invalid literal for int()
>    with base 10: '1.6.2' (element {http://www.w3.o
>    rg/2001/XMLSchema}schema/line 1)
> 
>    Thanks,
>    V
> 
>    On Thu, Mar 7, 2019 at 11:18 PM Dave Kuhlman <dkuhl...@davekuhlman.org>
>    wrote:
> 
>      I forgot to mention one thing -- If you want to convert the schema
>      to JSON, you will want to collect all included and imported files in
>      the schema into a single XML structure.  You can use
>      `process_includes.py` to do that.  It's in the gDS distribution.
>      Run `python process_includes.py --help` for info.
> 
>      Dave
> 
>      On Thu, Mar 07, 2019 at 01:03:54PM -0800, Dave Kuhlman wrote:
>      > 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
> 
>      --
> 
>      Dave Kuhlman
>      http://www.davekuhlman.org

-- 

Dave Kuhlman
http://www.davekuhlman.org


_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users

Reply via email to