Lamaizm... XML problem...

Discussion in 'Python' started by durumdara, Oct 9, 2007.

  1. durumdara

    durumdara Guest

    Hi!

    Something makes me crazy!!!
    I wanna read some XML, but everytime I got "None" for the value of prop:

    The code is:

    from xml.dom import minidom
    import sys

    ResultList = []

    def LoadProps(PropTag):
    print PropTag
    t_forms = PropTag.getElementsByTagName('form')
    for t_form in t_forms:
    t_comps = t_form.getElementsByTagName('component')
    for t_comp in t_comps:
    t_props = t_comp.getElementsByTagName('prop')
    for t_prop in t_props:
    attrs = t_prop.attributes.keys()
    print attrs
    print t_prop.nodeName
    print t_prop.nodeType
    print [t_prop.nodeValue]
    sys.exit()


    doc = minidom.parse('c:\\teszt3.xml')
    print doc
    t_langfile = doc.documentElement
    t_props = doc.getElementsByTagName('properties')[0]
    t_constants = doc.getElementsByTagName('constants')[0]
    LoadProps(t_props)

    -------------------
    The result is:

    >>>

    <xml.dom.minidom.Document instance at 0x0172A3C8>
    <DOM Element: properties at 0x1739378>
    [u'id', u'name']
    prop
    1
    [None]
    >>>

    -------------------

    The source file is:

    <?xml version="1.0" encoding="windows-1250"?>
    <langfile>
    <properties>
    <form>
    <component>
    <prop id="aaaa" name ="bbbb">cccc</prop>
    </component>
    </form>
    </properties>
    <constants>
    </constants>
    </langfile>
    -------------------
    I can get the attrs, but I can't get nodeValue (cccc)... I got None for it.

    Why???

    Please help me a little!!! I'm sure that I miss st, but I don't know
    what is that... :-(

    Thanks for it:
    dd
     
    durumdara, Oct 9, 2007
    #1
    1. Advertising

  2. durumdara wrote:
    > from xml.dom import minidom

    [...]
    > t_props = t_comp.getElementsByTagName('prop')
    > for t_prop in t_props:
    > attrs = t_prop.attributes.keys()
    > print attrs
    > print t_prop.nodeName
    > print t_prop.nodeType
    > print [t_prop.nodeValue]
    > sys.exit()
    > -------------------
    > The result is:
    >
    >>>>

    > <xml.dom.minidom.Document instance at 0x0172A3C8>
    > <DOM Element: properties at 0x1739378>
    > [u'id', u'name']
    > prop
    > 1
    > [None]
    >>>>

    > -------------------
    >
    > The source file is:
    >
    > <?xml version="1.0" encoding="windows-1250"?>
    > <langfile>
    > <properties>
    > <form>
    > <component>
    > <prop id="aaaa" name ="bbbb">cccc</prop>
    > </component>
    > </form>
    > </properties>
    > <constants>
    > </constants>
    > </langfile>
    > -------------------
    > I can get the attrs, but I can't get nodeValue (cccc)... I got None for it.


    The W3C DOM treats text as nodes, so you have to check the children of t_prop
    to find the text node and then read its nodeValue.

    Alternatively, consider using an XML library that actually helps users in
    working with XML, such as ElementTree or lxml.

    http://codespeak.net/lxml

    Stefan
     
    Stefan Behnel, Oct 9, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bomb Diggy
    Replies:
    0
    Views:
    448
    Bomb Diggy
    Jul 28, 2004
  2. Tony Prichard
    Replies:
    0
    Views:
    739
    Tony Prichard
    Dec 12, 2003
  3. Mark Van Orman

    embedding xml in xml as non-xml :)

    Mark Van Orman, Sep 14, 2004, in forum: XML
    Replies:
    5
    Views:
    481
    Patrick TJ McPhee
    Sep 15, 2004
  4. jkflens
    Replies:
    2
    Views:
    1,497
    jkflens
    May 30, 2006
  5. Erik Wasser
    Replies:
    5
    Views:
    464
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page