Perl脚本打印这样的数据

时间:2011-10-20 14:27:23

标签: xml perl

  

可能重复:
  In Perl, how can I produce a PDF file using data in an XML file?

我有一个xml文件,我提取了一些注释并保存在pdf文件中。我写这样的脚本

#!/usr/bin/perl
use warnings;
use strict;
use PDF::API2;
use PDF::API2::Page;
use XML::LibXML::Reader;
use Data::Dumper;

my $file;
open( $file, 'formal.xml');
my $reader = XML::LibXML::Reader->new( IO => $file )
    or die ("unable to open file");

my %nums;
while ($reader->nextElement( 'Number' ) ) {
    my $number = $reader->readInnerXml();
    $reader->nextElement( 'address' ); 
    my $info = $reader->readOuterXml(); 
    $nums{$number} = $info;
}

my $pdf = PDF::API2->new(); 
# $pdf->mediabox('Letter');
my $font = $pdf->corefont('Times-Roman');
my $page = $pdf->page();
$page->mediabox('Letter');

my $cnt = 0;
for my $line (split /\n/, Dumper(%nums)) {
    if ($cnt > 46) {
        $page = $pdf->page();
        $cnt = 0;
    }
    my $text = $page->text();
    $text->font($font,14);
    $text->translate(72, 720-$cnt*14);
    $text->text($line);
    ++$cnt;
}

$pdf->saveas('svr.pdf');

当我运行此代码时,它提取并打印提取的标签,就像在pdf文件中一样,如下所示。

   $VAR1 = '24';
   $VAR2 = '<Address>
            <housenumber="120"/>
           <streetname="xxx"/>
            <information/>
           </Address>';
  $VAR3 = '25';
   $VAR4 = '<Address>
       <housenumber="150"/>
    <streetname="xxx"/>
      <information/>
       </Address>';
     $VAR5 = '27';
     $VAR6 = '<Address>
             <housenumber="140"/>
             <streetname="xxx"/>
             </information>
                </Address>';
像那样,但我需要像这样用pdf打印数据

  number: 24, address information of the student.
     Information:Address,
             housenumber="120",
              streetname="xxx",
               information. 
     number: 25, address information of the student.
     Information:Address,
             housenumber="150",
              streetname="xxx"
              information. 
     number: 27, address information of the student.
     Information:Address,
             housenumber="140",
              streetname="xxx"
               information.  

我需要在pdf文件中打印这样的输出。在我的书面代码中,我按原样打印xml标签。我该怎么做才能打印出来,帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

使用XML::LibXML::Simple来解析XML文件,这将允许您获取节点或属性(没有标记)的内容。

答案 1 :(得分:1)

您可以使用$reader->copyCurrentNode(1)将元素及其子元素解析为DOM树,然后您可以从中提取所需的值,如下所示(警告:未经测试的代码):

my %nums;
while ($reader->nextElement( 'Number' )) {
    my $number = $reader->copyCurrentNode(1)->textContent;

    $reader->nextElement( 'address' ); 
    my $node = $reader->copyCurrentNode(1); 
    my $infohash = {
        house  => $node->getElementsByTagName( 'housenumber' )->[0]->textContent,
        street => $node->getElementsByTagName( 'streetname'  )->[0]->textContent,
    };
    $nums{$number} = $infohash;
}

有关如何从DOM中提取信息的更多信息,请参阅XML::LibXML::Element