You’ll need python-lxml, which is “pythonic binding for the libxml2 and libxslt libraries”.

Here’s an XML file I created for my RDS transmitter:

<?xml version='1.0' encoding='UTF-8' ?>
<rds>
	<group type="0A">
		<field name="TP" type="bool">true</field>
		<field name="TA" type="bool">false</field>
		<field name="MuSp" type="bool">true</field>
		<field name="PI" type="string">A210</field>
		<field name="PTY" type="int">31</field>
		<field name="AF1" type="float">89.8</field>
		<field name="AF2" type="float">102.3</field>
		<field name="PS" type="string">AZIMOUT.</field>
	</group>
	<group type="2A">
		<field name="RadioText" type="string">JRCRadio</field>
	</group>
</rds>

and here’s the python code to parse it:

#!/usr/bin/env python
from lxml import etree

xml = etree.parse('rds_data.xml')
print(etree.tostring(xml, pretty_print=True))
for x in xml.iter("group"):
	if x.keys():
		for name, value in x.items():
			print "group", name, value
		for y in x.getchildren():
			print "\t", y.tag
			for name, value in y.items():
				print "\t\t", name, value
			print "\t\tvalue", y.text
Advertisements