33 import xml.dom.minidom
38 while c
and c.nodeType != xml.dom.Node.ELEMENT_NODE:
45 while c
and c.nodeType != xml.dom.Node.ELEMENT_NODE:
51 parent = node.parentNode
54 if not isinstance(by, list):
63 parent.insertBefore(c, node)
66 parent.insertBefore(doc, node)
69 parent.removeChild(node)
74 Helper function to fetch a single attribute value from tag
75 :param tag (xml.dom.Element): DOM element node
76 :param a (str): attribute name
77 :return: attribute value if present, otherwise None
79 if tag.hasAttribute(a):
82 return tag.getAttribute(a)
89 Helper routine for fetching optional tag attributes
90 :param tag (xml.dom.Element): DOM element node
91 :param attrs [str]: list of attributes to fetch
98 Helper routine for fetching required tag attributes
99 :param tag (xml.dom.Element): DOM element node
100 :param attrs [str]: list of attributes to fetch
101 :raise RuntimeError: if required attribute is missing
104 for (res, name)
in zip(result, attrs):
106 raise RuntimeError(
"%s: missing attribute '%s'" % (tag.nodeName, name))
116 writer.write(indent +
"<" + self.tagName)
118 attrs = self._get_attributes()
119 a_names = sorted(attrs.keys())
121 for a_name
in a_names:
122 writer.write(
" %s=\"" % a_name)
123 xml.dom.minidom._write_data(writer, attrs[a_name].value)
126 if len(self.childNodes) == 1 \
127 and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE:
129 self.childNodes[0].writexml(writer,
"",
"",
"")
130 writer.write(
"</%s>%s" % (self.tagName, newl))
132 writer.write(
">%s" % newl)
133 for node
in self.childNodes:
135 if node.nodeType == xml.dom.minidom.Node.TEXT_NODE
and \
136 (
not node.data
or node.data.isspace()):
138 node.writexml(writer, indent + addindent, addindent, newl)
139 writer.write(
"%s</%s>%s" % (indent, self.tagName, newl))
141 writer.write(
"/>%s" % newl)
145 xml.dom.minidom.Element.writexml = fixed_writexml