简体   繁体   English

使用xml.dom.minidom计算python中xml标签的数量

[英]Counting number of xml tags in python using xml.dom.minidom

My XML file test.xml contains the following tags 我的XML文件test.xml包含以下标记

<?xml version="1.0" encoding="ISO-8859-1"?>
<AppName>
    <author>Subho Halder</author>
    <description> Description</description>
    <date>2012-11-06</date>
        <out>Output 1</out>
        <out>Output 2</out>
        <out>Output 3</out>
</AppName>

I want to count the number of times the <out> tag has occured 我想计算<out>标记发生的次数

This is my python code so far which I have written: 到目前为止,这是我编写的python代码:

from xml.dom.minidom import parseString
file = open('test.xml','r')
data = file.read()
file.close()
dom = parseString(data)
if (len(dom.getElementsByTagName('author'))!=0):
    xmlTag = dom.getElementsByTagName('author')[0].toxml()
    author = xmlTag.replace('<author>','').replace('</author>','')
    print author

Can someone help me out here? 有人可以帮我吗?

Try len(dom.getElementsByTagName('out')) 尝试len(dom.getElementsByTagName('out'))

from xml.dom.minidom import parseString
file = open('test.xml','r')
data = file.read()
file.close()
dom = parseString(data)
print len(dom.getElementsByTagName('out'))

gives

3

I would recommend using lxml 我建议使用lxml

import lxml.etree
doc = lxml.etree.parse(test.xml)
count = doc.xpath('count(//out)')

You can look up more information on XPATH here . 您可以在此处查找有关XPATH的更多信息。

If you want you can also use ElementTree . 如果需要,还可以使用ElementTree With the function below you will get a dictionary with the tag names as the key and number of times this tag is encountered in you XML file. 通过下面的功能,您将获得一个字典,其中以标签名称为键,以及在XML文件中遇到该标签的次数。

import xml.etree.ElementTree as ET
from collections import Counter

def count_tags(filename):
        my_tags = []
        for event, element in ET.iterparse(filename):
            my_tags.append(element.tag)
        my_keys = Counter(my_tags).keys()
        my_values = Counter(my_tags).values()
        my_dict = dict(zip(my_keys, my_values))
        return my_dict

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

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