in reply to module for HTML XML conversion

(update: the later example, Re: module for HTML XML conversion, is much better than this one.)

This is pretty specialized and inlines more than it should. You have to write a few lines of code for every part of your spec. So it is not the best if you have a need for generalized transforms but if you have an exact transform to do that won't change much, this can be great. XML::LibXML.

use warnings; use strict; use XML::LibXML; my $parser = XML::LibXML->new(); $parser->keep_blanks(0); # $parser->recover_silently(1); # <-- for bad HTML my $doc = $parser->parse_fh(\*DATA); # If you have well balanced snipp +et like below # my $doc = $parser->parse_html_fh(\*DATA); # If you have HTML. my $xml = XML::LibXML::Document->new(); $xml->setDocumentElement( $xml->createElement("engines") ); for my $spec_row ( $doc->findnodes('//div[@class="spec_row"]') ) { my $engine = $xml->createElement("engine"); $xml->getDocumentElement->appendChild($engine); my $type = $xml->createElement("type"); ( my $text = $spec_row->firstChild->textContent ) =~ s/\A\s+|\s+\z +//g; $type->appendChild( $xml->createTextNode($text) ); $engine->appendChild( $type ); my $builder = $xml->createElement("builder"); ( $text = $spec_row->lastChild->textContent ) =~ s/\A\s+|\s+\z//g; $builder->appendChild( $xml->createTextNode($text) ); $engine->appendChild( $builder ); } print $xml->serialize(1); __DATA__ <div class="spec_section"> <div class="spec_row"> <div class="spec_row_header orange_text_1"> Engine </div> </div> <div class="spec_row"> <div class="spec_row_left"> Type </div> <div class="spec_row_right"> Diesel </div> </div> <div class="spec_row"> <div class="spec_row_left"> Builder </div> <div class="spec_row_right"> MTU </div> </div> <div class="spec_row"> <div class="spec_row_left"> Model </div> <div class="spec_row_right"> 2x 16V396TB94 </div> </div> <div class="spec_row"> <div class="spec_row_left"> Power </div> <div class="spec_row_right"> 2561kw / 3480hp </div> </div> <div class="spec_row"> <div class="spec_row_left"> Total Power </div> <div class="spec_row_right"> 5121kw / 6960hp </div> </div> <div class="spec_row"> <div class="spec_row_left"> Engine Propulsion </div> <div class="spec_row_right"> Twin Screws </div> </div> </div>

Replies are listed 'Best First'.
Re^2: module for HTML XML conversion
by ikegami (Patriarch) on Mar 28, 2010 at 05:14 UTC

    my $doc = $parser->parse_fh(\*DATA); # If you have well balanced snippet like below

    Is there any reason to not use parse_html_fh to parse HTML? All of the following are well balanced (or impossible to balance) but invalid XML:

    <option selected ...>...</option> <font color=red>...</font> <br> <img ...> <meta ...> &nbsp; (without XML bits to define it)

    Encoding is also handled differently.

      No, I think it's a good idea. But the sample input was well balanced so I used it and included the other in (as you point out, the likely) case it wouldn't fly.

      The sample also fails to specially account for the first <div/> which contains the "column" names. We could probably do something automatic with that instead of hard coding the column parsing. The OP might have thought of that already. I didn't until looking it over again now. If I have a few minutes I'll revisit it with that approach.

Re^2: module for HTML XML conversion
by Anonymous Monk on Mar 27, 2010 at 17:47 UTC
    That's very kind of you to do that - I can definitely work on that approach. Much appreciated...