How to convert tag names and values from XML into HTML using Perl -


is there way convert simple xml document html using perl give me table of tag names , tag values?

the xml file output.xml this

<?xml version="1.0"?>  <doc>     <gi-estb-mib-nph>         <estbgeneralerrorcode.0>integer: 0</estbgeneralerrorcode.0>         <estbgeneralconnectedstate.0>integer: true(1)</estbgeneralconnectedstate.0>         <estbgeneralplatformid.0>integer: 2076</estbgeneralplatformid.0>         <estbgeneralfamilyid.0>integer: 25</estbgeneralfamilyid.0>         <estbgeneralmodelid.0>integer: 60436</estbgeneralmodelid.0>         <estbmocamacaddress.0>string: 0:0:0:0:0:0</estbmocamacaddress.0>         <estbmocanumberofnodes.0>integer: 0</estbmocanumberofnodes.0>     </gi-estb-mib-nph> </doc> 

i trying create html looks this

1. estbgeneralplatformid.0 - integer: 2076 2. estbgeneralfamilyid.0 - integer: 25 3.  

i trying use code web having hard time understanding how generate required format html tags.

what trying this

#!/usr/bin/perl  use strict; use warnings;  use xml::parser; use xml::libxml;  #add tagnumberconversion.pl here  $parser = xml::parser->new(); $parser->sethandlers(     start => \&start,     end   => \&end,     char  => \&char,     proc  => \&proc, );  $header = &getxhtmlheader(); print $header;  $parser->parsefile( '20150630104826.xml' );  $currenttag = "";  sub start() {      ( $parser, $name, %attr ) = @_;     $currenttag = $name;      if ( $currenttag eq 'doc' ) {         print "<head><title>"             . "output of snmpwalk cpeip4"             . "</title></head>";         print "<body><h2>" . "output of snmpwalk cpeip4" . "</h2>";         print '<table summary="'             . "output of snmpwalk cpeip4"             . '"><tr><th>tag name</th><th>tag value</th></tr>';     }     elsif ( $currenttag eq 'gi-estb-mib-nph' ) {         print "<tr>";     }     elsif ( $currenttag =~ /^estb/ ) {         print "<tr>";     }     else {         print "<td>";     } }  sub end() {      ( $parser, $name, %attr ) = @_;     $currenttag = $name;      if ( $currenttag eq 'doc' ) {         print "</table></body></html>";     }     elsif ( $currenttag eq 'gi-estb-mib-nph' ) {         print "</tr>";     }     elsif ( $currenttag =~ /^estb/ ) {         print "</tr>";     }     else {         print "</td>";     } }  sub char() {     ( $parser, $data ) = @_;      print $data; }  sub proc() {     ( $parser, $target, $data ) = @_;      if ( lc( $target ) eq 'perl' ) {         $data = eval( $data );         print $data;     } }  sub getxhtmlheader() {      $header = '<?xml version="1.0" encoding="utf-8"?> <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';      return $header; } 

this code in progress, realize overkill requirement.

so trying figure out if there quick way using perl.

please give me pointers if there indeed quick way.

the quick , dirty way use regular expression. comes risk of missing data , getting burned edge cases. since asked it...

#!/usr/bin/env perl  use strict;  open $fh, 'filename.xml'     or die "unable open filename.xml : $!"; $count = 1; print "<head><title>'output of snmpwalk cpeip4'</title></head>\n"; print "<body><h2>'output of snmpwalk cpeip4'</h2>\n"; print "<table summary='output of snmpwalk cpeip4'><tr><th>tag name</th><th>tag value</th></tr>\n"; while (my $line = <$fh>) {     next unless $line =~ m|<estb|;     # store into $tag , $value     # result of matching whitespace, followed '<'     # followed (store $tag)     # followed '>'     # followed (store $value)     # followed '<'     ($tag, $value) = $line =~ m|\s+<(.+?)>(.+?)<|;     print "<tr><td>" . $count++ . ". $tag</td><td>$value</td></tr>\n"; } print "</table></body></html>\n"; 

produces following:

<head><title>'output of snmpwalk cpeip4'</title></head> <body><h2>'output of snmpwalk cpeip4'</h2> <table summary='output of snmpwalk cpeip4'><tr><th>tag name</th><th>tag value</th></tr> <tr><td>1. estbgeneralerrorcode.0</td><td>integer: 0</td></tr> <tr><td>2. estbgeneralconnectedstate.0</td><td>integer: true(1)</td></tr> <tr><td>3. estbgeneralplatformid.0</td><td>integer: 2076</td></tr> <tr><td>4. estbgeneralfamilyid.0</td><td>integer: 25</td></tr> <tr><td>5. estbgeneralmodelid.0</td><td>integer: 60436</td></tr> <tr><td>6. estbmocamacaddress.0</td><td>string: 0:0:0:0:0:0</td></tr> <tr><td>7. estbmocanumberofnodes.0</td><td>integer: 0</td></tr> </table></body></html> 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -