return the content of the XML document as a byte string suitable for writing
7953
def tobytes(
        self,
        root=None,
        encoding='UTF-8',
        doctype=None,
        canonicalized=True,
        xml_declaration=True,
        pretty_print=True,
        with_comments=True,
    ):
        """"""
        if root is None:
            root = self.root
        if canonicalized == True:
            return self.canonicalized_bytes(root)
        else:
            return etree.tostring(
                root,
                encoding=encoding or self.info.encoding,
                doctype=doctype or self.info.doctype,
                xml_declaration=xml_declaration,
                pretty_print=pretty_print,
                with_comments=with_comments,
            )
594605
def tostring(element):
    """"""
    rv = []

    def serializeElement(element):
        if not hasattr(element, "tag"):
            if element.docinfo.internalDTD:
                if element.docinfo.doctype:
                    dtd_str = element.docinfo.doctype
                else:
                    dtd_str = "<!DOCTYPE %s>" % element.docinfo.root_name
                rv.append(dtd_str)
            serializeElement(element.getroot())

        elif element.tag == comment_type:
            rv.append("<!--%s-->" % (element.text,))

        else:
            # This is assumed to be an ordinary element
            if not element.attrib:
                rv.append("<%s>" % (element.tag,))
            else:
                attr = " ".join(["%s=\"%s\"" % (name, value)
                                 for name, value in element.attrib.items()])
                rv.append("<%s %s>" % (element.tag, attr))
            if element.text:
                rv.append(element.text)

            for child in element:
                serializeElement(child)

            rv.append("</%s>" % (element.tag,))

        if hasattr(element, "tail") and element.tail:
            rv.append(element.tail)

    serializeElement(element)

    return "".join(rv)
633379
def content(self):
        """"""

        if not self.payload:
            return ''

        if self.payload.is_multipart():
            message_bytes = mime_to_bytes(
                self.payload, 0).replace(b'\n', b'\r\n')
            boundary = b'--' + self.payload.get_boundary().encode('utf-8')
            temp = message_bytes.split(boundary)
            temp.pop(0)
            return boundary + boundary.join(temp)
        else:
            content = self.payload.get_payload()
            if isinstance(content, str_cls):
                content = content.encode('utf-8')
            return content
46642
def content(self):
        '''  '''
        if isinstance(self.__report_file, io.StringIO):
            return self.__report_file.getvalue()
        else:
            return ''
323535
def get(self, url, params=None):
        ''
        url = self.escapeUrl(url)

        content = six.BytesIO(self.raw(url, params=params))
        # We need to make sure that the xml fits in available memory before we parse
        # with lxml.objectify.fromstring(), or else it will bomb out.
        # If it is too big, we need to buffer it to disk before we run it through objectify. see #87
        #
        # get length of buffer
        content.seek(0,2)
        contentlen = content.tell()
        content.seek(0)

        MAX_BUFFER_SIZE=1024*1024*200 # 200MB. TODO: find a way to compute this
        if contentlen > MAX_BUFFER_SIZE:
            # xml is too big to parse with lxml.objectify.fromstring()
            contentfile = tempfile.NamedTemporaryFile()
            contentfile.write(content.read())
            o = lxml.objectify.parse(contentfile)
        else:
            o = lxml.objectify.fromstring(content.getvalue())
        if o.tag == 'error':
            JFSError.raiseError(o, url)
        return o
696595
def extract_transmitted_bytes_from_xml_element(element)
      bytes_received = element.xpath('rx').text.to_i * 1024
      bytes_sent = element.xpath('tx').text.to_i * 1024
      [bytes_received, bytes_sent]
    end
1288293
public String writeXmlDocumentAsString(Document document) throws IOException {
        try {
            Transformer tr = transformerFactory.newTransformer();
            tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            tr.setOutputProperty(OutputKeys.INDENT, "yes");
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            TransformerFactory tf = TransformerFactory.newInstance();
            tr.transform(new DOMSource(document), result);
            return writer.toString();
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }
507233
def to_string(self, indent="", newl="", addindent=""):
        '''
        
        '''
        buf = StringIO()
        self.to_xml().writexml(buf, indent=indent, addindent=addindent,
                               newl=newl)
        return buf.getvalue()
199291
def pformat_xml(xml):
    """"""
    try:
        from lxml import etree  # delayed import
        if not isinstance(xml, bytes):
            xml = xml.encode('utf-8')
        xml = etree.parse(io.BytesIO(xml))
        xml = etree.tostring(xml, pretty_print=True, xml_declaration=True,
                             encoding=xml.docinfo.encoding)
        xml = bytes2str(xml)
    except Exception:
        if isinstance(xml, bytes):
            xml = bytes2str(xml)
        xml = xml.replace('><', '>\n<')
    return xml.replace('  ', ' ').replace('\t', ' ')
367370
def toxml(self):
        

        return '<Dimension name="{0}"'.format(self.name) +\
          (' m = "{0}"'.format(self.m) if self.m != 0 else '') +\
          (' l = "{0}"'.format(self.l) if self.l != 0 else '') +\
          (' t = "{0}"'.format(self.t) if self.t != 0 else '') +\
          (' i = "{0}"'.format(self.i) if self.i != 0 else '') +\
          (' k = "{0}"'.format(self.k) if self.k != 0 else '') +\
          (' n = "{0}"'.format(self.n) if self.n != 0 else '') +\
          (' j = "{0}"'.format(self.j) if self.j != 0 else '') +\
          '/>'
1171919
def toxml(self):
        

        xmlstr = '<OnEvent port="{0}"'.format(self.port)

        chxmlstr = ''

        for action in self.actions:
            chxmlstr += action.toxml()

        if chxmlstr:
            xmlstr += '>' + chxmlstr + '</OnEvent>'
        else:
            xmlstr += '/>'

        return xmlstr
972463
def retrieve_xml(pdb_id, silent = True):
    ''''''
    xml_gz = retrieve_file_from_RCSB(get_rcsb_files_connection(), "/download/%s.xml.gz" % pdb_id, silent = silent)
    cf = StringIO.StringIO()
    cf.write(xml_gz)
    cf.seek(0)
    df = gzip.GzipFile(fileobj = cf, mode='rb')
    contents = df.read()
    df.close()
    return contents
1337601
public static String documentToString(Document xmlDocument) throws IOException {
        String encoding = (xmlDocument.getXmlEncoding() == null) ? "UTF-8" : xmlDocument.getXmlEncoding();
        OutputFormat format = new OutputFormat(xmlDocument);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        format.setEncoding(encoding);
        try (Writer out = new StringWriter()) {
            XMLSerializer serializer = new XMLSerializer(out, format);
            serializer.serialize(xmlDocument);
            return out.toString();
        }
    }
1079218
def create_xml_string(self):
        
        root = self.create_xml()

        xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + tostring(
            root, pretty_print=True
        )
        return xml
918523
def write_xml(self):
        '''
            
        '''
        key = None
        if self. language is not None:
            lang = {}
            lang['{http://www.w3.org/XML/1998/namespace}lang'] = self.language
            key = etree.Element('vocabulary-key', attrib=lang)
        else:
            key = etree.Element('vocabulary-key')

        name = etree.Element('name')
        name.text = self.name
        key.append(name)

        if self.family is not None:
            family = etree.Element('family')
            family.text = self.family
            key.append(family)

        if self.version is not None:
            version = etree.Element('version')
            version.text = self.version
            key.append(version)

        if self.code_value is not None:
            code_value = etree.Element('code-value')
            code_value.text = self.code_value
            key.append(code_value)

        return key
160345
def to_xml(ele, encoding="UTF-8", pretty_print=False):
    ""
    xml = etree.tostring(ele, encoding=encoding, pretty_print=pretty_print)
    if sys.version < '3':
        return xml if xml.startswith('<?xml') else '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml)
    else:
        return xml.decode('UTF-8') if xml.startswith(b'<?xml') \
            else '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml.decode('UTF-8'))
229662
def xml(self):
        
        root = Element('UsernameToken', ns=wssens)
        u = Element('Username', ns=wssens)
        u.setText(self.username)
        root.append(u)
        p = Element('Password', ns=wssens)
        p.setText(self.password)
        if self.password_digest:
            p.set("Type", wsdigest)
            p.setText(self.password_digest)
        root.append(p)
        if self.nonce is not None:
            n = Element('Nonce', ns=wssens)
            if self.nonce_has_encoding:
                n.set("EncodingType", nonce_encoding_type)
            n.setText(self.nonce)
            root.append(n)
        if self.created is not None:
            n = Element('Created', ns=wsuns)
            n.setText(str(DateTime(self.created)))
            root.append(n)
        return root