al insertar elemento se ubica continuo a los primos

FossilOrigin-Name: 3c26c6d8803c30d15f9baa083fc8cfc2ed8993061196f3299162d6a899680735
This commit is contained in:
bit4bit 2021-11-04 01:46:43 +00:00
parent 2b76cf6a10
commit adbd6d0d0e
2 changed files with 23 additions and 2 deletions

View File

@ -88,6 +88,9 @@ class LXMLBuilder:
def append(self, elem, child):
elem.append(child)
def append_next(self, elem, slibing):
elem.addnext(slibing)
def remove(self, elem):
elem.getparent().remove(elem)
@ -223,10 +226,18 @@ class FachoXML:
self.builder.append(current_elem, node)
current_elem = node
# se fuerza la adicion como un nuevo elemento
if append:
node = self.builder.build_from_expression(node_paths[-1])
self.builder.append(parent, node)
node_tag = node_paths[-1]
last_slibing = current_elem
for child in parent.getchildren():
if child.tag == node_tag:
last_slibing = child
node = self.builder.build_from_expression(node_tag)
self.builder.append_next(last_slibing, node)
return node
return current_elem

View File

@ -209,3 +209,13 @@ def test_facho_xml_get_element_attribute():
xml = facho.FachoXML('root')
xml.set_element('./Id', 'mero', code = 'ABC')
assert xml.get_element_attribute('/root/Id', 'code') == 'ABC'
def test_facho_xml_keep_orden_slibing():
xml = facho.FachoXML('root')
xml.find_or_create_element('./A')
xml.find_or_create_element('./B')
xml.find_or_create_element('./C')
xml.find_or_create_element('./B', append=True)
xml.find_or_create_element('./A', append=True)
assert xml.tostring() == '<root><A/><A/><B/><B/><C/></root>'