[New-bugs-announce] [issue46798] xml.etree.ElementTree: get() doesn't return default value, always ATTLIST value

padremayi report at bugs.python.org
Sat Feb 19 06:31:31 EST 2022


New submission from padremayi <francesco.other at gmail.com>:

XML test file:

<?xml version="1.0"?>
<!DOCTYPE main [
<!ELEMENT main (object+)>
    <!ELEMENT object (description, year, manufacturer)>
        <!ATTLIST object name CDATA #REQUIRED>
        <!ATTLIST object works (yes|no) "yes">
        <!ELEMENT description (#PCDATA)>
        <!ELEMENT year (#PCDATA)>
        <!ELEMENT manufacturer (#PCDATA)>
]>

<main>
    <object name="My object">
        <description>This is a simple object</description>
        <year>2022</year>
        <manufacturer>Myself</manufacturer>
    </object>
</main>


Python code:
import xml.etree.ElementTree


try:
    xml_data = xml.etree.ElementTree.iterparse("test.xml", events=("start", "end"))

    for event, xml_tag in xml_data:
        if event == "end" and xml_tag.tag == "object":
            object_name = xml_tag.get("name")
            object_description = xml_tag.find("description").text
            works = xml_tag.get("works", default="foo")

            print("works value: " + str(works))

            xml_tag.clear()

    print("Done!")

except (NameError, xml.etree.ElementTree.ParseError):
    print("XML error!")


Output:
works value: yes
Done!


Expected behaviour:
works value: foo
Done!

----------
components: XML
messages: 413543
nosy: padremayi
priority: normal
severity: normal
status: open
title: xml.etree.ElementTree: get() doesn't return default value, always ATTLIST value
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46798>
_______________________________________


More information about the New-bugs-announce mailing list