Reputation: 11
My XML file has this kind of form:
<user name="John Doe" title="Manager">
This manager is responsible for...
<group title="USA">
<column name="Inventory">
Inventory of the products
</column>
<column name="Sells">
Sells of the products
</column>
</group>
</user>
And the users and columns can go on and on, every user can have many columns. I'm trying to use either ET or DOM to read the column name and the description between the lines. Using ET, I am able to read all the tags but not what is between the tags. for example, I can't read the "Sells of the products"
I'm sure it's a simple thing, but I'm new to Python and to this whole XML subject. I can only use python 2.7. My code is below:
My code looks like that (it's still in process and it's not the complete yet):
with open(file.xml, 'rt') as f:
tree = ET.parse(f)
for node in tree.iter():
if node.tag == 'column':
print node
Upvotes: 1
Views: 914
Reputation: 11
Finally I managed to figure out the whole code and it works for me.
for node in tree.iter():
if node.tag == 'user':
userName = node.attrib['name']
#print userName
for group in node.getchildren():
if group.tag == 'group':
groupName = group.attrib['title']
#print groupName
for column in group.getchildren():
if column.tag == 'column':
columnName = column.attrib['name']
columnDesc = column.text
#print columnName, column.text
Upvotes: 0
Reputation: 8786
I don't have your code so can't see what you are doing, but tag.text
should get you the text of the tag. Example:
import xml.etree.ElementTree as ET
xml = '''<user name="John Doe" title="Manager">
<group title="USA">
<column name="Inventory">
Inventory of the products
</column>
<column name="Sells">
Sells of the products
</column>
</group>
</user>'''
root = ET.fromstring(xml)
inventory = root.findall('.//column[@name="Inventory"]')
print inventory[0].text.strip()
sells = root.findall('.//column[@name="Sells"]')
print sells[0].text.strip()
Upvotes: 1