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 | |
by Your Mother (Archbishop) on Mar 28, 2010 at 05:30 UTC | |
|
Re^2: module for HTML XML conversion
by Anonymous Monk on Mar 27, 2010 at 17:47 UTC |