Skip to content

Converts a Python dictionary to a valid XML string and vice versa. Supports attributes and CDATA

License

Notifications You must be signed in to change notification settings

lalitpatel/dict2xml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dict-2-xml

Converts a Python dictionary to a valid XML string. Supports attributes and CDATA.

Usage

from dict2xml import Dict2XML

book = {
    '@attributes': {
        'type': 'fiction'
    },
    '@text': 1984
}
print(Dict2XML('book', book).to_xml_string(xml_declaration=False))
# <book type="fiction">1984</book>

Examples

books = {}
# <books/>

XML with text nodes

Text modes can be added to any node by directly assigning the value or by having a @text (or @cdata) key in the dict

books = {
    'book': 1984
}
# or 
books = {
    'book': {
        '@text': 1984
    }
}
<books>
  <book>1984</book>
</books>

Attributes can be added to any node by having a @attributes key in the dict

books = {
    '@attributes': {
        'type': 'fiction'
    },
    'book': 1984
}
<books type="fiction">
  <book>1984</book>
</books>

XML with mutliple children of same kind

books = {
    '@attributes': {
        'type': 'fiction'
    },
    'book': ['1984', 'Foundation', 'Stranger in a Strange Land']
}
<books type="fiction">
  <book>1984</book>
  <book>Foundation</book>
  <book>Stranger in a Strange Land</book>
</books>

Mutliple children using CDATA

A text node can be wrapped in CDATA by having a @cdata key in the dict

books = {
    'book': [
        {
            'title': '1984',
            'isbn': 973534,
        },
        {
            'title': {'@cdata': 'Foundation'},
            'price': '$15.61',
            'isbn': 573234,
        },
        {
            'title': {'@cdata': 'Stranger in a Strange Land'},
            'price': '$18.00',
            'isbn': 341232
        }
    ]
}
<books>
  <book>
    <isbn>973523442132</isbn>
    <title>1984</title>
  </book>
  <book>
    <price>$15.61</price>
    <isbn>57352342132</isbn>
    <title><![CDATA[Foundation]]></title>
  </book>
  <book>
    <price>$18.00</price>
    <isbn>341232132</isbn>
    <title><![CDATA[Stranger in a Strange Land]]></title>
  </book>
</books>

Complex example with many different data types.

from collections import OrderedDict
books = OrderedDict({
    '@attributes': {
        'type': 'fiction'
    },
    'book': [
        {
            '@attributes': {
                'author': 'George Orwell',
                'available': None
            },
            'title': '1984',
            'isbn': 972132L,
        },
        {
            '@attributes': {
                'author': 'Isaac Asimov',
                'available': False
            },
            'title': {'@cdata': 'Foundation'},
            'price': '$15.61',
            'isbn': 5735232L,
        },
        {
            '@attributes': {
                'author': 'Robert A Heinlein',
                'available': True
            },
            'title': {'@cdata': 'Stranger in a Strange Land'},
            'price': {
                '@attributes': {
                    'discount': '10%'
                },
                '@text': '$18.00'
            },
            'isbn': 3412332L
        }
    ]
})
<books type="fiction">
  <book available="" author="George Orwell">
    <isbn>973523442132</isbn>
    <title>1984</title>
  </book>
  <book available="false" author="Isaac Asimov">
    <price>$15.61</price>
    <isbn>57352342132</isbn>
    <title><![CDATA[Foundation]]></title>
  </book>
  <book available="true" author="Robert A Heinlein">
    <price discount="10%">$18.00</price>
    <isbn>341232132</isbn>
    <title><![CDATA[Stranger in a Strange Land]]></title>
  </book>
</books>

About

Converts a Python dictionary to a valid XML string and vice versa. Supports attributes and CDATA

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages