Skip to main content

An xml file parser that makes the element text and attributes of an xml file accessible as ClassyXml class attributes.

Project description


ClassyXml

An xml file parser that makes the element text and attributes of an xml file accessible as ClassyXml class attributes.

Install it from PyPI

pip install classyxml

Usage

Parsing an XML file

<!-- countries.xml -->
<root>
  <country name="Liechtenstein">
    <rank>1</rank>
    <year>2008</year>
    <gdppc>141100</gdppc>
    <neighbor name="Austria" direction="E"/>
    <neighbor name="Switzerland" direction="W"/>
  </country>
  <country name="Singapore">
    <rank>4</rank>
    <year>2011</year>
    <gdppc>59900</gdppc>
    <neighbor name="Malaysia" direction="N"/>
  </country>
</root>
from classy_xml import ClassyXml

# Load the countries.xml file
classy = ClassyXml('countries.xml')

# Print the XML information
print(len(classy.country))                      # Output: 2

print(classy.country[0].name)                   # Output: 'Liechtenstein'
print(classy.country[0].rank.text)              # Output: '1'
print(classy.country[0].year.text)              # Output: '2008'
print(classy.country[0].gdppc.text)             # Output: '141100'
print(len(classy.country[0].neighbor))          # Output: 2
print(classy.country[0].neighbor[0].name)       # Output: 'Austria'
print(classy.country[0].neighbor[0].direction)  # Output: 'E'
print(classy.country[0].neighbor[1].name)       # Output: 'Switzerland'
print(classy.country[0].neighbor[1].direction)  # Output: 'W'

print(classy.country[1].name)                   # Output: 'Singapore'
print(classy.country[1].rank.text)              # Output: '4'
print(classy.country[1].year.text)              # Output: '2011'
print(classy.country[1].gdppc.text)             # Output: '59900'
print(len(classy.country[1].neighbor))          # Output: 1
print(classy.country[1].neighbor.name)          # Output: 'Malaysia'
print(classy.country[1].neighbor.direction)     # Output: 'N'

Generating an XML file

This will generate the countries.xml file illustrated above.

from classy_xml import ClassyXml, XmlElement

# Create an empty ClassyXml object
classy = ClassyXml()

# Creating Liechtenstein Element
# Setting the text and attributes using the XmlElement arguments
liechtenstein = XmlElement(attributes={'name': 'Liechtenstein'})
liechtenstein.rank = XmlElement(text=1)
liechtenstein.year = XmlElement(text=2008)
liechtenstein.gdppc = XmlElement(text=141100)
liechtenstein.neighbor = XmlElement(attributes={'name': 'Austria', 'direction': 'E'})
liechtenstein.neighbor = XmlElement(attributes={'name': 'Switzerland', 'direction': 'W'})

# Creating Singapore Element
# Setting the text and attributes directly
singapore = XmlElement()
singapore.name = 'Singapore'
singapore.rank = XmlElement()
singapore.rank.text = 4
singapore.year = XmlElement()
singapore.year.text = 2011
singapore.gdppc = XmlElement()
singapore.gdppc.text = 59900
singapore.neighbor = XmlElement()
singapore.neighbor[0].name = 'Malaysia'
singapore.neighbor[0].direction = 'N'

# Add the countries to ClassyXml
classy.country = liechtenstein
classy.country = singapore

# Save the XML file
classy.save_as('countries.xml')

Modifying an existing XML file

This will add a USA element to the countries.xml file illustrated above.

from classy_xml import ClassyXml, XmlElement

# Load the countries.xml file
classy = ClassyXml('countries.xml')

# Create a USA country element
usa = XmlElement(attributes={'name': 'USA'})
usa.rank = XmlElement(text=2)
usa.year = XmlElement(text=2012)
usa.gdppc = XmlElement(text=12345)
usa.neighbor = XmlElement(attributes={'name': 'Canada', 'direction': 'N'})
usa.neighbor = XmlElement(attributes={'name': 'Mexico', 'direction': 'S'})

# Add the USA country element the ClassyXml countries
classy.country = usa

# Save the XML file
classy.save()

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

classyxml-0.0.6.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

classyxml-0.0.6-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file classyxml-0.0.6.tar.gz.

File metadata

  • Download URL: classyxml-0.0.6.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for classyxml-0.0.6.tar.gz
Algorithm Hash digest
SHA256 ed8f14c4fe299c497a92fbae479bb123717f105cbceedbe3458d5d2e10db3fe8
MD5 ecdd89390be9deb1f01075708ff4fe5b
BLAKE2b-256 75ec36d5e390b6be21c8f42570a36dda28997dda58110c21646754c3505b88e2

See more details on using hashes here.

File details

Details for the file classyxml-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: classyxml-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for classyxml-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a0df029d2f4ab5d060d4e96dca36423998a523a39daf861bffc9b8ed5a118049
MD5 b14d64e9ce99c1ad35bcb724b1d4a38e
BLAKE2b-256 e26b294a9246f0f74ecd5558a426c29b9419e2841eba3920d6129453d2031d99

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