如果XML有多个顶级项目,如何使用Python ElementTree读取XML文件?
我有一个XML文件,我想用Python ElementTree阅读。
不幸的是,它有多个顶级标签。我会将<doc>...</doc>
包裹在XML周围,除非我必须在<doc>
和<?xml>
字段之后放置<!DOCTYPE>
。但找出<!DOCTYPE>
结束的地方并非易事。
我有什么:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE FOO BAR "foo.dtd" [
<!ENTITY ...>
<!ENTITY ...>
<!ENTITY ...>
]>
<ARTICLE> ... </ARTICLE>
<ARTICLE> ... </ARTICLE>
<ARTICLE> ... </ARTICLE>
<ARTICLE> ... </ARTICLE>
我想要的是什么:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE FOO BAR "foo.dtd" [
<!ENTITY ...>
<!ENTITY ...>
<!ENTITY ...>
]>
<DOC>
<ARTICLE> ... </ARTICLE>
<ARTICLE> ... </ARTICLE>
<ARTICLE> ... </ARTICLE>
<ARTICLE> ... </ARTICLE>
</DOC>
注意标签ARTICLE的名称可能会改变,所以我不能为它而烦恼。
有人可以向我建议如何在XML标题后添加封闭的<doc>...</doc>
,或建议其他解决方法吗?
答案 0 :(得分:0)
我编写了以下函数来在 XML处理指令之后添加一个顶级标记。您现在可以在common Python library common.myelementtree.add_toplevel_tag
中找到此代码import re
xmlprocre = re.compile("(\s*<[\?\!])")
def add_toplevel_tag(string):
"""
After all the XML processing instructions, add an enclosing top-level <DOC> tag, and return it.
e.g.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE FOO BAR "foo.dtd" [ <!ENTITY ...> <!ENTITY ...> <!ENTITY ...> ]> <ARTICLE> ...
</ARTICLE> <ARTICLE> ... </ARTICLE> <ARTICLE> ... </ARTICLE> <ARTICLE> ... </ARTICLE>
=>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE FOO BAR "foo.dtd" [ <!ENTITY ...> <!ENTITY ...> <!ENTITY ...> ]><DOC> <ARTICLE> ...
</ARTICLE> <ARTICLE> ... </ARTICLE> <ARTICLE> ... </ARTICLE> <ARTICLE> ... </ARTICLE></DOC>
"""
def _advance_proc(string, idx):
# If possible, advance over whitespace and one processing
# instruction starting at string index idx, and return its index.
# If not possible, return None
# Find the beginning of the processing instruction
m = xmlprocre.match(string[idx:])
if m is None: return None
#print "Group", m.group(1)
idx = idx + len(m.group(1))
#print "Remain", string[idx:]
# Find closing > bracket
bracketdebt = 1
while bracketdebt > 0:
if string[idx] == "<": bracketdebt += 1
elif string[idx] == ">": bracketdebt -= 1
idx += 1
#print "Remain", string[idx:]
return idx
loc = 0
while 1:
# Advance one processing instruction
newloc = _advance_proc(string, loc)
if newloc is None: break
else: loc = newloc
return string[:loc] + "<DOC>" + string[loc:] + "</DOC>"