yaneurabeya has asked for the wisdom of the Perl Monks concerning the following question:

Hello again Monks,

I am on a quest to implement an XML parser and XML printer to dispose of plaintext logfile parsing in a system at work.

In my effort I've found many parsers but (and I might have missed something here), I haven't found any XML file printers, and I want to avoid creating one of my own if possible.

My current target for a parser is the XML::Parser (expat) module using the stream property.

Any tips or ideas are more than welcome :). Thanks!

-Garrett

  • Comment on Perl has plenty of XML parsers, but is there an XML printer?

Replies are listed 'Best First'.
Re: Perl has plenty of XML parsers, but is there an XML printer?
by GrandFather (Saint) on Jul 19, 2007 at 22:10 UTC

      XML printer => a module that prints out XML compatible spec strings to file descriptors :).

      Anyhow, I'll look into XML::*Writer. I looked at XML::Twig, but I'm not sure it's what I'm aiming for (was targeted for partial XML file parsing from what the author wrote).

      My work's Perl dist also doesn't have a lot of the more esoteric XML Perl modules, and because I'm trying to conform to cross site Perl distributions, I'm avoiding the less popular XML solutions.

      Could a list of (the more popular) modules that do both parsing and printing be provided?

      Thanks!

        XML::Twig is the "kitchen sink" XML manipulating module. It does pretty much everything you would want to do in the context of XML and is widely used.

        You may like to post a very small sample of what you need to parse and what your output should look like.


        DWIM is Perl's answer to Gödel
Re: Perl has plenty of XML parsers, but is there an XML printer?
by MonkE (Hermit) on Jul 19, 2007 at 22:37 UTC
    I'm not sure why you want to "print" the log file. I'm assuming for the sake of this post that you just need some human-readable way of viewing the XML log. So with that in mind I'm thinking that you may not even need a Perl module. Why don't you just create an XSLT style sheet for use with a browser and have each log file reference that stylesheet.

    I have done this before for server status reports. The XML document makes a nice, machine-readable status report. Feeding the exact same document to a browser such as Firefox gives HTML (or text) output for humans, since it references the XSLT stylesheet.

    Again, I'm not sure what you're after, but I thought I'd mention this approach.

      This isn't going to be used in a web environment, so I don't think that there will be a need for a solution like a XSLT stylesheet.

      All that's going to be done is data retrieval and comparison via flat files (don't have access to a DB).

        Getting access to a db like SQLite or MySQL or PostgreSQL isn't that hard. But if you really want to compare flat files and XML files in a database-like manner without a database, then DBD::AnyData sounds like what you want. It handles many kinds of flat files (CSV, Fixed-width, ini, etc.) and uses XML::Twig to both read and write XML files using the standard DBI interface.

        For a quick and easy database take a look at DBD::SQLite and DBI - that's the whole ball o' wax.


        DWIM is Perl's answer to Gödel
Re: Perl has plenty of XML parsers, but is there an XML printer?
by Trizor (Pilgrim) on Jul 19, 2007 at 22:10 UTC

    You can build an XML node tree with the structures in XML::XPath though all of the required methods aren't documented (but Matt didn't mark them with underscores so I consider them fair game) but I haven't done it in a while and can't remember off the top of my head. Its a solution thats very fast, if you don't mind reading a bit of source.