2009-08-20 4 views
2

내가 만들 필요가 XML 파일은 내가 필요한 XML 파일을 생성하기 위해 여러 인쇄를 사용하지 않으 Perl로 XML 템플릿을 만드는 방법은 무엇입니까?

<file> 
    <state>$state</state> 
    <timestamp>$time</timestamp> 
    <location>$location</location> 
      .... 
</file> 

처럼, 내가 무엇을 기대하고있어 구조 모두를 정의하는 템플릿을하는 것입니다 및 XML 형식

XML 파일을 만들 때 템플릿의 변수에 대한 실제 값을 제공하고 지정된 템플릿을 새로 만든 파일에 한 번만 작성하면됩니다.

답변

8

사용 HTML::Template 그.

#!/usr/bin/perl 

use strict; 
use warnings; 

use HTML::Template; 

my $template_text = <<EO_TMPL; 
<TMPL_LOOP FILES> 
<file> 
    <state><TMPL_VAR STATE></state> 
    <timestamp><TMPL_VAR TIME></timestamp> 
    <location><TMPL_VAR LOCATION></location> 
</file> 
</TMPL_LOOP> 
EO_TMPL 

my $tmpl = HTML::Template->new(scalarref => \$template_text); 

$tmpl->param(
    FILES => [ 
    { state => 'one', time => 'two', location => 'three' }, 
    { state => 'alpha', time => 'beta', location => 'gamma' }, 
]); 

print $tmpl->output; 

출력 :

<file> 
    <state>one</state> 
    <timestamp>two</timestamp> 
    <location>three</location> 
</file> 

<file> 
    <state>alpha</state> 
    <timestamp>beta</timestamp> 
    <location>gamma</location> 
</file> 
+0

대답은 정확히 내가 필요로하는 것입니다. –

+1

안녕하세요! 좋은 ol 'html :: template! – innaM

관련 문제