我编写了一个脚本,以xml格式打印出当前目录中的所有.xml文件,但我无法弄清楚如何将xmlns属性添加到顶级标记.
我想得到的输出是:
xmlns="http://www.host.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.host.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.host.org/xml/ns/dbchangelog">
但是,这是我得到的输出:
这是我的脚本:
import lxml.etree
import lxml.builder
import glob
E = lxml.builder.ElementMaker()
ROOT = E.databaseChangeLog
DOC = E.include
# grab all the xml files
files = [DOC(file=f) for f in glob.glob("*.xml")]
the_doc = ROOT(*files)
str = lxml.etree.tostring(the_doc, pretty_print=True, xml_declaration=True, encoding='utf-8')
print str
我在网上找到了一些明确设置命名空间属性的例子,here和here,但说实话,我刚刚开始时他们有点过头了.有没有其他方法将这些xmlns属性添加到databaseChangeLog标记?