Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

I have a program that peruses an XML record from an attachment. I have the XML record put away in a string which I might want to change over straightforwardly to a Python word reference, a similar way it is done in Django's basic JSON library.

example:

str ="<?xml version="1.0" ?><person><name>john</name><age>20</age></person"

dic_xml = convert_to_dic(str)

Ultimately, dic_xml will look like 

{'person' : { 'name' : 'john', 'age' : 20 } }

  

1 Answer

0 votes
by (26.4k points)

Look at the following code:

from xml.etree import cElementTree as ElementTree

class XmlListConfig(list):

    def __init__(self, aList):

        for element in aList:

            if element:

                # treat like dict

                if len(element) == 1 or element[0].tag != element[1].tag:

                    self.append(XmlDictConfig(element))

                # treat like list

                elif element[0].tag == element[1].tag:

                    self.append(XmlListConfig(element))

            elif element.text:

                text = element.text.strip()

                if text:

                    self.append(text)

class XmlDictConfig(dict):

    '''

    Example usage:

    >>> tree = ElementTree.parse('your_file.xml')

    >>> root = tree.getroot()

    >>> xmldict = XmlDictConfig(root)

    Or, if you want to use an XML string:

    >>> root = ElementTree.XML(xml_string)

    >>> xmldict = XmlDictConfig(root)

    And then use xmldict for what it is... a dict.

    '''

    def __init__(self, parent_element):

        if parent_element.items():

            self.update(dict(parent_element.items()))

        for element in parent_element:

            if element:

                # treat like dict - we assume that if the first two tags

                # in a series are different, then they are all different.

                if len(element) == 1 or element[0].tag != element[1].tag:

                    aDict = XmlDictConfig(element)

                # treat like list - we assume that if the first two tags

                # in a series are the same, then the rest are the same.

                else:

                    # here, we put the list in dictionary; the key is the

                    # tag name the list elements all share in common, and

                    # the value is the list itself 

                    aDict = {element[0].tag: XmlListConfig(element)}

                # if the tag has attributes, add those to the dict

                if element.items():

                    aDict.update(dict(element.items()))

                self.update({element.tag: aDict})

            # this assumes that if you've got an attribute in a tag,

            # you won't be having any text. This may or may not be a 

            # good idea -- time will tell. It works for the way we are

            # currently doing XML configuration files...

            elif element.items():

                self.update({element.tag: dict(element.items())})

            # finally, if there are no child tags and no attributes, extract

            # the text

            else:

                self.update({element.tag: element.text})

Example use:

tree = ElementTree.parse('your_file.xml')

root = tree.getroot()

xmldict = XmlDictConfig(root)

Else, if you are willing to use XML string:

root = ElementTree.XML(xml_string)

xmldict = XmlDictConfig(root)

Looking for python tutorial? Join python training course fast!

For more details, do check out...

Related questions

0 votes
1 answer
asked Nov 12, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
2 answers
asked Aug 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...