473,505 Members | 14,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Limited XML tidy


I have a program which produces well-formed XML documents,
but takes several hours if not days to do so. It would
be useful to be able to take the incomplete output and
manipulate it as XML.

Clearly, however, the incomplete output will not be well-
formed, so before being able to manipulate it I need to
make it wellformed.

This is essentially fairly simple. I know it's well-formed
up to now - all I need to do is close unclosed tags. So,
I 've made a short function that will do this:

#!/usr/bin/python

import sys
import xml.sax

tagStack = []

closingTags = ""

class DodgyHandler(xml.sax.ContentHandler):
def startElement(self, tag, attributes):
tagStack.append(tag)

def endElement(self, tag):
tagStack.pop()
class DodgyErrorHandler(xml.sax.ErrorHandler):
def fatalError(self,exception):
global closingTags
tagStack.reverse()
for tag in tagStack:
closingTags += "</%s>" % tag
return closingTags
def finishXML(text):
p = xml.sax.make_parser()
p.setContentHandler(DodgyHandler())
p.setErrorHandler(DodgyErrorHandler())

for line in text:
p.feed(line)
p.close()

text.append(closingTags)
However - while this works for 90% of the cases I need,
it fails in the case where my incomplete output stops in the
middle of a tag (not to mention some other more arcane
places I don't really care about).

The problem is that when the sax handler raises an exception,
I can't see how to find out why. What I want to do is for
DodgyErrorHandler to do something different depending on
where we are in the course of parsing. Is there anyway
to get that information back from xml.sax (or indeed from
any other sax handler?)

Toby

--
Dr. Toby White
Dept. of Earth Sciences, Downing Street, Cambridge CB2 3EQ. UK
Email: <to***@cam.ac.uk>

Aug 23 '05 #1
3 1557
In article <t8*************@parabrisas.esc.cam.ac.uk>, Toby White wrote:
[snip]

I do similar stuff in the new (upcoming) version of Atox
(atox.sf.net), which works with potentially ill-formed, partial XML
(in the form of PYX events) internally, and can take partial,
ill-formed XML as input.
The problem is that when the sax handler raises an exception,
I can't see how to find out why. What I want to do is for
DodgyErrorHandler to do something different depending on
where we are in the course of parsing. Is there anyway
to get that information back from xml.sax (or indeed from
any other sax handler?)
What I ended up doing was using an SGML parser (sgmlop) instead. It's
highly forgiving (even of illegal entities and the like) but gives me
the information I need. Might be worth a look in your app too?
Toby


--
Magnus Lie Hetland
http://hetland.org
Aug 25 '05 #2
> The problem is that when the sax handler raises an exception,
I can't see how to find out why. What I want to do is for
DodgyErrorHandler to do something different depending on
where we are in the course of parsing. Is there anyway
to get that information back from xml.sax (or indeed from
any other sax handler?)

You can get raw location information, yes. See:

http://www.xml.com/pub/a/2004/11/24/py-xml.html

But I don't think this is enough for you. You also need recovery,
which you're implementing in crude form.

I tend to agree with Magnus that using an SGML parser might be your
best bet. You might even be able to turn that SGML into XML using a
tool such as James Clark's SX:

http://www.jclark.com/sp/sx.htm

--
Uche
http://copia.ogbuji.net

Aug 25 '05 #3
uc*********@gmail.com writes:
The problem is that when the sax handler raises an exception, I can't see how to find out why. What I want to do is for
DodgyErrorHandler to do something different depending on
where we are in the course of parsing. Is there anyway
to get that information back from xml.sax (or indeed from
any other sax handler?)

You can get raw location information, yes. See:

http://www.xml.com/pub/a/2004/11/24/py-xml.html

But I don't think this is enough for you. You also need recovery,
which you're implementing in crude form.


(If you're referring to the Locator objects), yes I'm aware
that's possible. But what I want is not my location in the
document, but for the parser to say "this is an error because
I am in the middle of a tag & the document ended", or "I
was in the middle of a text section and the document ended", or
"I was in the middle of an attribute value and the document
ended", etc, so that I can then construct a simple end to the
document, inserting quote marks, finishing the tag, and closing
all unclosed tags as appropriate.

I have just realised that I might be able to grab the message
that the exception gives me, look at the expat source code
and work out what parsing events cause which error messages.
Which is a bit round the houses, but I think ought to work.

I tend to agree with Magnus that using an SGML parser might be your
best bet. You might even be able to turn that SGML into XML using a
tool such as James Clark's SX:

http://www.jclark.com/sp/sx.htm


If I can't get my scheme above to work, I'll have a go. But I was
hoping to do this without requiring additional packages. And in
any case, it doesn't need to be perfectly robust. As long as it
handles 99% of cases, I'll be happy.

--
Dr. Toby White
Dept. of Earth Sciences, Downing Street, Cambridge CB2 3EQ. UK
Email: <to***@cam.ac.uk>
Aug 26 '05 #4

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

Similar topics

0
1785
by: compu_global_hyper_mega_net_2 | last post by:
Greetings, usenet's obviously dying. Where's everyone hanging out? One of the yahoo groups or something? anyway. Hopefully someone picks this up: I'm trying to compile php-5.0.0RC2 with...
1
2126
by: Mike Gifford | last post by:
Hello Folks, I'm trying to get tidy set up on a pretty standard fedora core 1 server. Looks pretty simple from here: http://ca3.php.net/manual/en/ref.tidy.php Should just have to go: pear -v...
0
3313
by: Börni | last post by:
Hello, I am using tidy to clean up an xml file, but my problem is that it replaces newlines with normal whitespaces. Still worse is that if i retrieve text from the xml file it contains all...
3
2758
by: Mike | last post by:
Greetings, I just used Tidy HTML for the first time. The address below contains the files need to check on Tidy's corrections. I would appreciate any feedback on the Tidy report and the...
12
17078
by: Stefan Weiss | last post by:
Hi. (this is somewhat similar to yesterday's thread about empty links) I noticed that Tidy issues warnings whenever it encounters empty tags, and strips those tags if cleanup was requested....
0
1829
by: Christoph Schneegans | last post by:
Hi! I'd like to present an easy, yet powerful approach to use the HTML Tidy program from <http://tidy.sourceforge.net/> in ASP.NET. It is similiar to...
40
5535
by: VK | last post by:
Hi, After the response on my request from W3C I'm still unclear about Tidy vs. Validator discrepansies. That started with <IFRAME> issue, but there is more as I know. Anyway, this very basic...
0
1863
by: BG Mahesh | last post by:
hi I have installed Tidy on Fedore Core 4.0 using RPM. I have a very simple script that uses tidy, ------------tidy.php------------------ <html>a html document</html> <? $html =...
1
20194
by: Martin Odhelius | last post by:
Hello, Does anybody here have any example code for Tidy.Net (http://sourceforge.net/projects/tidynet/) ? I can't find one single example. I try to convert a html-string to well formed xhtml,...
0
5022
by: kempshall | last post by:
Can somebody please tell me how to install the Tidy module for PHP 5 on a Mac? I tried what the php.net website said, which is running the command "pecl install tidy" but the installation failed...
0
7098
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
7303
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,...
0
7367
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...
0
7471
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
3187
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...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.