Is there any way to convert a simple XML document into HTML using Perl that would give me a table of tag names and tag values?
The XML file output.xml is like 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 am trying to create HTML which looks like this
1. eSTBGeneralPlatformID.0 - INTEGER: 2076
2. eSTBGeneralFamilyID.0 - INTEGER: 25
3.
I was trying to use code from the web but I am really having a hard time understanding how to generate the required format for HTML tags.
What I was trying was this
#!/usr/bin/perl
use strict;
use warnings;
use XML::Parser;
use XML::LibXML;
#Add TagNumberConversion.pl here
my $parser = XML::Parser->new();
$parser->setHandlers(
Start => \&start,
End => \&end,
Char => \&char,
Proc => \&proc,
);
my $header = &getXHTMLHeader();
print $header;
$parser->parsefile( '20150630104826.xml' );
my $currentTag = "";
sub start() {
my ( $parser, $name, %attr ) = @_;
$currentTag = $name;
if ( $currentTag eq 'doc' ) {
print "<head><title>"
. "Output of snmpwalk for cpeIP4"
. "</title></head>";
print "<body><h2>" . "Output of snmpwalk for cpeIP4" . "</h2>";
print '<table summary="'
. "Output of snmpwalk for 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() {
my ( $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() {
my ( $parser, $data ) = @_;
print $data;
}
sub proc() {
my ( $parser, $target, $data ) = @_;
if ( lc( $target ) eq 'perl' ) {
$data = eval( $data );
print $data;
}
}
sub getXHTMLHeader() {
my $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 is code in progress, but I realize that this will be overkill for my requirement.
So I am trying to figure out if there is any quick way to do it using Perl.
Please give me some pointers if there is indeed any quick way.
&when you are calling Perl subroutines -- justmy $header = getXHTMLHeader()is correct. And you shouldn't use prototypes when you are defining subroutines -- those()after the subroutine names make sure that no parameters are ever passed, which isn't what you want at all. Justsub start { ... }is correct. And you should reserve capital letters for global identifiers such as package names -- local identifiers should consist of lower-case letters, decimal digits and underscores