gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:54:04 26 4
gpt4 key购买 nike

我的 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>

我想统计 <out> 的次数标签已出现

这是我到目前为止编写的 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

有人可以帮我吗?

最佳答案

尝试 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'))

给予

3

关于python - 使用 xml.dom.minidom 计算 python 中 xml 标签的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14897426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com