parse xml

K

kostia

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")
n = doc.getElementsByTagName("root")[0].firstChild.nodeValue.strip()
print n

and it is failed. How to get the value? Please, help.
 
A

Andreas Waldenburger

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")
n = doc.getElementsByTagName("root")[0].firstChild.nodeValue.strip()
print n

and it is failed. [snip]

How? What's the error message?
 
S

Sudheer Satyanarayana

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
 
M

MRAB

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")
n = doc.getElementsByTagName("root")[0].firstChild.nodeValue.strip()
print n

and it is failed. How to get the value? Please, help.

The 'parseString' method does what it says, it parses a string. You're
giving it the string "boolean_width.xml", so that's what it's parsing.

If you want to parse a file then use the 'parse' method.
 
H

Hidura

First every element represents a node so you have to use
value=n.childNodes[0].nodeValue with that you'll have the 5000

2010/10/15 said:
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")
n = doc.getElementsByTagName("root")[0].firstChild.nodeValue.strip()
print n

and it is failed. How to get the value? Please, help.
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top