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))
 
  111 _WRITE_DATA_EXTRA_KWARGS = dict(attr=
True)
 
  113     xml.dom.minidom._write_data(
None, 
None, attr=
True)
 
  115     _WRITE_DATA_EXTRA_KWARGS = {}
 
  123     writer.write(indent + 
"<" + self.tagName)
 
  125     attrs = self._get_attributes()
 
  126     a_names = sorted(attrs.keys())
 
  128     for a_name 
in a_names:
 
  129         writer.write(
" %s=\"" % a_name)
 
  130         xml.dom.minidom._write_data(writer, attrs[a_name].value, **_WRITE_DATA_EXTRA_KWARGS)
 
  133         if len(self.childNodes) == 1 \
 
  134            and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE:
 
  136             self.childNodes[0].writexml(writer, 
"", 
"", 
"")
 
  137             writer.write(
"</%s>%s" % (self.tagName, newl))
 
  139         writer.write(
">%s" % newl)
 
  140         for node 
in self.childNodes:
 
  142             if node.nodeType == xml.dom.minidom.Node.TEXT_NODE 
and \
 
  143                     (
not node.data 
or node.data.isspace()):
 
  145             node.writexml(writer, indent + addindent, addindent, newl)
 
  146         writer.write(
"%s</%s>%s" % (indent, self.tagName, newl))
 
  148         writer.write(
"/>%s" % newl)
 
  152 xml.dom.minidom.Element.writexml = fixed_writexml