[Tutor] Retrieving the text of a XML tag with ElementTree

Bernard Lebel 3dbernard at gmail.com
Wed Sep 14 20:14:43 CEST 2005


Let say I have this XML chunk:

<?xml version="1.0"?>

  <sceneobject name="Camera_Root" fullname="Camera_Root" type="CameraRoot">
      <property name="Visibility" fullname="Camera_Root.visibility"
          <parameter scriptname="viewvis"
fullname="Camera_Root.visibility.viewvis" type="Parameter"

I wish to retrieve the "False" between the opening/closing tags.
I thought I could use something like:

from elementtree.ElementTree import parse

oTree = parse( r'C:\temp\Camera_Root.xml' )

for oXMLObject in oTree.findall( '//sceneobject' ):
	if oXMLObject.attrib[ 'type' ] == 'CameraRoot':
		oXMLProps = oXMLObject.find( 'properties' )
		# We found the tag
		for oXMLProp in oXMLProps.findall( 'property' ):
			if oXMLProp.attrib[ 'name' ] == 'Visibility':
				print oXMLProp.text

Now the print statements prints an empty line, wich is obviously not
what I'm after :-)


More information about the Tutor mailing list