Newbie XML problem

KP kai.peters at gmail.com
Mon Dec 21 23:29:46 EST 2015


>From my first foray into XML with Python:

I would like to retrieve this list from the XML upon searching for the 'config' with id attribute = 'B'


config = {id: 1, canvas: (3840, 1024), comment: "a comment",
             {id: 4, gate: 3, (0,    0, 1280, 1024)},   
             {id: 5, gate: 2, (1280, 0, 2560, 1024)},
             {id: 6, gate: 1, (2560, 0, 3840, 1024)}}

I have started to use this code - but this is beginning to feel very non-elegant; not the cool Python code I usually see...

import xml.etree.ElementTree as ET

tree   = ET.parse('master.xml')
master = tree.getroot()

for config in master:
    if config.attrib['id'] == 'B':
    ...  

Thanks for any help!


<?xml version="1.0" encoding="UTF-8"?>

<master>
	<config id="A">
		<canvas>3840,1024</canvas>
		<comment>"bla"</comment>
		<panel>
			<id>1</id>
			<gate>6</gate>
			<coordinates>0,0,1280,1024</coordinates>
		</panel>
		<panel>
			<id>2</id>
			<gate>5</gate>
			<coordinates>1280,0,2560,1024</coordinates>
		</panel>
		<panel>
			<id>3</id>
			<gate>4</gate>
			<coordinates>2560,0,3840,1024</coordinates>
		</panel>
	</config>
	<config id="B">
		<canvas>3840,1024</canvas>
		<comment>"a comment"</comment>
		<panel>
			<id>4</id>
			<gate>3</gate>
			<coordinates>0,0,1280,1024</coordinates>
		</panel>
		<panel>
			<id>5</id>
			<gate>2</gate>
			<coordinates>1280,0,2560,1024</coordinates>
		</panel>
		<panel>
			<id>6</id>
			<gate>1</gate>
			<coordinates>2560,0,3840,1024</coordinates>
		</panel>
	</config>
<master>



More information about the Python-list mailing list