Undocumented
Function | attrib |
Test attribute handling. |
Function | cdata |
Test CDATA handling (etc). |
Function | check_element |
Undocumented |
Function | check_element_tree |
Undocumented |
Function | check_mapping |
Undocumented |
Function | check_string |
Undocumented |
Function | copy |
Test copy handling (etc). |
Function | element |
Test element tree interface. |
Function | encoding |
Test encoding issues. |
Function | find |
Test find methods (including xpath syntax). |
Function | makeelement |
Test makeelement handling. |
Function | parsefile |
Test parsing from file. Note that we're opening the files in here; by default, the 'parse' function opens the file in binary mode, and doctest doesn't filter out carriage returns. |
Function | qname |
Test QName handling. |
Function | serialize |
Undocumented |
Function | stdout |
Undocumented |
Function | summarize |
Undocumented |
Function | summarize_list |
Undocumented |
Function | unserialize |
Undocumented |
Function | writefile |
No summary |
Constant | ENTITY_XML |
Undocumented |
Constant | SAMPLE_XML |
Undocumented |
Constant | SAMPLE_XML_NS |
Undocumented |
Test attribute handling.
>>> elem = ElementTree.Element("tag") >>> elem.get("key") # 1.1 >>> elem.get("key", "default") # 1.2 'default' >>> elem.set("key", "value") >>> elem.get("key") # 1.3 'value'
>>> elem = ElementTree.Element("tag", key="value") >>> elem.get("key") # 2.1 'value' >>> elem.attrib # 2.2 {'key': 'value'}
>>> elem = ElementTree.Element("tag", {"key": "value"}) >>> elem.get("key") # 3.1 'value' >>> elem.attrib # 3.2 {'key': 'value'}
>>> elem = ElementTree.Element("tag", {"key": "other"}, key="value") >>> elem.get("key") # 4.1 'value' >>> elem.attrib # 4.2 {'key': 'value'}
Test CDATA handling (etc).
>>> serialize(unserialize("<tag>hello</tag>")) '<tag>hello</tag>' >>> serialize(unserialize("<tag>hello</tag>")) '<tag>hello</tag>' >>> serialize(unserialize("<tag><![CDATA[hello]]></tag>")) '<tag>hello</tag>'
Test copy handling (etc).
>>> import copy >>> e1 = unserialize("<tag>hello<foo/></tag>") >>> # e2 = copy.copy(e1) >>> e3 = copy.deepcopy(e1) >>> e1.find("foo").tag = "bar">>> serialize(e1).replace(' ', '') '<tag>hello<bar/></tag>'
## >>> serialize(e2).replace(' ', '') ## '<tag>hello<bar/></tag>'
>>> serialize(e3).replace(' ', '') '<tag>hello<foo/></tag>'
Test element tree interface.
>>> element = ElementTree.Element("tag") >>> check_element(element) >>> tree = ElementTree.ElementTree(element) >>> check_element_tree(tree)
Test encoding issues.
>>> elem = ElementTree.Element("tag") >>> elem.text = u'abc' >>> serialize(elem) '<tag>abc</tag>' >>> serialize(elem, "utf-8") '<tag>abc</tag>' >>> serialize(elem, "us-ascii") '<tag>abc</tag>' >>> serialize(elem, "iso-8859-1").lower() "<?xml version='1.0' encoding='iso-8859-1'?>\n<tag>abc</tag>"
>>> elem.text = "<&\"\'>" >>> serialize(elem) '<tag><&"\'></tag>' >>> serialize(elem, "utf-8") '<tag><&"\'></tag>' >>> serialize(elem, "us-ascii") # cdata characters '<tag><&"\'></tag>' >>> serialize(elem, "iso-8859-1").lower() '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag><&"\'></tag>'
>>> elem.attrib["key"] = "<&\"\'>" >>> elem.text = None >>> serialize(elem) '<tag key="<&"\'>"/>' >>> serialize(elem, "utf-8") '<tag key="<&"\'>"/>' >>> serialize(elem, "us-ascii") '<tag key="<&"\'>"/>' >>> serialize(elem, "iso-8859-1").lower() '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag key="<&"\'>"/>'
>>> elem.text = u'\xe5\xf6\xf6<>' >>> elem.attrib.clear() >>> serialize(elem) '<tag>åöö<></tag>' >>> serialize(elem, "utf-8") '<tag>\xc3\xa5\xc3\xb6\xc3\xb6<></tag>' >>> serialize(elem, "us-ascii") '<tag>åöö<></tag>' >>> serialize(elem, "iso-8859-1").lower() "<?xml version='1.0' encoding='iso-8859-1'?>\n<tag>\xe5\xf6\xf6<></tag>"
>>> elem.attrib["key"] = u'\xe5\xf6\xf6<>' >>> elem.text = None >>> serialize(elem) '<tag key="åöö<>"/>' >>> serialize(elem, "utf-8") '<tag key="\xc3\xa5\xc3\xb6\xc3\xb6<>"/>' >>> serialize(elem, "us-ascii") '<tag key="åöö<>"/>' >>> serialize(elem, "iso-8859-1").lower() '<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>\n<tag key="\xe5\xf6\xf6<>"/>'
Test find methods (including xpath syntax).
>>> elem = SAMPLE_XML >>> elem.find("tag").tag 'tag' >>> ElementTree.ElementTree(elem).find("tag").tag 'tag' >>> elem.find("section/tag").tag 'tag' >>> ElementTree.ElementTree(elem).find("section/tag").tag 'tag' >>> elem.findtext("tag") 'text' >>> elem.findtext("tog", "default") 'default' >>> ElementTree.ElementTree(elem).findtext("tag") 'text' >>> elem.findtext("section/tag") 'subtext' >>> ElementTree.ElementTree(elem).findtext("section/tag") 'subtext' >>> summarize_list(elem.findall("tag")) ['tag', 'tag'] >>> summarize_list(elem.findall("*")) ['tag', 'tag', 'section'] >>> summarize_list(elem.findall(".//tag")) ['tag', 'tag', 'tag'] >>> summarize_list(elem.findall("section/tag")) ['tag'] >>> summarize_list(elem.findall("section//tag")) ['tag'] >>> summarize_list(elem.findall("section/*")) ['tag'] >>> summarize_list(elem.findall("section//*")) ['tag'] >>> summarize_list(elem.findall("section/.//*")) ['tag'] >>> summarize_list(elem.findall("*/*")) ['tag'] >>> summarize_list(elem.findall("*//*")) ['tag'] >>> summarize_list(elem.findall("*/tag")) ['tag'] >>> summarize_list(elem.findall("*/./tag")) ['tag'] >>> summarize_list(elem.findall("./tag")) ['tag', 'tag'] >>> summarize_list(elem.findall(".//tag")) ['tag', 'tag', 'tag'] >>> summarize_list(elem.findall("././tag")) ['tag', 'tag'] >>> summarize_list(ElementTree.ElementTree(elem).findall("/tag")) ['tag', 'tag'] >>> summarize_list(ElementTree.ElementTree(elem).findall("./tag")) ['tag', 'tag'] >>> elem = SAMPLE_XML_NS >>> summarize_list(elem.findall("tag")) [] >>> summarize_list(elem.findall("{http://effbot.org/ns}tag")) ['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag'] >>> summarize_list(elem.findall(".//{http://effbot.org/ns}tag")) ['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag']
Test makeelement handling.
>>> elem = ElementTree.Element("tag") >>> subelem = elem.makeelement("subtag", {"key": "value"}) >>> elem.append(subelem) >>> serialize(elem) '<tag><subtag key="value"/></tag>'
>>> elem.clear() >>> serialize(elem) '<tag/>' >>> elem.append(subelem) >>> serialize(elem) '<tag><subtag key="value"/></tag>'
Test parsing from file. Note that we're opening the files in here; by default, the 'parse' function opens the file in binary mode, and doctest doesn't filter out carriage returns.
>>> file = open("samples/simple.xml", "rb") >>> tree = ElementTree.parse(file) >>> file.close() >>> tree.write(stdout()) <root> <element key="value">text</element> <element>text</element>tail <empty-element/> </root> >>> file = open("samples/simple-ns.xml", "rb") >>> tree = ElementTree.parse(file) >>> file.close() >>> tree.write(stdout()) <root xmlns="http://namespace/"> <element key="value">text</element> <element>text</element>tail <empty-element/> </root>
Test QName handling.
- decorated tags
>>> elem = ElementTree.Element("{uri}tag") >>> serialize(elem) # 1.1 '<ns0:tag xmlns:ns0="uri"/>'
## 2) decorated attributes
## >>> elem.attrib["{uri}key"] = "value" ## >>> serialize(elem) # 2.1 ## '<ns0:tag ns0:key="value" xmlns:ns0="uri"/>'
>>> elem = ElementTree.Element("tag") >>> elem.text = "text" >>> serialize(elem) '<tag>text</tag>' >>> ElementTree.SubElement(elem, "subtag").text = "subtext" >>> serialize(elem) '<tag>text<subtag>subtext</subtag></tag>'
str
=
Undocumented
Value |
|
Undocumented
Value |
|
Undocumented
Value |
|