将perl输出转换为XML(标签)

问题描述 投票:-1回答:1

我已经用TAP3Edit解码了TAPFiles,但我不知道输入的确切文件格式是什么(在下面共享)?它不是JSON,我想知道是否有人知道此输出的格式?另一个问题是关于将输入自动转换为XML文件(我在此处共享了相对输出)以及使用perl或任何其他编程语言?(我听说Perl非常适合文本处理)

PS:此数据不是真实的,我仅将其用于测试。

输入

$VAR1 = {
    'Detail' => [
        {
            'TimeStamp' => {
                'LocalTime' => '20200231',
                'utcTime' => 2
            },
            'A-Value' => 1,
            'B-Value' => '2',
            'C-Value' => 3,
            'D-Value' => 4
        }
    ]
}

所需的输出:

<Detail>
  <TimeStamp>
     <LocalTime>20200231</LocalTime>
     <utcTime>2</utcTime>
  </TimeStamp>
  <A-Value>1</A-Value>
  <B-Value>2</B-Value>
  <C-Value>1</C-Value>
  <D-Value>2</D-Value>
</Detail>
xml perl text tags tap
1个回答
-1
投票

这里是修改后的代码。

!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
use XML::Dumper;
use TAP3::Tap3edit;
$Data::Dumper::Indent=1;
$Data::Dumper::Useqq=1;
my $dump = new XML::Dumper;
use File::Basename;
use open qw/:std :utf8/;
use XML::LibXML;

my $perl='';
my $xml='';
my $tap3 = TAP3::Tap3edit->new();
$data = foreach my $file(glob 'Taps/X*')
{

   $files= basename($file);

   $tap3->decode($files) || die $tap3->error;

}
my $doc = XML::LibXML::Document->createDocument('1.0', 'UTF-8');
toxml($doc, 'data', $data);
print $doc->toString(1);
sub toxml {
    my ($parent, $name, $data) = @_;
    my @args = $name=~/\A\w+\z/ ? ($name) : ('value', name=>$name);
    if ( ref $data eq 'HASH' ) {
        my $el = newel($parent, @args);
        toxml($el, $_, $data->{$_}) for sort keys %$data;}
    elsif ( ref $data eq 'ARRAY' ) {
toxml(ref eq 'ARRAY' ? newel($parent, @args) : $parent,
            $name, $_) for @$data;
    }
    elsif ( ref $data ) { die "Can't handle $data (yet)" }
    else { newel($parent, @args)->appendText($data) }

}
sub newel {
   my ($parent, $name, %attrs) = @_;
   my $el = $parent->ownerDocument->createElement($name);
   $el->setAttribute( $_ => $attrs{$_} ) for keys %attrs;
   if ( $parent->nodeType==XML_DOCUMENT_NODE )
    { $parent->setDocumentElement($el) }
     else { $parent->appendChild($el) }
      return $el;
}
my $filename="Out.xml";
$perl = $tap3->structure;

这里是错误:

"my" variable $file masks earlier declaration in same statement at perl-V04.pl line 20.
"my" variable $tap3 masks earlier declaration in same scope at perl-V04.pl line 22.
"my" variable $files masks earlier declaration in same statement at perl-V04.pl line 22.
"my" variable $tap3 masks earlier declaration in same statement at perl-V04.pl line 22.
"my" variable $doc masks earlier declaration in same statement at perl-V04.pl line 26.
"my" variable $doc masks earlier declaration in same scope at perl-V04.pl line 27.
Global symbol "$data" requires explicit package name at perl-V04.pl line 17.
syntax error at perl-V04.pl line 17, near "= foreach "
syntax error at perl-V04.pl line 24, near "}"
Execution of perl-V04.pl aborted due to compilation errors. 
© www.soinside.com 2019 - 2024. All rights reserved.