我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用lxml.etree.tounicode()。
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
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
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>'
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
def get_reply_msg (self): return etree.tounicode(self.reply)
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)
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
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
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)
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'
def post_processing(self, translator): return etree.tounicode(translator.translation, pretty_print=True)
def get_xml_reply(self): return etree.tounicode(self.reply)
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)
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)