parse xml

Sudheer Satyanarayana sudheer.s at sudheer.net
Fri Oct 15 14:43:05 EDT 2010


On 10/15/2010 11:19 PM, kostia wrote:
> I have xml file:
> <?xml version="1.1" encoding="UTF-8"?>
> <root>
>          <n>50000</n>
> </root>
>
> I want to get the value of n (= 50000) inside my python program, I'm
> doing this:
>
> import xml.dom.minidom
> from xml.dom.minidom import Node
> doc = xml.dom.minidom.parseString("boolean_width.xml")
>    
Use parse to parse a file. doc = parse('boolean_width.xml')

> n = doc.getElementsByTagName("root")[0].firstChild.nodeValue.strip()
> print n
>
> and it is failed. How to get the value? Please, help.
>    

Here's a complete program:

import xml.dom.minidom
from xml.dom.minidom import Node, parse

doc = parse('boolean_width.xml')

my_node_list = doc.getElementsByTagName("n")
my_n_node = my_node_list[0]
my_child = my_n_node.firstChild
my_text = my_child.data



-- 
With warm regards,
Sudheer. S
Personal home page - http://sudheer.net | Tech Chorus - 
http://techchorus.net
Web and IT services - http://binaryvibes.co.in



More information about the Python-list mailing list