简体   繁体   English

使用xml.dom.minidom或elementtree读取XML文件-Python

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

My XML file has this kind of form: 我的XML文件具有以下形式:

<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. 我正在尝试使用ET或DOM来读取列名和行之间的描述。 Using ET, I am able to read all the tags but not what is between the tags. 使用ET,我能够读取所有标签,但不能读取标签之间的内容。 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. 我敢肯定这很简单,但是对于Python和整个XML主题来说我是新手。 I can only use python 2.7. 我只能使用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. 我没有您的代码,所以看不到您在做什么,但是tag.text应该为您提供标签的文本。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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