473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

xml.sax problem, help needed.

I've written an xml parser using xml.sax which works most of the time
but often traces back when trying to read a file. The output of the
traceback is below:

Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py",
line 271, in run
main()
File "/usr/lib/python2.4/site-packages/cherrypy/_cphttptools.py",
line 502, in main
body = page_handler(*args, **cherrypy.request.paramMap)
File "netscan.py", line 160, in index
parse()
File "netscan.py", line 117, in parse
parser.parse ( scan_results )
File "/usr/lib/python2.4/xml/sax/expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/usr/lib/python2.4/xml/sax/xmlreader.py", line 125, in parse
self.close()
File "/usr/lib/python2.4/xml/sax/expatreader.py", line 217, in close
self.feed("", isFinal = 1)
File "/usr/lib/python2.4/xml/sax/expatreader.py", line 211, in feed
self._err_handler.fatalError(exc)
File "/usr/lib/python2.4/xml/sax/handler.py", line 38, in fatalError
raise exception
SAXParseException: /var/log/netscan/scanresults.txt:8:0: no element found
I don't understand why it's telling me that no element is found. It
looks like a problem inside xml.sax, but I'm not sure if I've caused
it or how. The xml file is good and is generated by nmap, it's not
missing tags or anything and is quite small. My script code which has
generated this is below:
#!/usr/bin/env python

import xml.sax
import sys
import os

dir = '/var/log/netscan'
scan = 'scanresults.txt'
temp = 'tempscan.txt'

scan_results = dir + '/' + scan
temp_results = dir + '/' + temp

if not os.path.isdir(dir):
sys.exit("%s does not exist! exiting..." % dir)

network = [
# {
# "status" : "",
# "address" : "",
# "hostname" : "",
# "port[0]" : "",
# "protocol[0]" : "",
# "service[0]" : "",
# "state[0]" : "",
# "product[0]" : "",
# "version[0]" : "",
# "extrainfo[0]" : ""
# }
]

class scanparser( xml.sax.ContentHandler ):
def __init__(self):
self.host = {}
self.host['status'] = ""
self.host['address'] = ""
self.host['hostname'] = ""
self.host['port'] = []
self.host['protocol'] = []
self.host['service'] = []
self.host['state'] = []
self.host['product'] = []
self.host['version'] = []
self.host['extrainfo'] = []
def startElement(self,name,attributes):
global scan_start,scan_stop
if name =='nmaprun':
scan_start = attributes.getValue('startstr')
elif name == 'finished':
scan_stop = attributes.getValue('timestr')
elif name =='status':
self.host['status'] = attributes.getValue('state')

elif name == 'address':
if attributes.getValue('addrtype') == 'ipv4':
self.host['address'] = attributes.getValue('addr')

elif name == 'hostname':
self.host['hostname'] = attributes.getValue('name')

elif name == 'port':
self.host['port'].append( attributes.getValue('portid') )
self.host['protocol'].append( attributes.getValue('protocol') )

elif name == 'service':
self.host['service'].append( attributes.getValue('name') )
if attributes.has_key('product'):
self.host['product'].append( attributes.getValue('product') )
else:
self.host['product'].append("")
if attributes.has_key('version'):
self.host['version'].append( attributes.getValue('version') )
else:
self.host['version'].append('')
if attributes.has_key('extrainfo'):
self.host['extrainfo'].append(
attributes.getValue('extrainfo') )
else:
self.host['extrainfo'].append('')

elif name == 'state':
self.host['state'].append( attributes.getValue('state') )
def endElement(self,name):
if name == 'host':
network.append(self.host.copy())
self.host = {}
self.host['status'] = ""
self.host['address'] = ""
self.host['hostname'] = ""
self.host['port'] = []
self.host['protocol'] = []
self.host['service'] = []
self.host['state'] = []
self.host['product'] = []
self.host['version'] = []
self.host['extrainfo'] = []
def parse():
global network
parser = xml.sax.make_parser()
parser.setContentHandler( scanparser() )
network = []
parser.parse ( scan_results )

parse()

(Well, really it runs from cherrypy but for brevity I've left that out)
Any help or pointers anybody could give would be greatly appreciated...
Hari
Aug 1 '06 #1
0 1300

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: s a n j a y | last post by:
Hi All, I am want to upgrade MySQL from 4.0 to 4.1 on linux. The reason being the subqueries. Problem is Failed dependencies: libcrypto.so.0.9.6 is needed by MySQL-Max-4.1.0-0...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
6
by: kabrit | last post by:
Entrez "aide" pour afficher à nouveau les noms de produits. Entrez "quitter" pour quitter. *********************************************************** DB2.PE IBM_db2msen81... Preparing......
3
by: StBond | last post by:
Hi everyone, I am new to Access and Visual Basic so things my be getting across a bit cloudy. I only started using VB for one week. I am having a little problem with the database that I am...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
1
by: Nacho | last post by:
Hello.. I have one problem with my reusable function to validate date.. I want to use this function to all my web project no validate date, but It's works fine if I have one textbox to...
12
by: Lars Eighner | last post by:
I take this example from a writer in alt.usage.english <news://r3jie3p93s1eaflgcckn2hinf3li4mnfud@4ax.com>, where the question of whether the "and" is required came up strictly as an issue of...
37
by: C_guy | last post by:
Does anyone know of a (hopefully free) tool that can traverse a project and determine which "#include"s are not needed or needed in every .C file? This would be helpful in removing header...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.