Python 带命名空间的XML

from xml.etree import ElementTree as ET
from xml.dom import minidom


def MyXMLWrite(root, path):
    rough_string = ET.tostring(root, encoding='utf-8')
    reparsed = minidom.parseString(rough_string)
    new_string = reparsed.toprettyxml(indent="\t")
    f = open(path, 'w', encoding='utf-8')
    f.write(new_string)
    f.close()


ET.register_namespace('h', "http://www.w3.org/TR/htm14")

root = ET.Element("{http://www.w3.org/TR/htm14}STUFF")
body = ET.SubElement(root, "{http://www.w3.org/TR/htm14}MORE_STUFF", attrib={"{http://www.w3.org/TR/htm14}www": "abc"})
body.text = "STUFF EVERYWHERE"

MyXMLWrite(root, 'piaoliang.xml')

 

上一篇:巩固javawbe第二天


下一篇:BOM的history对象