我的代码:
#!/usr/bin/python
import xml.dom.minidom
from array import *
import re
count = array('i',[0,0,0,0,0,0,0])
def find_root_tags(file,str1,i):
dom = xml.dom.minidom.parse(file)
if dom == str1:
count[i] = count[i]+1
for j in dom.getElementsByTagName(str1.lower()):
count[i] = count[i] + 1
find_text(file,str1,i)
find_root_attribute(file,str1,i,dom)
def find_text(file,str1,i):
str1 = str1.lower()
exp = re.compile(r'<.*?>')
with open(file) as f:
lines = ''.join(line for line in f.readlines())
text_only = exp.sub('',lines).strip()
text_only = text_only.lower()
k = text_only.count(str1)
count[i] = count[i]+k
def find_root_attribute(file,str1,i,dom):
str1 = str1.lower()
for child in dom.childnodes:
if child.hasAttributes():
print cmn
当我运行此代码时,它给出了错误,即文档对象没有属性childnodes
。基本上我想将文档实例转换为节点类型实例,然后想要使用节点属性childnodes
。或者我应该通过创建节点实例再次解析xml
文件。我怎么能这样做?
请帮忙,我是xml
解析