在Python中将XML转换为JSON

时间:2018-02-20 23:49:44

标签: python json xml

我试图使用ElementTree Python将以下XML文件转换为JSON。但我无法这样做。

  • Python版本:2.6.6
  • 操作系统:RedHat Enterprise Linux 6.7

许可证-V2一览info.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE netapp SYSTEM "file:/etc/netapp_gx.dtd">

<netapp xmlns="http://www.netapp.com/filer/admin" version="1.110">
  <results status="passed">
    <licenses>
      <license-v2-info>
        <customer-id>none</customer-id>
        <description>Cluster Base License</description>
        <legacy>false</legacy>
        <owner>us01cm15</owner>
        <package>base</package>
        <serial-number>1-80-090875</serial-number>
        <type>license</type>
      </license-v2-info>
      <license-v2-info>
        <customer-id>none</customer-id>
        <description>Volume Encryption License</description>
        <legacy>false</legacy>
        <owner>us01cm15t2f09</owner>
        <package>ve</package>
        <serial-number>1-81-0000000000000721735000422</serial-number>
        <type>license</type>
      </license-v2-info>
    </licenses>
  </results>
</netapp>

以下是我的尝试:

def parseXML(xml_file):
    """
    Parse XML with ElementTree
    """
    tree = ET.ElementTree(file=xml_file)
    print tree.getroot()
    root = tree.getroot()
    print "tag=%s, attrib=%s" % (root.tag, root.attrib)

    for child in root:
        print 'child-tag:', child.tag, 'child attrib', child.attrib, '\n'
        if child.tag == "results":
            for step_child in child:
                print step_child.tag

    # iterate over the entire tree
    print "-" * 40
    print "Iterating using a tree iterator"
    print "-" * 40
    iter_ = tree.getiterator()
    for elem in iter_:
        print elem.tag

    # get the information via the children!
    print "-" * 40
    print "Iterating using getchildren()"
    print "-" * 40
    appointments = root.getchildren()
    for appointment in appointments:
        appt_children = appointment.getchildren()
        for appt_child in appt_children:
            print "%s=%s" % (appt_child.tag, appt_child.text)


#----------------------------------------------------------------------


if __name__ == '__main__':
    parseXML(current_dir + '/XMLs/license-v2-list-info.xml')

我的代码的示例输出:

<Element {http://www.netapp.com/filer/admin}netapp at acfc68>
tag={http://www.netapp.com/filer/admin}netapp, attrib={'version': '1.110'}
child-tag: {http://www.netapp.com/filer/admin}results child attrib {'status': 'p
assed'} 

----------------------------------------
Iterating using a tree iterator
----------------------------------------
{http://www.netapp.com/filer/admin}netapp
{http://www.netapp.com/filer/admin}results
{http://www.netapp.com/filer/admin}licenses
{http://www.netapp.com/filer/admin}license-v2-info
{http://www.netapp.com/filer/admin}customer-id
{http://www.netapp.com/filer/admin}description
{http://www.netapp.com/filer/admin}legacy
{http://www.netapp.com/filer/admin}owner
{http://www.netapp.com/filer/admin}package
{http://www.netapp.com/filer/admin}serial-number
{http://www.netapp.com/filer/admin}type
{http://www.netapp.com/filer/admin}license-v2-info
{http://www.netapp.com/filer/admin}customer-id
{http://www.netapp.com/filer/admin}description
{http://www.netapp.com/filer/admin}legacy
{http://www.netapp.com/filer/admin}owner
{http://www.netapp.com/filer/admin}package
{http://www.netapp.com/filer/admin}serial-number
{http://www.netapp.com/filer/admin}type
{http://www.netapp.com/filer/admin}license-v2-info
{http://www.netapp.com/filer/admin}customer-id
{http://www.netapp.com/filer/admin}description
{http://www.netapp.com/filer/admin}legacy
{http://www.netapp.com/filer/admin}owner
{http://www.netapp.com/filer/admin}package
{http://www.netapp.com/filer/admin}serial-number
{http://www.netapp.com/filer/admin}type
{http://www.netapp.com/filer/admin}license-v2-info
{http://www.netapp.com/filer/admin}customer-id
{http://www.netapp.com/filer/admin}description
{http://www.netapp.com/filer/admin}legacy
{http://www.netapp.com/filer/admin}owner
{http://www.netapp.com/filer/admin}package
{http://www.netapp.com/filer/admin}serial-number
{http://www.netapp.com/filer/admin}type
{http://www.netapp.com/filer/admin}license-v2-info
{http://www.netapp.com/filer/admin}customer-id
{http://www.netapp.com/filer/admin}description

期望的输出:

{
    "@version": "1.110",
    "results": {
        "@status": "passed",
        "licenses": {
            "license-v2-info": [
                {
                    "customer-id": "none",
                    "description": "Cluster Base License",
                    "legacy": "false",
                    "owner": "us01cm15",
                    "package": "base",
                    "serial-number": "1-80-090875",
                    "type": "license"
                },
                {
                    "customer-id": "none",
                    "description": "Volume Encryption License",
                    "legacy": "false",
                    "owner": "us01cm15t2f09",
                    "package": "ve",
                    "serial-number": "1-81-0000000000000721735000422",
                    "type": "license"
                }
            ]
        }
    }
}

任何帮助都非常感谢。

注意: 我想坚持使用ElementTree模块。为了在我的机器上安装另一个模块,我必须经过批准过程。

0 个答案:

没有答案