Easy question on minidom

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dean Card

    #1

    Easy question on minidom

    I am using minidom to parse a 20,000 line XML file. I have a few instances
    where the number of child nodes of a particular node can be variable in
    number. To access them I am doing something like the following...

    xmldoc = minidom.parseSt ring(r)
    results = xmldoc.childNod es[0]

    for myNode in results.childNo des[1].childNodes:
    do Stuff with myNode...

    problem is I am having a heck of a time trying to access the value of the
    node. For instance I get things like this

    <DOM Element: PersonName at 0x8391dac>
    <DOM Element: PostalAddress at 0x8391f0c>
    <DOM Element: VoiceNumber at 0x842a18c>
    <DOM Element: VoiceNumber at 0x842a32c>
    <DOM Element: E-mail at 0x842a4cc>

    But I need the values... any help here?


  • Alex Martelli

    #2
    Re: Easy question on minidom

    Dean Card <mailer@forums. com> wrote:
    [color=blue]
    > I am using minidom to parse a 20,000 line XML file. I have a few instances
    > where the number of child nodes of a particular node can be variable in
    > number. To access them I am doing something like the following...
    >
    > xmldoc = minidom.parseSt ring(r)
    > results = xmldoc.childNod es[0]
    >
    > for myNode in results.childNo des[1].childNodes:
    > do Stuff with myNode...
    >
    > problem is I am having a heck of a time trying to access the value of the
    > node. For instance I get things like this
    >
    > <DOM Element: PersonName at 0x8391dac>
    > <DOM Element: PostalAddress at 0x8391f0c>
    > <DOM Element: VoiceNumber at 0x842a18c>
    > <DOM Element: VoiceNumber at 0x842a32c>
    > <DOM Element: E-mail at 0x842a4cc>
    >
    > But I need the values... any help here?[/color]

    Not sure what you mean by "the values" -- if you mean for example the
    contents of text data lying under the node, you can access them...:
    [color=blue][color=green][color=darkred]
    >>> from xml.dom import minidom
    >>> s='<foo><bar>un o</bar><baz>dos</baz></foo>'
    >>> xmldoc = minidom.parseSt ring(s)
    >>> xmldoc.childNod es[0][/color][/color][/color]
    <DOM Element: foo at 0x81238>[color=blue][color=green][color=darkred]
    >>> xmldoc.childNod es[0].childNodes[/color][/color][/color]
    [<DOM Element: bar at 0x812d8>, <DOM Element: baz at 0x818f0>][color=blue][color=green][color=darkred]
    >>> for node in xmldoc.childNod es[0].childNodes: print node[/color][/color][/color]
    ....
    <DOM Element: bar at 0x812d8>
    <DOM Element: baz at 0x818f0>[color=blue][color=green][color=darkred]
    >>> for node in xmldoc.childNod es[0].childNodes: print[/color][/color][/color]
    node.childNodes[0]
    ....
    <DOM Text node "uno">
    <DOM Text node "dos">[color=blue][color=green][color=darkred]
    >>> for node in xmldoc.childNod es[0].childNodes: print[/color][/color][/color]
    node.childNodes[0].data
    ....
    uno
    dos[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    i.e., the text node is the .childNodes[0] (or more elegantly the
    ..firstChild, of course) of the element node containing it, and if you
    need that text node's text data, that's its .data attribute.

    If you need something else, I suggest you post a small example (like
    mine here) with a clear explanation of what exactly it is that you need!


    Alex

    Comment

    Working...