(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>

In reply to Re: module for HTML XML conversion by Your Mother
in thread module for HTML XML conversion by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.