Python lxml.etree 模块,tounicode() 实例源码

我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用lxml.etree.tounicode()

项目:PySIGNFe    作者:thiagopena    | 项目源码 | 文件源码
def _le_xml(self, arquivo):
        if arquivo is None:
            return False

        if not isinstance(arquivo, basestring):
            arquivo = etree.tounicode(arquivo)

        if arquivo is not None:
            if isinstance(arquivo, basestring): 
                if NAMESPACE_NFSE in arquivo:
                    arquivo = por_acentos(arquivo)
                if u'<' in arquivo:
                    self._xml = etree.fromstring(tira_abertura(arquivo))
                else:
                    arq = open(arquivo)
                    txt = ''.join(arq.readlines())
                    txt = tira_abertura(txt)
                    arq.close()
                    self._xml = etree.fromstring(txt)
            else:
                self._xml = etree.parse(arquivo)
            return True

        return False
项目:inxs    作者:funkyfuture    | 项目源码 | 文件源码
def test_remove_element_text_preservation():
    fragment = etree.fromstring(
        '<root><hi>Published in</hi> <docDate>late <hi>February</hi> 1848</docDate><lb/>.</root>'
    )

    lb = lxml_utils.find(fragment, 'lb')
    lxml_utils.remove_elements(lb)
    result = etree.tounicode(fragment)
    assert result == '<root><hi>Published in</hi> <docDate>late <hi>February</hi>' \
                     ' 1848</docDate></root>', result

    doc_date = lxml_utils.find(fragment, 'docDate')
    lxml_utils.remove_elements(doc_date, keep_children=True,
                               preserve_text=True, preserve_tail=True)
    result = etree.tounicode(fragment)
    assert result == '<root><hi>Published in</hi> late <hi>February</hi> 1848</root>', result

    hi = lxml_utils.find(fragment, 'hi')
    lxml_utils.remove_elements(hi, keep_children=False, preserve_text=True, preserve_tail=True)
    result = etree.tounicode(fragment)
    assert result == '<root>Published in late <hi>February</hi> 1848</root>', result
项目:omb-eregs    作者:18F    | 项目源码 | 文件源码
def test_clean_content():
    aroot = etree.fromstring("""
    <aroot>
        <content> Some text <br />   </content>
        <childA>
            <content>
                More    text
            </content>
        </childA>
        <childB />
    </aroot>
    """)
    preprocess.clean_content(aroot)
    assert aroot.findtext('./childA/content') == 'More    text'
    content_xml = etree.tounicode(aroot.find('./content')).strip()
    assert content_xml == '<content>Some text <br/></content>'
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def build_yang_response(self, root, request, yang_options=None,
                            custom_rpc=False):
        try:
            self.custom_rpc = custom_rpc
            yang_xml = self.to_yang_xml(root, request, yang_options,
                                        custom_rpc)
            log.info('yang-xml', yang_xml=etree.tounicode(yang_xml,
                                                          pretty_print=True))
            return self.build_xml_response(request, yang_xml, custom_rpc)
        except Exception as e:
            log.exception('error-building-yang-response', request=request,
                          xml=etree.tostring(root))
            self.rpc_response.is_error = True
            self.rpc_response.node = ncerror.BadMsg(request)
            return
项目:netconf-proxy    作者:fortinet-solutions-cse    | 项目源码 | 文件源码
def get_reply_msg (self):
        return etree.tounicode(self.reply)
项目:netconf-proxy    作者:fortinet-solutions-cse    | 项目源码 | 文件源码
def send_rpc_reply (self, rpc_reply, origmsg):
        reply = etree.Element(qmap('nc') + "rpc-reply", attrib=origmsg.attrib, nsmap=origmsg.nsmap)
        try:
            rpc_reply.getchildren                           # pylint: disable=W0104
            reply.append(rpc_reply)
        except AttributeError:
            reply.extend(rpc_reply)
        ucode = etree.tounicode(reply, pretty_print=True)
        if self.debug:
            logger.debug("%s: Sending RPC-Reply: %s", str(self), str(ucode))
        self.send_message(ucode)
项目:inxs    作者:funkyfuture    | 项目源码 | 文件源码
def debug_dump_document(name='tree'):
    """ Dumps all contents of the element referenced by ``name`` from the
        :attr:`inxs.Transformation._available_symbols` to the log at info level. """
    def handler(transformation):
        try:
            nfo(etree.tounicode(transformation._available_symbols[name]))
        except KeyError:
            nfo(f"No symbol named '{name}' found.")
        return transformation.states.previous_result
    return handler
项目:inxs    作者:funkyfuture    | 项目源码 | 文件源码
def test_remove_element_text_preservation_part_2():
    fragment = etree.fromstring(
        '<root><a><c/></a><b/>b<d><e/>e</d>d</root>'
    )

    b = lxml_utils.find(fragment, 'b')
    lxml_utils.remove_elements(b, preserve_tail=True)
    result = etree.tounicode(fragment)
    assert result == '<root><a><c/></a>b<d><e/>e</d>d</root>', result

    d = lxml_utils.find(fragment, 'd')
    lxml_utils.remove_elements(d, keep_children=True, preserve_tail=True)
    result = etree.tounicode(fragment)
    assert result == '<root><a><c/></a>b<e/>ed</root>', result
项目:inxs    作者:funkyfuture    | 项目源码 | 文件源码
def test_remove_element_text_preservation_part_3():
    fragment = etree.fromstring(
        '<item><name><hi>Rosa</hi></name>, about Quark.</item>'
    )
    name = lxml_utils.find(fragment, 'name')
    lxml_utils.remove_elements(name, keep_children=True, preserve_text=True, preserve_tail=True)
    assert etree.tounicode(fragment) == '<item><hi>Rosa</hi>, about Quark.</item>', \
        etree.tounicode(fragment)
项目:inxs    作者:funkyfuture    | 项目源码 | 文件源码
def test_replace_text():
    element = etree.Element('x')
    element.text, element.tail = ('foo', 'bar')
    transformation = Transformation()
    transformation.states = SimpleNamespace(previous_result=0)
    lib.replace_text('foo', 'peng')(element, transformation)
    lib.replace_text('bar', 'zack', tail=True)(element, transformation)
    assert etree.tounicode(element) == '<x>peng</x>zack'
项目:napalm-yang    作者:napalm-automation    | 项目源码 | 文件源码
def post_processing(self, translator):
        return etree.tounicode(translator.translation, pretty_print=True)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def get_xml_reply(self):
        return etree.tounicode(self.reply)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def get_xml_reply(self):
        return etree.tounicode(self.reply)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def get_xml_reply(self):
        return etree.tounicode(self.reply)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def get_xml_reply(self):
        return etree.tounicode(self.reply)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def get_xml_reply(self):
        return etree.tounicode(self.reply)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def get_xml_reply(self):
        return etree.tounicode(self.reply)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def send_rpc_reply(self, rpc_reply, origmsg):
        reply = etree.Element(qmap(C.NC) + C.RPC_REPLY, attrib=origmsg.attrib,
                              nsmap=origmsg.nsmap)
        try:
            rpc_reply.getchildren
            reply.append(rpc_reply)
        except AttributeError:
            reply.extend(rpc_reply)
        ucode = etree.tounicode(reply, pretty_print=True)
        log.info("RPC-Reply", reply=ucode)
        self.send_message(ucode)
项目:voltha    作者:opencord    | 项目源码 | 文件源码
def send_custom_rpc_reply(self, rpc_reply, origmsg):
        reply = etree.Element(qmap(C.NC) + C.RPC_REPLY, attrib=origmsg.attrib,
                              nsmap=rpc_reply.nsmap)
        try:
            reply.extend(rpc_reply.getchildren())
        except AttributeError:
            reply.extend(rpc_reply)
        ucode = etree.tounicode(reply, pretty_print=True)
        log.info("Custom-RPC-Reply", reply=ucode)
        self.send_message(ucode)