Skip to main content

This library permits XML generation from Python objects

Project description

Build Status https://coveralls.io/repos/github/gisce/libComXML/badge.svg?branch=master

This library permits XML generation from Python objects

<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
  <CD>
    <TITLE>Tupelo Honey</TITLE>
    <ARTIST>Van Morrison</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>Polydor</COMPANY>
    <PRICE>8.20</PRICE>
    <YEAR>1971</YEAR>
  </CD>
</CATALOG>
from libcomxml.core import XmlModel, XmlField


class Cd(XmlModel):
    def __init__(self):
        self.data = XmlField('CD')
        self.title = XmlField('TITLE')
        self.artist = XmlField('ARTIST')
        self.country = XmlField('COUNTRY')
        self.company = XmlField('COMPANY')
        self.price = XmlField('PRICE')
        self.year = XmlField('YEAR')
        super(Cd, self).__init__('CD', 'data')


class Catalog(XmlModel):
    def __init__(self):
        self.catalog = XmlField('CATALOG')
        self.cds = []
        super(Catalog, self).__init__('CATALOG', 'catalog')


catalog = Catalog()
cd = Cd()
cd.feed({
    'title': 'Empire Burlesque',
    'artist': 'Bob Dylan',
    'country': 'USA',
    'company': 'Columbia',
    'price': 10.90,
    'year': 1985
})
catalog.cds.append(cd)
cd = Cd()
cd.feed({
    'title': 'Hide your hear',
    'artist': 'Bonnie Tyler',
    'country': 'UK',
    'company': 'CBS Records',
    'price': 9.90,
    'year': 1988
})
catalog.cds.append(cd)
# Also we can add XML String directly as a new element
cd = """<CD>
        <TITLE>Tupelo Honey</TITLE>
        <ARTIST>Van Morrison</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>Polydor</COMPANY>
        <PRICE>8.20</PRICE>
        <YEAR>1971</YEAR>
        </CD>"""
self.catalog.cds.append(cd)
catalog.build_tree()
print catalog

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libComXML-3.0.1.tar.gz (18.5 kB view details)

Uploaded Source

File details

Details for the file libComXML-3.0.1.tar.gz.

File metadata

  • Download URL: libComXML-3.0.1.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for libComXML-3.0.1.tar.gz
Algorithm Hash digest
SHA256 020632a65f33716cf914bf9d89441766805281f13f2cc520b0f1c285a33f7b9b
MD5 9d8cf530faacb2aa1a02c02ec3111ffe
BLAKE2b-256 841f4af2f7d4312b387a6eda21eee2b3266d6ed1104bf0d1bac2ed2f0fad394a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page