Python ElementTree unable to parse xml file correctly -
i trying parse xml file using elementree of python. xml file below:
<app xmlns="test attribute"> <name>sagar</name> </app>
parser code:
from xml.etree.elementtree import elementtree xml.etree.elementtree import element import xml.etree.elementtree etree def parser(): eletree = etree.parse('app.xml') eleroot = eletree.getroot() print("tag:"+str(eleroot.tag)+"\nattrib:"+str(eleroot.attrib)) if __name__ == "__main__": parser()
output:
[sagar@linux parser]$ python test.py tag:{test attribute}app <------------- should print "app" attrib:{}
when remove "xmlns" attribute or rename "xmlns" attribute else eleroot.tag printing correct value. why can't element tree unable parse tags when have "xmlns" attribute in tag. missing pre-requisite parse xml of format using element tree?
your xml uses attribute xmlns
, trying define default xml namespace. xml namespaces used solve naming conflicts, , require valid uri value, such value of "test attribute"
invalid, appears troubling parsing of xml etree
.
for more information on xml namespaces see xml namespaces @ w3 schools.
edit:
after looking issue further appears qualified name of element when using python's elementtree
has form {namespace_url}tag_name
. means that, defined default namespace of "test attribute", qualified name of "app" tag infact {test attribute}app
, you're getting out of program.
Comments
Post a Comment