Problem with pyXML DOM

  • Thread starter Florian Lindner
  • Start date
F

Florian Lindner

Hello,
I'm using the PyXML Package.

My XML document looks like that:

<visConf>
<graph name="testgraph1" type="chart">
<source type="CSV">
<filename>/home/florian/visualizer/testdata.csv</filename>
</source>

</graph>
</visConf>

print sourceNode.getElementsByTagName("filename")[0]
print sourceNode.getElementsByTagName("filename")[0].nodeValue()

gives:

<Element Node at b7af74ac: Name='filename' with 0 attributes and 1 children>

Traceback (most recent call last):
File "ConfigReader.py", line 40, in ?
c = ConfigReader(f)
File "ConfigReader.py", line 32, in __init__
print sourceNode.getElementsByTagName("filename")[0].nodeValue()
TypeError: 'NoneType' object is not callable

Given only the first call everything seems fine, I have a Node object for
the filename.
But why does the second call fails?

The documentationhttp://pyxml.sourceforge.net/topics/howto/node20.html says:


nodeValue
Value of this node. For some types of node, such as Text nodes, the value is
a string containing a chunk of textual data; for others, such as Text, the
value is just None.

Which I don't really understand? What is differencee between the two text
nodes??

Thanks,

Florian
 
M

Maniac

Florian said:
Traceback (most recent call last):
File "ConfigReader.py", line 40, in ?
c = ConfigReader(f)
File "ConfigReader.py", line 32, in __init__
print sourceNode.getElementsByTagName("filename")[0].nodeValue()
TypeError: 'NoneType' object is not callable
This is because nodeValue here is 'None' and 'None()' doesn't make
sense. Looks like you want the thing between <filename> and </filename>.
This is not a nodeValue, this is nodeValue of firstChild of element
'filename'. So this should work:

print sourceNode.getElementsByTagName("filename")[0].firstChild.nodeValue
 
F

Florian Lindner

Maniac said:
Florian said:
Traceback (most recent call last):
File "ConfigReader.py", line 40, in ?
c = ConfigReader(f)
File "ConfigReader.py", line 32, in __init__
print sourceNode.getElementsByTagName("filename")[0].nodeValue()
TypeError: 'NoneType' object is not callable
This is because nodeValue here is 'None' and 'None()' doesn't make
sense. Looks like you want the thing between <filename> and </filename>.
This is not a nodeValue, this is nodeValue of firstChild of element
'filename'. So this should work:

print sourceNode.getElementsByTagName("filename")[0].firstChild.nodeValue

Ok, works perfect. Thanks!

But I don't really understand the logic:

given I have the node A

<filename>path</filename>

A.firstChild.nodeValue == path

How would the second child of A look like? (ok, None in this case) How would
a XML fragment look like that has a second child?

What would be the nodeValue of A?

for me a child of a node is something like

<filename>
<child1 />
<child2 />
</filename>


Thanks,

Florian
 
M

Maniac

Florian said:
But I don't really understand the logic:

given I have the node A

<filename>path</filename>

A.firstChild.nodeValue == path

How would the second child of A look like? (ok, None in this case)
Yes. It's actually required by DOM standard: NodeList (which is returned
from getElementsByTagName) should return 'null' (None in Python) on
unknown index:
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-844377136
How would
a XML fragment look like that has a second child?
Like this for example:
<filename><child/><child/></filename>

or like this:

What would be the nodeValue of A?
It's not related to children. There are several types of nodes:
documents, elements, text, comments, processing instructions (things in
for me a child of a node is something like

<filename>
<child1 />
<child2 />
</filename>
Almost :). Here filename has actually 5 nodes:
1. text node '\n '
2. element node 'child'
3. text node '\n '
4. element node 'child'
5. text node '\n'

In other words: in DOM whitespace counts.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top