我認爲現在解僱minidom API是不合時宜的。有一些輔助函數,我們可以像pythonic那樣,我們希望,例如:
# Helper function to wrap the DOM element/attribute creation API.
def El(tag, attribs = None, text = None):
el = doc.createElement(tag)
if text: el.appendChild(doc.createTextNode(text))
if attribs is None: return el
for k, v in attribs.iteritems(): el.setAttribute(k, v)
return el
# Construct an element tree from the passed tree.
def make_els(parent_el, this_el, child_els):
parent_el.appendChild(this_el)
for x in child_els:
if type(x) is tuple:
child_el, grandchild_els = x
make_els(this_el, child_el, grandchild_els)
else:
this_el.appendChild(x)
doc.removeChild(doc.documentElement)
make_els(doc, El('html', { 'xmlns': 'http://www.w3.org/1999/xhtml', 'dir': 'ltr', 'lang': 'en' }), [
( El('head'), [
El('meta', { 'http-equiv': 'Content-Type', 'content': 'text/html; charset=utf-8' }),
El('meta', { 'http-equiv': 'Content-Style-Type', 'content': 'text/css' }),
El('link', { 'rel': 'stylesheet', 'type': 'text/css', 'href': 'main.css', 'title': 'Default Stylesheet' }),
El('title', {}, 'XXXX XXXX XXXXr {}, {}'.format(args.xxxx, env.build_time))
]),
( El('frameset', { 'cols': '20%, 80%' }), [
El('frame', { 'src': 'xxx_list.html', 'name': 'listframe', 'title': 'XXXX XXXX XXXX' }),
El('frame', { 'src': 'xxx_all_xxxx_all.html', 'name': 'regframe', 'title': 'XXX XXXX XXXX' }),
( El('noframes'), [
( El('body'), [
El('h2', {}, 'Frame Alert'),
El('p', {}, 'This document is designed to be viewed using the frames feature.')
])
])
])
])
print '\ndoc:\n', doc.toprettyxml(indent = ' ')
我建議使用Google搜索XPath。 – FatalError
只是在旁邊注意:您不能用正則表達式來解析XML(或HTML或大多數標記語言)。後者是類型3(常規),前者不是。 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Hyperboreus