def xml_to_strelem(dom_node, xml_space="preserve"): if dom_node is None: return StringElem() if isinstance(dom_node, basestring): dom_node = etree.fromstring(dom_node) normalize_xml_space(dom_node, xml_space, remove_start=True) result = StringElem() if dom_node.text: result.sub.append(StringElem(unicode(dom_node.text))) for child_dom_node in dom_node: result.sub.append(make_placeable(child_dom_node, xml_space)) if child_dom_node.tail: result.sub.append(StringElem(unicode(child_dom_node.tail))) result.prune() return result
def xml_to_strelem(dom_node, xml_space="preserve"): if dom_node is None: return StringElem() if isinstance(dom_node, basestring): dom_node = etree.fromstring(dom_node) normalize_xml_space(dom_node, xml_space, remove_start=True) result = StringElem() sub = result.sub # just an optimisation for child_dom_node in dom_node: sub.append(make_placeable(child_dom_node, xml_space)) if child_dom_node.tail: sub.append(StringElem(unicode(child_dom_node.tail))) # This is just a strange way of inserting the first text and avoiding a # call to .prune() which is very expensive. We assume the tree is optimal. node_text = dom_node.text if sub and node_text: sub.insert(0, StringElem(unicode(node_text))) elif node_text: sub.append(unicode(node_text)) return result
def xml_to_strelem(dom_node, xml_space="preserve"): if dom_node is None: return StringElem() if isinstance(dom_node, six.string_types): dom_node = etree.fromstring(dom_node) normalize_xml_space(dom_node, xml_space, remove_start=True) result = StringElem() sub = result.sub # just an optimisation for child_dom_node in dom_node: if child_dom_node.tag is etree.Comment: continue sub.append(make_placeable(child_dom_node, xml_space)) if child_dom_node.tail: sub.append(StringElem(six.text_type(child_dom_node.tail))) # This is just a strange way of inserting the first text and avoiding a # call to .prune() which is very expensive. We assume the tree is optimal. node_text = dom_node.text if sub and node_text: sub.insert(0, StringElem(six.text_type(node_text))) elif node_text: sub.append(six.text_type(node_text)) return result
def xml_to_strelem(dom_node, xml_space="preserve"): if dom_node is None: return StringElem() if isinstance(dom_node, six.string_types): parser = etree.XMLParser(resolve_entities=False) dom_node = etree.fromstring(dom_node, parser) normalize_xml_space(dom_node, xml_space, remove_start=True) result = StringElem() sub = result.sub # just an optimisation for child_dom_node in dom_node: if child_dom_node.tag is etree.Comment: continue sub.append(make_placeable(child_dom_node, xml_space)) if child_dom_node.tail: sub.append(StringElem(six.text_type(child_dom_node.tail))) # This is just a strange way of inserting the first text and avoiding a # call to .prune() which is very expensive. We assume the tree is optimal. node_text = dom_node.text if sub and node_text: sub.insert(0, StringElem(six.text_type(node_text))) elif node_text: sub.append(six.text_type(node_text)) return result