Parse xml from file using etree works when reading string, but not a file

前端 未结 2 896
萌比男神i
萌比男神i 2021-01-27 00:58

I am a relative newby to Python and SO. I have an xml file from which I need to extract information. I\'ve been struggling with this for several days, but I think I finally foun

2条回答
  •  长发绾君心
    2021-01-27 01:36

    Your XML file uses a default namespace. You need to qualify your searches with the correct namespace:

    identifier = node.findtext('{http://www.eol.org/transfer/content/0.3}identifier')
    

    for ElementTree to match the correct elements.

    You could also give the .find(), findall() and iterfind() methods an explicit namespace dictionary. This is not documented very well:

    namespaces = {'eol': 'http://www.eol.org/transfer/content/0.3'} # add more as needed
    
    root.findall('eol:identifier', namespaces=namespaces)
    

    Prefixes are only looked up in the namespaces parameter you pass in. This means you can use any namespace prefix you like; the API splits off the eol: part, looks up the corresponding namespace URL in the namespaces dictionary, then changes the search to look for the XPath expression {http://www.eol.org/transfer/content/0.3}identifier instead.

    If you can switch to the lxml library things are better; that library supports the same ElementTree API, but collects namespaces for you in a .nsmap attribute on elements.

提交回复
热议问题