1

my xml code fetched over network looks like this

<?xml version='1.0' ?><liverequestresponse><liverequesttime>180</liverequesttime><livemessage></livemessage></liverequestresponse>

and my python minidom code is

import urllib, urllib2, time
from xml.dom.minidom import parse
response = urllib2.urlopen(req)
the_page = response.read() 
#print the_page 
dom = parse(response)
name = dom.getElementsByTagNameNS('liverequestresponse')
print name[0].nodeValue

gives some errors

print the_page

works fine

Or if they are any other libraries which are better than minidom, plz tell me.. I would prefer the one which comes pre-installed on linux

UPDATE

errors

Traceback (most recent call last):
  File "logout.py", line 18, in <module>
    dom = parse(response)
  File "/usr/lib64/python2.7/xml/dom/minidom.py", line 1920, in parse
    return expatbuilder.parse(file)
  File "/usr/lib64/python2.7/xml/dom/expatbuilder.py", line 928, in parse
    result = builder.parseFile(file)
  File "/usr/lib64/python2.7/xml/dom/expatbuilder.py", line 211, in parseFile
    parser.Parse("", True)
xml.parsers.expat.ExpatError: no element found: line 1, column 0
0

2 Answers 2

3

if you use response.read before parse(response) you'll already have read the content of the response. a second call to response.read (which parse is doing) will result in an empty string.

The simplest solution is to just drop the first response.read call. But if you really need the response string for some reason, you could try:

import urllib, urllib2, time
import StringIO
from xml.dom.minidom import parse
response = urllib2.urlopen(req)
the_page = response.read() 
#print the_page 
dom = parse(StringIO.StringIO(the_page))
name = dom.getElementsByTagName('liverequesttime')
text = name[0].firstChild
print text.nodeValue
Sign up to request clarification or add additional context in comments.

3 Comments

it prints none!, I tried dropping response.read too.. its not that important so I commented it and run the script the outout was none
it prints None because the liverequestresponse node has no value. It only contains a child node, which contains a text node which has a value. minidom isn't the most userfriendly xml parsing library. lxml is way better, or also xml.etree is nicer.
That worked, I tried with childnode but it didnt worked! Thanks
1

An approach with lxml, which is being very used in Python lately to parse XML with very good results and performance:

import urllib2
from lxml import etree

with urllib2.urlopen(req) as f:
    xml = etree.parse(f)

xml.find('.//liverequesttime').text

The output of the last line would be: 180

7 Comments

lxml has to be installed are there any inbuilt libraries which are better than minidom?
lxml needs to be installed but it is pre-packaged on a lot of linux distributions, though you always can install it with easy_install
I dont want to take the chances I'm writing a http login client for minimalistic linux, I may have to use this on Arch linux core
Well, it seems you have it available with Arch linux in extra repository
it is in the repos but my script logs in to a server on intranet after which I can access internet
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.