Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Simple noisy option?
by BrowserUk (Patriarch) on Jan 30, 2003 at 00:59 UTC | |
Update:Mirod points out that this trick will only work if your version of XML::Simple is using XML::Parser as it's underlying parser. If you have/are using SAX as the underlying Parser - ignore this:). Thanks for the heads-up Mirod++. XML::Simple has a parseropts option which allows you to pass XML::Parser options through to the XML::Parser instantiated by XML::Simple. One of the XML::Parser options is Handlers=> [ Event=>\&handler, ...]. By using this to register your own handlers you can get your own code called--with salient information as to what is being processed--as XML::Parser processes your document. The problem is that doing so overrides the XML::Parser::Tree style handlers used by XML::Simple. However, these handlers conform to a standard naming convention and it is easy to use perl's goto &func; form of goto, to invoke these standard handlers from your own once you have finished tracing the info. The PoC code below produces this output while successful parsing the emebedded example XML.
The code: (Update:simplified option setting code) Read more... (1528 Bytes)
For more information on what handlers you can override, and what information is passed to them see the XML::Parser pod. (or source). I might make a package out of this idea if anyone is interested. Examine what is said, not who speaks. The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead. | [reply] [d/l] [select] |
|
Re: XML::Simple noisy option?
by BazB (Priest) on Jan 29, 2003 at 23:42 UTC | |
You might want to consider using the SAX events that XML::Simple (and many other XML parsers) can generate. As well as keeping resource usage down, you can print the new document to the browser as you process it.
Cheers.
If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong. | [reply] |
by mirod (Canon) on Jan 30, 2003 at 00:34 UTC | |
I don't think this would work. I haven't read that part of the code, but I believe you still have to slurp the entire document in memory with XMLin and process it before you start emitting SAX events using XMLout. What could work is using a SAX parser, and setting up a SAX filter that would spur out messages before passing the data untouched to XML::Simple. Or even using a tee like XML::Filter::Tee so that both XML::Simple and a separate SAX handler receive the input. That handler can then spit out a message depending on where it is in the parsing. OTOH playing with signals to send an "everything is OK" message every x second might be quite simpler ;--) | [reply] |
|
Re: XML::Simple noisy option?
by pg (Canon) on Jan 30, 2003 at 03:26 UTC | |
The design objective of SAX is to parse XML data as a stream on fly. Remember the data source can be a TCP port, SAX would parse the XML stream when data is still coming. As a design cosideration, it would be terribly inefficient, not to use the idle time in between TCP communication. Terrible. On the other hand, remember, even with all the best design consideration, XML parsing is still very slow. This is not something you can escape at this stage of XML's life cycle. Update: After received serveral msg's from mirod. I realized that this is some orange/apple thing. I think we down voted each other, that's fine and that's part of the life here ;-). Couple of XP points is much less important than to make the facts straight. In this sense, we both did well. | [reply] |
by mirod (Canon) on Jan 30, 2003 at 04:16 UTC | |
XML::Simple is not a SAX parser. It is a module that lets you load XML data into a Perl structure. It works on top of a parser, which can be XML::Parser or, since versions 1.08_01, any SAX parser. So by (good) design it slurps a whole XML file into memory. Not only did you not read the code, it looks like you did not read the documentation either ;--) | [reply] |
by pg (Canon) on Jan 30, 2003 at 04:49 UTC | |
Slurping a whole file, in order to use a SAX::Parser? Good he/she didn't do it. To be frank with you, the point is really not whether XML::Simple slurp the whole file or not, that's not something you or me can change, it was just done in that way, but it is a very poor judgement to praise "slurp a whole file" as a good design in this context. | [reply] |