parse .xml with prefix's on tags? xml.etree.ElementTree

前端 未结 2 1677
无人及你
无人及你 2021-01-06 09:59

I can read tags, except when there is a prefix. I\'m not having luck searching SO for a previous question.

I need to read media:content. I tried i

相关标签:
2条回答
  • 2021-01-06 10:45

    media is an XML namespace, it has to be defined somewhere earlier with xmlns:media="...". See http://lxml.de/xpathxslt.html#namespaces-and-prefixes for how to define xml namespaces for use in XPath expressions in lxml.

    0 讨论(0)
  • 2021-01-06 10:59

    Here's an example of using XML namespaces with ElementTree:

    >>> x = '''\
    <channel xmlns:media="http://www.w3.org/TR/html4/">
      <title>Popular  Photography in the last 1 week</title>
      <item>
        <title>foo</title>
        <media:category label="Miscellaneous">photography/misc</media:category>
        <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/>
      </item>
      <item> ... </item>
    </channel>
    '''
    >>> node = ElementTree.fromstring(x)
    >>> for elem in node.findall('item/{http://www.w3.org/TR/html4/}category'):
            print elem.text
    
    
    photography/misc
    
    0 讨论(0)
提交回复
热议问题