string manipulation.

G

gerardob

I am trying to read an xml using minidom from python library xml.dom

This is the xml file:
---------------------------------
<rm_structure>
<resources>
<resource>
AB
<Capacity>100</Capacity>
<NumberVirtualClasses>
2
</NumberVirtualClasses>
</resource>
</resources>
</rm_structure>
----------------------------------
This is the python code:
--------------------------------
from xml.dom import minidom
doc= minidom.parse("example.xml")
resources_section = doc.getElementsByTagName('resources')
list_resources = resources_section[0].getElementsByTagName('resource')

for r in list_resources:
name = r.childNodes[0].nodeValue
print name
print len(name)
---------------------------------
The problem is that the nodeValue stored in the variable 'name' is not "AB"
(what i want) but instead it is a string that has length of 8 and it seems
it include the tabs and/or other things.
How can i get the string "AB" without the other stuff?
Thanks.
 
N

Neil Cerutti

I am trying to read an xml using minidom from python library xml.dom

This is the xml file:
---------------------------------
<rm_structure>
<resources>
<resource>
AB
<Capacity>100</Capacity>
<NumberVirtualClasses>
2
</NumberVirtualClasses>
</resource>
</resources>
</rm_structure>
----------------------------------
This is the python code:
--------------------------------
from xml.dom import minidom
doc= minidom.parse("example.xml")
resources_section = doc.getElementsByTagName('resources')
list_resources = resources_section[0].getElementsByTagName('resource')

for r in list_resources:
name = r.childNodes[0].nodeValue
print name
print len(name)
---------------------------------
The problem is that the nodeValue stored in the variable 'name' is not "AB"
(what i want) but instead it is a string that has length of 8 and it seems
it include the tabs and/or other things.
How can i get the string "AB" without the other stuff?

Check out the strip member function.

name = r.childNodes[0].nodeValue.strip()
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top