473,513 Members | 2,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with xml.parsers.expat please?

There seems to be no XML parser that can do validation in
the Python Standard Libraries. And I am stuck with Python
2.1.1. until my web master upgrades (I use Python for
CGI). I know pyXML has validating parsers, but I can not
compile things on the (unix) webserver. And even if I
could, the compiler I have access to would be different
than what was used to compile python for CGI.

I need to write a CGI script that does XML validation (and
then later also does other things). It does not have to
be complete standards compliant validation but at least it
should check if elements are declared and allowed in
special places in the XML tree.

I tried to understand SAX and DOM but I gave up, and
effbot advises to avoid them anyway. So I am studying
xml.parsers.expat now, but I am stuck.

The program below *does* print information about DOCTYPE
declarations but nothing about the element definitions in
the DTD. I feed it an XML file with a DOCTYPE declaration
like <!DOCTYPE ROOTTAG SYSTEM "MYDTD.DTD"> and the DTD is
in the same directory. I also tried inputting the DTD
itself to this program but that doesn't work either
(ExpatError: syntaxerror at the first element definition).

Please help if you can.


# file: minimal_validate.py
#
import xml.parsers.expat

def element_decl_handler(name, model):
print 'ELEMENT definition: ', name, ' model: ', model

def doctype_decl_handler(doctypeName, systemId, publicId, has_internal_subset):
print 'DOCTYPE declaration: '
print ' doctypeName: ', doctypeName
print ' systemId: ', systemId
print ' publicId:', publicId
print ' internal subset:', has_internal_subset

p = xml.parsers.expat.ParserCreate()

p.ElementDeclHandler = element_decl_handler
p.StartDoctypeDeclHandler = doctype_decl_handler

import sys
input = file(sys.argv[1]).read()
p.Parse(input)
Jul 18 '05 #1
1 6039
Will Stuyvesant wrote:
There seems to be no XML parser that can do validation in
the Python Standard Libraries. And I am stuck with Python
2.1.1. until my web master upgrades (I use Python for
CGI). I know pyXML has validating parsers, but I can not
compile things on the (unix) webserver. And even if I
could, the compiler I have access to would be different
than what was used to compile python for CGI.
So it didn't work out with xmlproc? Isn't xmlproc a pure python
parser that you should be able to drop in and run without
compiling anything?
I need to write a CGI script that does XML validation (and
then later also does other things). It does not have to
be complete standards compliant validation but at least it
should check if elements are declared and allowed in
special places in the XML tree.
I think you would be much more likely to get constructive help
if you posted some examples of the tree structures and data
that you're processing.
I tried to understand SAX and DOM but I gave up, and
effbot advises to avoid them anyway. So I am studying
xml.parsers.expat now, but I am stuck.
SAX and DOM aren't solutions, they're tools. They are simply
different ways to accessing the contents of an XML document.
They may or may not be suitable for your problem, depending
on a wide variety of considerations.

I think the problem needs to be clearly defined before an
appropriate solution can be reached.
The program below *does* print information about DOCTYPE
declarations but nothing about the element definitions in
the DTD. I feed it an XML file with a DOCTYPE declaration
like <!DOCTYPE ROOTTAG SYSTEM "MYDTD.DTD"> and the DTD is
in the same directory. I also tried inputting the DTD
itself to this program but that doesn't work either
(ExpatError: syntaxerror at the first element definition).

Please help if you can.

# file: minimal_validate.py
#
import xml.parsers.expat

def element_decl_handler(name, model):
print 'ELEMENT definition: ', name, ' model: ', model

def doctype_decl_handler(doctypeName, systemId, publicId, has_internal_subset):
print 'DOCTYPE declaration: '
print ' doctypeName: ', doctypeName
print ' systemId: ', systemId
print ' publicId:', publicId
print ' internal subset:', has_internal_subset

p = xml.parsers.expat.ParserCreate()

p.ElementDeclHandler = element_decl_handler
p.StartDoctypeDeclHandler = doctype_decl_handler

import sys
input = file(sys.argv[1]).read()
p.Parse(input)


I think you need to do some reading on what SAX does. In summary, it
gives you the pieces of an XML document, in a series of function
callbacks. You've got to do something with the pieces that you're
given.
SAX won't solve your problem any more than anything else unless you
know what pieces you are receiving, and are doing something with them.

One memory efficient way of building up a document in memory is to
create a python object to represent every element, and with each
"element object" being a (python) attribute of its parent. It's a lot
easier than it sounds, and can be read about here

http://aspn.activestate.com/ASPN/Coo.../Recipe/149368

And you can read about SAX in general here

http://www.devarticles.com/art/1/383/2
http://www-106.ibm.com/developerwork...ipsaxflex.html

The latter is a good example from Uche Ogbuji about extracting pieces
of a document from a SAX stream, which might be easily adaptable to
your
problem.

But I still think you'd be better to describe the problem as simply as
you can here, rather than fumbling around.

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #2

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

Similar topics

2
3905
by: Thomas Guettler | last post by:
Hi! What are the difference between xml.parsers.expat and xml.sax? Up to now I used xml.sax.make_parser and subclass from ContentHandler. I think xml.sax.make_parser uses expat as default....
0
1449
by: dagurp | last post by:
I have this code: import xml.parsers.expat parser = xml.parsers.expat.ParserCreate(encoding="UTF-8") text = unicode("<div>þórður</div>",'UTF-8') print parser.Parse(text,1) And this is what I...
4
3788
by: Laurens | last post by:
Hi, Is there any good open-source C++ XML parser library that isn't as huge as Xerces? The Xerces DLL is about 2.4 Mb in size, which is far too big for my application. (It doesn't seem to be...
2
1156
by: Nikhil | last post by:
Hi, Does anybody knows faster parsers than C - RXP in validating category and Expat in non-validating category? Are both of them (or their faster ones) portable in Mac OS X? Thanks, ...
1
1313
by: Avi Kak | last post by:
Hello: This questions relates to the behavior of the Perl SAX 2.0 parser XML::LibXML::SAX. (This behavior is also shown by the XML::SAX::Expat parser and, possibly by all other Perl SAX 2.0...
1
1546
by: bloon | last post by:
I know there are three most popular open-source XML parsers. They are expat, libxml, and Xerces. All three are cross-platform. Does anybody test these three parsers? Which one is the fastest when...
1
1280
by: Srinivasa Parupalli | last post by:
Dear Friends, I am struck up with logic. I am using one class(BMSLexer) which takes filename as arugument and the instance of the class is used my another class. example is shown below. Now my...
6
2626
by: kaens | last post by:
Hey everyone, this may be a stupid question, but I noticed the following and as I'm pretty new to using xml and python, I was wondering if I could get an explanation. Let's say I write a simple...
6
4411
by: rellaboyina | last post by:
Dear All, I am having some data which will be stored in XML format and this needs to be parsed using the parser module XML::Parser and XML::Parser::Expat. This data consists of some special...
0
7254
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
7153
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
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.