Example #1
0
 def _write2(self, file, node, encoding, namespaces):
     # write XML to file
     tag = node.tag
     if tag is ElementTree.Comment:
         text = node.text.encode(encoding)
         file.write("<!--%s-->" % text)
     elif tag is ElementTree.ProcessingInstruction:
         text = node.text.encode(encoding)
         file.write("<?%s?>" % text)
     else:
         items = node.items()
         xmlns_items = [] # new namespaces in this scope
         try:
             if isinstance(tag, ElementTree.QName) or tag[:1] == "{":
                 tag, xmlns = ElementTree.fixtag(tag, namespaces)
                 if xmlns: xmlns_items.append(xmlns)
         except TypeError:
             ElementTree._raise_serialization_error(tag)
         file.write("<" + ElementTree._encode(tag, encoding))
         if items or xmlns_items:
             items.sort() # lexical order
             for k, v in items:
                 try:
                     if isinstance(k, ElementTree.QName) or k[:1] == "{":
                         k, xmlns = ElementTree.fixtag(k, namespaces)
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     ElementTree._raise_serialization_error(k)
                 try:
                     if isinstance(v, ElementTree.QName):
                         v, xmlns = ElementTree.fixtag(v, namespaces)
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     ElementTree._raise_serialization_error(v)
                 file.write(" %s=\"%s\"" % (ElementTree._encode(k, encoding),
                                            ElementTree._escape_attrib(v, encoding)))
             for k, v in xmlns_items:
                 file.write(" %s=\"%s\"" % (ElementTree._encode(k, encoding),
                                            ElementTree._escape_attrib(v, encoding)))
         if node.text or len(node):
             file.write(">")
             if node.text:
                 text = node.text.encode(encoding)
                 file.write(text)
             for n in node:
                 self._write(file, n, encoding, namespaces)
             file.write("</" + ElementTree._encode(tag, encoding) + ">")
         else:
             file.write(" />")
         for k, v in xmlns_items:
             del namespaces[v]
     if node.tail:
         tail = node.tail.encode(encoding)
         file.write(tail)
Example #2
0
 def _write(self, file, node, encoding, namespaces):
     # write XML to file
     tag = node.tag
     if tag is ET.Comment:
         file.write("<!-- %s -->" % ET._escape_cdata(node.text, encoding))
     elif tag is ET.ProcessingInstruction:
         file.write("<?%s?>" % ET._escape_cdata(node.text, encoding))
     else:
         items = node.items()
         xmlns_items = []  # new namespaces in this scope
         try:
             if isinstance(tag, ET.QName) or tag[:1] == "{":
                 tag, xmlns = ET.fixtag(tag, namespaces)
                 if xmlns: xmlns_items.append(xmlns)
         except TypeError:
             ET._raise_serialization_error(tag)
         file.write("<" + ET._encode(tag, encoding))
         if items or xmlns_items:
             items.sort()  # lexical order
             for k, v in items:
                 try:
                     if isinstance(k, ET.QName) or k[:1] == "{":
                         k, xmlns = ET.fixtag(k, namespaces)
                         if xmlns: ET.xmlns_items.append(xmlns)
                 except TypeError:
                     ET._raise_serialization_error(k)
                 try:
                     if isinstance(v, ET.QName):
                         v, xmlns = ET.fixtag(v, namespaces)
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     ET._raise_serialization_error(v)
                 file.write(
                     " %s=\'%s\'" %
                     (ET._encode(k, encoding), _escape_attrib(v, encoding)))
             for k, v in xmlns_items:
                 file.write(
                     " %s=\'%s\'" %
                     (ET._encode(k, encoding), _escape_attrib(v, encoding)))
         if node.text or len(node):
             file.write(">")
             if node.text:
                 file.write(ET._escape_cdata(node.text, encoding))
             for n in node:
                 self._write(file, n, encoding, namespaces)
             file.write("</" + ET._encode(tag, encoding) + ">")
         else:
             file.write(" />")
         for k, v in xmlns_items:
             del namespaces[v]
     if node.tail:
         file.write(ET._escape_cdata(node.tail, encoding))
Example #3
-1
def custom_xml_write(self, file, node, encoding, namespaces, indentation='\n'):
    """
    Custom write function based on ElementTree.ElementTree._write only for python 2.6
    Basically it does the same but writes each attribute in a different line
    The same was done with custom_serialize_xml for python 2.7
    """
    tag = node.tag
    next_indentation = node.tail
    if tag is pyET.Comment:
        file.write("<!-- %s -->" % pyET._escape_cdata(node.text, encoding))
    elif tag is pyET.ProcessingInstruction:
        file.write("<?%s?>" % pyET._escape_cdata(node.text, encoding))
    else:
        items = node.items()
        xmlns_items = []
        try:
            if isinstance(tag, pyET.QName) or tag[:1] == "{":
                tag, xmlns = pyET.fixtag(tag, namespaces)
                if xmlns: xmlns_items.append(xmlns)
        except TypeError:
            pyET._raise_serialization_error(tag)
        file.write("<" + pyET._encode(tag, encoding))
        if items or xmlns_items:
            items.sort()
            for k, v in items:
                try:
                    if isinstance(k, pyET.QName) or k[:1] == "{":
                        k, xmlns = pyET.fixtag(k, namespaces)
                        if xmlns: xmlns_items.append(xmlns)
                except TypeError:
                    pyET._raise_serialization_error(k)
                try:
                    if isinstance(v, pyET.QName):
                        v, xmlns = pyET.fixtag(v, namespaces)
                        if xmlns: xmlns_items.append(xmlns)
                except TypeError:
                    pyET._raise_serialization_error(v)
                file.write("%s\t\t%s=\"%s\"" % (indentation, pyET._encode(k, encoding),
                                                pyET._escape_attrib(v, encoding)))
            for k, v in xmlns_items:
                file.write("%s\t\t%s=\"%s\"" % (indentation, pyET._encode(k, encoding),
                                                pyET._escape_attrib(v, encoding)))
        if node.text or len(node):
            file.write(">")
            if node.text:
                file.write(pyET._escape_cdata(node.text, encoding))
            for n in node:
                self._write(file, n, encoding, namespaces, next_indentation)
            file.write("</" + pyET._encode(tag, encoding) + ">")
        else:
            file.write(" />")
        for k, v in xmlns_items:
            del namespaces[v]
    if node.tail:
        file.write(pyET._escape_cdata(node.tail, encoding))