• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

hay/xml2json: Python script converts XML to JSON or the other way around

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

hay/xml2json

开源软件地址:

https://github.com/hay/xml2json

开源编程语言:

Python 100.0%

开源软件介绍:

XML2JSON

This module is deprecated and will not be updated anymore (May 2019)

  • To convert between text-based data formats (including XML and JSON) use my library dataknead.
  • To work with XML in Python i recommend the excellent xmltodict.

Deprecated documentation

Python script converts XML to JSON or the other way around

Usage

Make this executable

$ chmod +x xml2json

Then invoke it from the command line like this

$ xml2json -t xml2json -o file.json file.xml

Or the other way around

$ xml2json -t json2xml -o file.xml file.json

Without the -o parameter xml2json writes to stdout

$ xml2json -t json2xml file.json

Additional the options: Strip text (#text and #tail) in the json

$ xml2json -t xml2json -o file.json file.xml --strip_text

Strip namespace in the json

$ xml2json -t xml2json -o file.json file.xml --strip_namespace

In code

from xml2json import json2xml
d = {'r': {'@p': 'p1', '#text': 't1', 'c': 't2'}}
print(json2xml(d))
> b'<r p="p1">t1<c>t2</c></r>'

Installation

Either clone this repo or use pip like this:

pip install https://github.com/hay/xml2json/zipball/master

License

xml2json is released under the terms of the MIT license.

Contributors

This script was originally written by R.White, Rewritten to a command line utility by Hay Kranen with contributions from George Hamilton (gmh04) and Dan Brown (jdanbrown)

Links

How it works

xml2json relies on ElementTree for the XML parsing. This is based on pesterfish.py but uses a different XML->JSON mapping. The XML -> JSON mapping is described here

XML                              JSON
<e/>                             "e": null
<e>text</e>                      "e": "text"
<e name="value" />               "e": { "@name": "value" }
<e name="value">text</e>         "e": { "@name": "value", "#text": "text" }
<e> <a>text</a ><b>text</b> </e> "e": { "a": "text", "b": "text" }
<e> <a>text</a> <a>text</a> </e> "e": { "a": ["text", "text"] }
<e> text <a>text</a> </e>        "e": { "#text": "text", "a": "text" }

This is very similar to the mapping used for Yahoo Web Services

This is a mess in that it is so unpredictable -- it requires lots of testing (e.g. to see if values are lists or strings or dictionaries). For use in Python this could be vastly cleaner. Think about whether the internal form can be more self-consistent while maintaining good external characteristics for the JSON.

Look at the Yahoo version closely to see how it works. Maybe can adopt that completely if it makes more sense...

R. White, 2006 November 6




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap