Lamaizm... XML problem...

D

durumdara

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
 
S

Stefan Behnel

durumdara said:
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()
[/QUOTE]
<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>[/QUOTE]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top