Skip to content Skip to sidebar Skip to footer

Xml Output From Html Page Using Web Scraping In Perl

Is it possible to have an xml output from webpage using web::scraper in perl. as an example, My html looks like follows(I took some part of html from URL): >

Solution 1:

You just need to move your output into the first for loop. Since you've an equal number of items in each of the three keys in $res, you can just use $i to access all of the individual items. You'll get the three values that belong to each other with your iteration from $i.

formy $i (0 .. $#{$res->{renners}}) {
  print <<"XML";
<PropertyList>
  <Property>
    <Name>$res->{renners}[$i]</Name>
    <ReturnValue>$res->{landrenner}[$i]</ReturnValue>
    <domversion>$res->{dom}[$i]</domversion>
  </Property>
</PropertyList>
XML
}

I changed the print statements to use a HERE doc because it is more easily readable. I also changed the line my $res = $teamsdata->scrape(URI->new($urlToScrape)); to my $res = $rennersdata->scrape(URI->new($urlToScrape)); because $teamsdata was not declared.

Solution 2:

I realize this is not exactly what you are looking for, but take a look at HTML::Element. It has a as_XML method which you can use to convert the HTML tree into XML. HTH.

Solution 3:

Try this: Simply rearrange the print statements:

print"<PropertyList>\n";   
formy $i (0 .. $#{$res->{renners}}) {    
    print"<Property>\n";
    print"<Name> ";
        print $res->{renners}[$i]; print"\n";
    print"</Name>"; print"\n";

    formy $j (0 .. $#{$res->{landrenner}}) {
        print"<ReturnValue>\n";
            print $res->{landrenner}[$j];print"\n";
        print"</ReturnValue>\n";
    }
    formy $k (0 .. $#{$res->{dom}}) {
        print"<domversion>\n";
            print $res->{dom}[$k];print"\n";
        print"</domversion>\n";
    }   
    print"</Property>\n";
}
print"</PropertyList>\n";

Post a Comment for "Xml Output From Html Page Using Web Scraping In Perl"