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, XmlElement
# Load the countries.xml file
obj = ClassyXml('countries.xml')
# Print the XML information
print(len(obj.country)) # Output: 2
print(obj.country[0].name) # Output: 'Liechtenstein'
print(obj.country[0].rank.text) # Output: '1'
print(obj.country[0].year.text) # Output: '2008'
print(obj.country[0].gdppc.text) # Output: '141100'
print(len(obj.country[0].neighbor)) # Output: 2
print(obj.country[0].neighbor[0].name) # Output: 'Austria'
print(obj.country[0].neighbor[0].direction) # Output: 'E'
print(obj.country[0].neighbor[1].name) # Output: 'Switzerland'
print(obj.country[0].neighbor[1].direction) # Output: 'W'
print(obj.country[1].name) # Output: 'Singapore'
print(obj.country[1].rank.text) # Output: '4'
print(obj.country[1].year.text) # Output: '2011'
print(obj.country[1].gdppc.text) # Output: '59900'
print(len(obj.country[1].neighbor)) # Output: 1
print(obj.country[1].neighbor.name) # Output: 'Malaysia'
print(obj.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
obj = ClassyXml()
# Creating Country 1 Element
# Setting the text and attributes using the XmlElement arguments
obj.country = XmlElement(attributes={'name': 'Liechtenstein'})
obj.country[0].rank = XmlElement(text=1)
obj.country[0].year = XmlElement(text=2008)
obj.country[0].gdppc = XmlElement(text=141100)
obj.country[0].neighbor = XmlElement(
attributes={'name': 'Austria', 'direction': 'E'})
obj.country[0].neighbor = XmlElement(
attributes={'name': 'Switzerland', 'direction': 'W'})
# Creating Country 2 Element
# Setting the text and attributes directly
obj.country = XmlElement()
obj.country[1].name = 'Singapore'
obj.country[1].rank = XmlElement()
obj.country[1].rank.text = 4
obj.country[1].year = XmlElement()
obj.country[1].year.text = 2011
obj.country[1].gdppc = XmlElement()
obj.country[1].gdppc.text = 59900
obj.country[1].neighbor = XmlElement()
obj.country[1].neighbor[0].name = 'Malaysia'
obj.country[1].neighbor[0].direction = 'N'
# Save the XML file
obj.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
obj = ClassyXml('countries.xml')
# Add a USA country element
obj.country = XmlElement(attributes={'name': 'USA'})
obj.country[-1].rank = XmlElement(text=2)
obj.country[-1].year = XmlElement(text=2012)
obj.country[-1].gdppc = XmlElement(text=12345)
obj.country[-1].neighbor = XmlElement(
attributes={'name': 'Canada', 'direction': 'N'})
obj.country[-1].neighbor = XmlElement(
attributes={'name': 'Mexico', 'direction': 'S'})
# Save the XML file
obj.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.3.tar.gz
(6.7 kB
view hashes)
Built Distribution
Close
Hashes for classyxml-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9451d57fc2f6bc087554751f7736e2f4fff4fd21c815e393a22e0cd0ee81f64 |
|
MD5 | 32c67a80307d39ae54981c65299dbf53 |
|
BLAKE2b-256 | 3a279eea0dd74e72d18cf18bc4ce6b46315a3f3712cb9f8e49a2404153eca37d |