简体   繁体   中英

Reading XML file using xml.dom.minidom or elementtree - Python

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

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM