Perl 脚本打印这样的数据
我有一个 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 标签。我应该做什么来打印,帮我写这个脚本。
Possible Duplicate:
In Perl, how can I produce a PDF file using data in an XML file?
I have one xml file, I extracted some comments and saved in pdf file.I written script like this
#!/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');
when I run this code it extracting and printing extracted tags as it is in pdf file like as shown below.
$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>';
like that but i need to print data in pdf like this
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.
I need to print output like this in pdf file. In my Written code I am printing xml tags as it is. What should i do to print like, help me with this script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 XML::LibXML::Simple 来解析 XML 文件并这将允许您获取节点或属性的内容(不带标签)。
Use XML::LibXML::Simple instead to parse the XML file and this will allow you to get the content of the either the nodes or the attributes (without the tags).
您可以使用
$reader ->copyCurrentNode(1)
将元素及其子元素解析为 DOM 树,然后您可以从中提取所需的值,如下所示(警告:未经测试的代码):请参阅 XML: :LibXML::Element 了解有关如何从 DOM 中提取信息的更多信息。
You can use
$reader->copyCurrentNode(1)
to parse an element and its children into a DOM tree, from which you can then extract the values you want, like this (warning: untested code):See XML::LibXML::Element for more information on how to extract information from the DOM.