【问题标题】:Generating XML like code from a file从文件生成类似 XML 的代码
【发布时间】:2013-01-11 01:06:59
【问题描述】:

已经为我设定了目标,但我不知道如何到达那里。提前道歉。

使用 Perl - 我将收到一个字符分隔文件(我可以指定其结构),我需要将其转换为 XML like 文件

<MyConfig>
   Active = yes
   Refresh = 10 min
 <Includes>
 <Include_Rule_1>
 Active = yes
 Match = /Foo [Bb]ar/
 </Include_Rule_1>
 <Include_Rule_2>
 Active = yes
 Match = /Baz.*/
<Include_Rule_2>
</Include>
<Exclude>
<Exclude_Rule_1>
Exclude = /Bim Bam/
</Exclude_Rule_1>
 </Exclude>
</MyConfig>

所以简而言之,它将是 XML like(各个值不被尖括号包围),具有 3 个不变的部分,但它们的深度总是未知的。

我可以使用 CPAN 库,但不喜欢,因为此脚本需要在我无法访问或控制的单个服务器上运行。

有什么想法吗?首发指针 ?提示或技巧?

对不起,我在这里迷路了。

【问题讨论】:

  • 你能提供一个示例输入吗?
  • 不清楚你想要生成这个XML文件的确切方式,但是,看看stackoverflow.com/questions/4142046/create-xml-file-using-java
  • 好吧,因为我可以说明输入看起来像我没有包含它,以免限制解决方案
  • 看看split 函数将一行分成字段,然后是 XML::Generator perl 模块(查看 [CPAN](cpan.org))。这是一个简单的 XML 输出模块。
  • 欢迎来到 SO。花点时间看看您的示例输出。您是否误漏了&lt;/Includes&gt;?为什么第一个“Active”和“Refresh”属性不是“Includes_Rule”的一部分?你可能想看看 YAML standard 和 Perl 模块,如 YAML::Tiny

标签: xml perl csv


【解决方案1】:

您可以像在这个入门示例中一样使用Template::Toolkit 模块(您需要先解析输入文件以提供 HASH):

#!/usr/bin/env perl

use strict; use warnings;
use Template;
my $tt = Template->new;
my $input = {
    MyConfig_Active => "yes",
    MyConfig_refresh => "10 min",
    MyConfig_Include_Rule_1_Active => "yes",
    MyConfig_Include_Rule_1_Match => "/Foo [Bb]ar/"
};
$tt->process('template.tpl', $input)
    || die $tt->error;

template.tpl 文件:

<MyConfig>
   Active = [% MyConfig_Active %]
   Refresh =  [% MyConfig_refresh %]
 <Includes>
 <Include_Rule_1>
 Active = [% MyConfig_Include_Rule_1_Active %]
 Match = "[% MyConfig_Include_Rule_1_Match %]"
 </Include_Rule_1>
(...)
</MyConfig>

样本输出:

<MyConfig>
   Active = yes
   Refresh =  10 min
 <Includes>
 <Include_Rule_1>
 Active = yes
 Match = "/Foo [Bb]ar/"
 </Include_Rule_1>
(...)
</MyConfig>

http://template-toolkit.org/docs/tutorial/Datafile.html

【讨论】:

  • 如果您想感谢,请考虑upvote。如果您认为问题得到了很好的回答,您也可以接受。这就是 stackexchange 网站的工作方式。谢谢=)
  • 我已经尝试过了,但除非我的声誉至少达到 15,否则我不允许投票
【解决方案2】:

一种选择是使用Config::General 从您从解析的字符分隔文件的结果中填充的哈希生成这样的文件。可以使用 Config::General 轻松读取此文件以重新填充哈希:

use strict;
use warnings;
use Config::General;

my %config = (
    Active   => 'yes',
    Refresh  => '10 min',
    Includes => {
        Include_Rule_1 => {
            Active => 'yes',
            Match  => '/Foo [Bb]ar/'
        },
        Include_Rule_2 => {
            Active => 'yes',
            Match  => '/Baz.*/'
        }
    },
    Excludes => { 
        Exclude_Rule_1 => { 
            Exclude => '/Bim Bam/'
        }
    }

);

my $conf = new Config::General();

# Save structure to file
$conf->save_file('myConfig.conf', \%config);

myConfig.conf的内容:

<Excludes Exclude_Rule_1>
    Exclude   /Bim Bam/
</Excludes>
Refresh   10 min
<Includes>
    <Include_Rule_1>
        Match   /Foo [Bb]ar/
        Active   yes
    </Include_Rule_1>
    <Include_Rule_2>
        Match   /Baz.*/
        Active   yes
    </Include_Rule_2>
</Includes>
Active   yes

【讨论】:

  • Config::General 看起来很有希望,我整天都在玩它并阅读 cpan 文档。 1)它似乎以相反的顺序打印东西。 2)它似乎结合了嵌套块“”而不是“”和“” 3)如果有一个在值之间添加“=”的开关会很好 4)看起来处理好“包含”部分我不知道为什么谢谢!
猜你喜欢
  • 2010-11-17
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 2015-02-09
  • 2018-05-15
  • 2017-12-12
相关资源
最近更新 更多