Parsing XML with namespace in Python via 'ElementTree'

前端 未结 6 1682
臣服心动
臣服心动 2020-11-21 09:48

I have the following XML which I want to parse using Python\'s ElementTree:



        
6条回答
  •  长情又很酷
    2020-11-21 10:34

    To get the namespace in its namespace format, e.g. {myNameSpace}, you can do the following:

    root = tree.getroot()
    ns = re.match(r'{.*}', root.tag).group(0)
    

    This way, you can use it later on in your code to find nodes, e.g using string interpolation (Python 3).

    link = root.find(f"{ns}link")
    

提交回复
热议问题