diff options
Diffstat (limited to 'Doc/library/xml.etree.elementtree.rst')
-rw-r--r-- | Doc/library/xml.etree.elementtree.rst | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/Doc/library/xml.etree.elementtree.rst b/Doc/library/xml.etree.elementtree.rst index 15325c2..20cfc4c 100644 --- a/Doc/library/xml.etree.elementtree.rst +++ b/Doc/library/xml.etree.elementtree.rst @@ -129,7 +129,7 @@ As an :class:`Element`, ``root`` has a tag and a dictionary of attributes:: It also has children nodes over which we can iterate:: >>> for child in root: - ... print child.tag, child.attrib + ... print child.tag, child.attrib ... country {'name': 'Liechtenstein'} country {'name': 'Singapore'} @@ -148,7 +148,7 @@ the sub-tree below it (its children, their children, and so on). For example, :meth:`Element.iter`:: >>> for neighbor in root.iter('neighbor'): - ... print neighbor.attrib + ... print neighbor.attrib ... {'name': 'Austria', 'direction': 'E'} {'name': 'Switzerland', 'direction': 'W'} @@ -162,9 +162,9 @@ with a particular tag, and :attr:`Element.text` accesses the element's text content. :meth:`Element.get` accesses the element's attributes:: >>> for country in root.findall('country'): - ... rank = country.find('rank').text - ... name = country.get('name') - ... print name, rank + ... rank = country.find('rank').text + ... name = country.get('name') + ... print name, rank ... Liechtenstein 1 Singapore 4 @@ -188,9 +188,9 @@ Let's say we want to add one to each country's rank, and add an ``updated`` attribute to the rank element:: >>> for rank in root.iter('rank'): - ... new_rank = int(rank.text) + 1 - ... rank.text = str(new_rank) - ... rank.set('updated', 'yes') + ... new_rank = int(rank.text) + 1 + ... rank.text = str(new_rank) + ... rank.set('updated', 'yes') ... >>> tree.write('output.xml') @@ -226,9 +226,9 @@ We can remove elements using :meth:`Element.remove`. Let's say we want to remove all countries with a rank higher than 50:: >>> for country in root.findall('country'): - ... rank = int(country.find('rank').text) - ... if rank > 50: - ... root.remove(country) + ... rank = int(country.find('rank').text) + ... if rank > 50: + ... root.remove(country) ... >>> tree.write('output.xml') @@ -887,6 +887,7 @@ Example of changing the attribute "target" of every link in first paragraph:: [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>] >>> for i in links: # Iterates through all found links ... i.attrib["target"] = "blank" + ... >>> tree.write("output.xhtml") .. _elementtree-qname-objects: |