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

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


Hello,

Let say I have this XML chunk:


<?xml version="1.0"?>

<root>
  <sceneobject name="Camera_Root" fullname="Camera_Root" type="CameraRoot">
    <properties>
      <property name="Visibility" fullname="Camera_Root.visibility"
type="visibility">
        <parameters>
          <parameter scriptname="viewvis"
fullname="Camera_Root.visibility.viewvis" type="Parameter"
sourceclassname="nosource">False</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 :-)


Thanks
Bernard


More information about the Tutor mailing list