This is a bit of a tricky one. First, you'll need to get the parent element as described in this previous question.
parent_map = dict((c, p) for p in tree.getiterator() for c in p)
Now, when you get your element from iterating, you can also get the parent:
for elem in list(tree.getiterator('pre')):
parent = parent_map[elem]
wrap_elem(parent, elem)
Finally, you're in position to move the element around:
def wrap_elem(parent, elem)
parent_index = list(parent).index(elem)
parent.remove(elem)
new_elem = ET.Element('div', attrib = {
'class': 'wrapper'
})
parent.insert(parent_index, new_elem)
new_elem.append(elem)
2, add a new subelement div
into parent
:
div = ET.SubElement(parent, 'div')
and set the attrib of div
:
div.set('class', 'wrapper')
3, add the element in step 1 as a subelement of div
,
ET.SubElement(div, temp)
Something like this works for one:
for i, element in enumerate(parent):
if is_the_one_you_want_to_replace(element):
parent.remove(element)
parent.insert(i, new_element)
break
Something like this works for many:
replacement_map = {} for i, element in enumerate(parent): if is_an_element_you_want_to_replace(element): replacement_map[i] = el_to_remove, el_to_add for index, (el_to_remove, el_to_add) in replacement_map.items(): parent.remove(el_to_remove) parent.insert(index, el_to_add)
Another solution that works for me, similar to lyfing's. Copy the element into a temp; retag the original element with the wanted outer tag and clear it, then append the copy into the original.
import copy
temp = copy.deepcopy(elem)
elem.tag = "div"
elem.set("class", "wrapper")
elem.clear()
elem.append(temp)
This is a bit of a tricky one. First, anycodings_python you'll need to get the parent element as anycodings_python described in this previous question.,Here is the steps: Before your anycodings_python iteration, you should get the parent anycodings_python element of these iterated element first, anycodings_python store it into variable parent.,If you can get markdown to use lxml, anycodings_python this is a little easier -- I believe anycodings_python that lxml elements know their parents anycodings_python already.,How can I replace an element during anycodings_python iteration in an elementtree? I'm writing a anycodings_python treeprocessor for markdown and would like to anycodings_python wrap an element.
How can I replace an element during anycodings_python iteration in an elementtree? I'm writing a anycodings_python treeprocessor for markdown and would like to anycodings_python wrap an element.
<pre class='inner'>...</pre>
Should become
<div class='wrapper'>
<pre class='inner'>...</pre>
</div>
This is a bit of a tricky one. First, anycodings_python you'll need to get the parent element as anycodings_python described in this previous question.
parent_map = dict((c, p) for p in tree.getiterator() for c in p)
Now, when you get your element from anycodings_python iterating, you can also get the parent:
for elem in list(tree.getiterator('pre')):
parent = parent_map[elem]
wrap_elem(parent, elem)
Finally, you're in position to move the anycodings_python element around:
def wrap_elem(parent, elem)
parent_index = list(parent).index(elem)
parent.remove(elem)
new_elem = ET.Element('div', attrib = {
'class': 'wrapper'
})
parent.insert(parent_index, new_elem)
new_elem.append(elem)
2, add a new subelement div into parent: anycodings_python
div = ET.SubElement(parent, 'div')
and set the attrib of div:
div.set('class', 'wrapper')
3, add the element in step 1 as a anycodings_python subelement of div,
ET.SubElement(div, temp)
Something like this works for one:
for i, element in enumerate(parent):
if is_the_one_you_want_to_replace(element):
parent.remove(element)
parent.insert(i, new_element)
break
Something like this works for many:
replacement_map = {} for i, element in enumerate(parent): if is_an_element_you_want_to_replace(element): replacement_map[i] = el_to_remove, el_to_add for index, (el_to_remove, el_to_add) in replacement_map.items(): parent.remove(el_to_remove) parent.insert(index, el_to_add)
Another solution that works for me, anycodings_python similar to lyfing's. Copy the element anycodings_python into a temp; retag the original element anycodings_python with the wanted outer tag and clear it, anycodings_python then append the copy into the original.
import copy
temp = copy.deepcopy(elem)
elem.tag = "div"
elem.set("class", "wrapper")
elem.clear()
elem.append(temp)
Once created, an Element object may be manipulated by directly changing its fields (such as Element.text), adding and modifying attributes (Element.set() method), as well as adding new children (for example with Element.append()).,fromstring() parses XML from a string directly into an Element, which is the root element of the parsed tree. Other parsing functions may create an ElementTree. Check the documentation to be sure.,Subelement factory. This function creates an element instance, and appends it to an existing element.,Note that XMLParser skips over comments in the input instead of creating comment objects for them. An ElementTree will only contain comment nodes if they have been inserted into to the tree using one of the Element methods.
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N" />
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W" />
<neighbor name="Colombia" direction="E" />
</country>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
root = ET.fromstring(country_data_as_string)
>>> root.tag 'data' >>>
root.attrib {}
>>>
for child in root:
...print(child.tag, child.attrib)
...
country {
'name': 'Liechtenstein'
}
country {
'name': 'Singapore'
}
country {
'name': 'Panama'
}
>>> root[0][1].text '2008'