http://qs1969.pair.com?node_id=175543

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

Hello,

Warning: I'm a Perl/XML newbie. I have an interesting (at least I think so) XML problem that I was hoping to solve with Perl and its XML extensions. I've been hanging out on the site (surfing some previous postings regarding the specific modules that I've been playing with) and I'm just about through the Perl & XML O'Reilly book (really enjoyed the read). Enough chit chat.

I'm using Matt Sergeant's XML::Generator::DBI module to query my data source and return an XML document similar to the following:

<?xml version="1.0" encoding="UTF-8"?> . . . <Message> <MessageID>376</MessageID> <CreateDate>2002-05-31 09:14:41.047</CreateDate> <Subject>Re: Msg54</Subject> </Message> <Message> <MessageID>377</MessageID> <CreateDate>2002-06-04 08:47:47.627</CreateDate> <Subject>Re: Msg95</Subject> </Message> <Message> <MessageID>378</MessageID> <CreateDate>2002-06-04 08:51:58.390</CreateDate> <Subject>Demo</Subject> </Message> <Message> <MessageID>379</MessageID> <CreateDate>2002-06-04 08:54:38.593</CreateDate> <Subject>Re: Demo</Subject> </Message> . . .
The problem is that I have to extract (inclusively) the data between each <Message>...</Message> section and write each chunk to its own file using the value in <MessageID>...</MessageID> for the file name. I understand that this module generates SAX events as a result of the query however, I was considering building a DOM tree and using XPath to extract the necessary text. Can anyone offer any advice on my purposed method of attack? I understand the benefits of using SAX with regards to performance however, the ease of the XPath syntax seems very appealing.

Thanks so much,
Jay

Replies are listed 'Best First'.
Welcome to the Joy of SAX
by Matts (Deacon) on Jun 19, 2002 at 06:24 UTC
    Check out XML::LibXML::SAX::Builder. It builds a DOM from a SAX stream. From there you can use XPath on the data set, and even serialise back to SAX when you're done if you want to. There's nothing in the SAX spec that says you shouldn't be able to use XPath on the data, or shouldn't build a DOM with it. SAX just happens to be the universal plumbing.

    Also you might want to check out Barrie Slaymaker's XML::Filter::Dispatcher, which allows you attach to a SAX pipeline and dispatch to methods/functions based on XPath's specified in the constructor. Rather like XML::Twig, but better ;-)

    I've been meaning to write a node about just how great SAX is for a while now, maybe it's time I just did it.

      Note that, in order not to be outdone, XML::Twig supports SAX (SAX1 and SAX2 actually) output in version 3.05. I am still pondering whether to include SAX input in the main module or to create a separate XML::TwigSAX, but I will get there, and join the happy Perl/SAX family ;--)

      Plus does XML::Filter::Dispatcher (which looks pretty cool BTW) have a tee-shirt? ;--)

      Thanks so much. I'll look into these modules.
Re: Design recommendation for parsing and creating XML files.
by jmurphy (Acolyte) on Jun 19, 2002 at 07:17 UTC
    I apologize. I just re-read the post and realized the formatting for the last paragraph was incorrect. It probably didn't make much sense. It should have read:

    The problem is that I have to extract (inclusively) the data between each <Message>...</Message> section and write each chunk to its own file using the value in <MessageID>...</MessageID> for the file name. I understand that this module generates SAX events as a result of the query however, I was considering building a DOM tree and using XPath to extract the necessary text. Can anyone offer any advice on my purposed method of attack? I understand the benefits of using SAX with regards to performance however, the ease of the XPath syntax seems very appealing.

    Thanks again,
    Jay