Python xml.dom, help reading attribute data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thierry Lam

    #1

    Python xml.dom, help reading attribute data

    Let's say I have the following xml tag:

    <para role="success"> 1</para>

    I can't figure out what kind of python xml.dom codes I should invoke to
    read the data 1? Any help please?

    Thanks
    Thierry

  • Jeremy Jones

    #2
    Re: Python xml.dom, help reading attribute data

    Thierry Lam wrote:
    [color=blue]
    >Let's say I have the following xml tag:
    >
    ><para role="success"> 1</para>
    >
    >I can't figure out what kind of python xml.dom codes I should invoke to
    >read the data 1? Any help please?
    >
    >Thanks
    >Thierry
    >
    >
    >[/color]
    In [20]: import xml.dom.minidom

    In [21]: s = '''<para role="success"> 1</para>'''

    In [22]: x = xml.dom.minidom .parseString(s)

    In [23]: print x.firstChild.fi rstChild.data
    1


    I doubt this really answers what you're really wanting to ask. And this
    is a really really brittle way of trying to parse XML. But it answers
    exactly what you're asking. Hope it gives you the start you need. Post
    a follow-up when you have more questions.

    JMJ

    Comment

    • Alan Kennedy

      #3
      Re: Python xml.dom, help reading attribute data

      [Thierry Lam][color=blue]
      > Let's say I have the following xml tag:
      >
      > <para role="success"> 1</para>
      >
      > I can't figure out what kind of python xml.dom codes I should invoke to
      > read the data 1? Any help please?[/color]

      This is job for xpath.

      #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      from xml import xpath
      from xml.dom import minidom

      doc = """<para role="success"> 1</para>"""

      mydom = minidom.parseSt ring(doc)
      #result_nodes = xpath.Evaluate( "/para/text()", mydom)
      #result_nodes = xpath.Evaluate( "/para[1]/text()", mydom)
      result_nodes = xpath.Evaluate( "/para[@role='success']/text()", mydom)
      for ix, r in enumerate(resul t_nodes):
      print "result %d: %s" % (ix, r)
      #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      Xpath support is a part of the PyXML package, which you can get from here



      Xpath tutorials from here


      Sorry! We can't seem to find the resource you're looking for


      there-are-other-ways-to-do-it-but-i-like-xpath-ly'yrs,

      --
      alan kennedy
      ------------------------------------------------------
      email alan: http://xhaus.com/contact/alan

      Comment

      • Giovanni Bajo

        #4
        Re: Python xml.dom, help reading attribute data

        Thierry Lam wrote:
        [color=blue]
        > Let's say I have the following xml tag:
        >
        > <para role="success"> 1</para>
        >
        > I can't figure out what kind of python xml.dom codes I should invoke
        > to read the data 1? Any help please?
        >
        > Thanks
        > Thierry[/color]


        If you use elementtree:
        [color=blue][color=green][color=darkred]
        >>> from elementtree import ElementTree
        >>> node = ElementTree.fro mstring("""<par a role="success"> 1</para>""")
        >>> node.text[/color][/color][/color]
        '1'[color=blue][color=green][color=darkred]
        >>> node.attrib["role"][/color][/color][/color]
        'success'
        --
        Giovanni Bajo


        Comment

        Working...