I think my first example was not getting very close to what you actually want. Try this instead. It's also a bit weird, tricky, and not robust but the output seems right this time and it's semi-automatic for finding key, value pairs.

use warnings; use strict; use XML::LibXML; sub snipName { s/\s+//g and $_ = lcfirst($_) for @_ }; sub snipValue { s/\A\s+|\s+\z//g for @_ }; my $parser = XML::LibXML->new(); $parser->keep_blanks(0); # $parser->recover_silently(1); # Might need, might not. my $doc = $parser->parse_html_fh(\*DATA); my $xml = XML::LibXML::Document->new(); my $root_name = [ $doc->findnodes('//div[@class="spec_section"]//div[c +ontains(@class,"spec_row_header")]') ]->[0]->textContent; snipName($root_name); $xml->setDocumentElement( $xml->createElement($root_name) ); for my $col ( $doc->findnodes('//div[@class="spec_row_left"]') ) { snipName( my $name = $col->textContent ); my $tag = $xml->createElement($name); my $value = [ $col->parentNode->findnodes('div[@class="spec_row_ri +ght"]') ]->[0]->textContent; snipValue( $value ); $tag->appendChild( $xml->createTextNode( $value ) ); $xml->getDocumentElement->appendChild($tag); } 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>

Which yields-

<?xml version="1.0"?> <engine> <type>Diesel</type> <builder>MTU</builder> <model>2x 16V396TB94</model> <power>2561kw / 3480hp</power> <totalPower>5121kw / 6960hp</totalPower> <enginePropulsion>Twin Screws</enginePropulsion> </engine>

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.