XML::简单且唯一的名称

发布于 2024-12-29 09:49:35 字数 830 浏览 1 评论 0原文

我很难制作一个 Perl 脚本来正确解析如下所示的 XML 文件:

<Report name="NAME">
<ReportHost name="UNIQUE_1"><HostProperties>
<tag name="TAG_1">tag_value</tag>
<tag name="TAG_2">tag_value</tag>
</ReportHost>
<ReportHost name="UNIQUE_2"><HostProperties>
<tag name="TAG_1">tag_value</tag>
<tag name="TAG_2">tag_value</tag>
</ReportHost>

现在,我需要能够以某种方式调用这些 UNIQUE_n,但我无法管理。 Dumper 返回如下结构:

'Report' => {
            'ReportHost' => {
                             'UNIQUE_1' => {
                                           'HostProperties' => {
                                                               'tag' => { [...]

我尝试了 ForceArray,但无法使 ReportHost 成为数组并惨败。

I'm having a hard time making a Perl script to correctly parse an XML file which looks like the following:

<Report name="NAME">
<ReportHost name="UNIQUE_1"><HostProperties>
<tag name="TAG_1">tag_value</tag>
<tag name="TAG_2">tag_value</tag>
</ReportHost>
<ReportHost name="UNIQUE_2"><HostProperties>
<tag name="TAG_1">tag_value</tag>
<tag name="TAG_2">tag_value</tag>
</ReportHost>

Now, I need to be able to call those UNIQUE_n somehow, but I couldn't manage. Dumper returns a structure like the following:

'Report' => {
            'ReportHost' => {
                             'UNIQUE_1' => {
                                           'HostProperties' => {
                                                               'tag' => { [...]

I tried ForceArray, but couldn't make ReportHost an array and failed miserably.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2025-01-05 09:49:35

你说你在让 Perl“正确解析”XML 时遇到困难,但你没有说你想要什么样的结果。抛开您的示例 XML 缺少一些结束标记这一事实不谈,也许您想要这样的内容:

my $report = XMLin(\*DATA,
    ForceArray => [ 'ReportHost', 'tag' ],
    KeyAttr    => { tag => 'name' },
    ContentKey => '-content',
);

print Dumper($report);

这给出了:

$VAR1 = {
      'ReportHost' => [
                      {
                        'HostProperties' => {
                                            'tag' => {
                                                     'TAG_1' => 'tag_value',
                                                     'TAG_2' => 'tag_value'
                                                   }
                                          },
                        'name' => 'UNIQUE_1'
                      },
                      {
                        'HostProperties' => {
                                            'tag' => {
                                                     'TAG_1' => 'tag_value',
                                                     'TAG_2' => 'tag_value'
                                                   }
                                          },
                        'name' => 'UNIQUE_2'
                      }
                    ],
      'name' => 'NAME'
};

您可以像这样循环访问数据:

my $report_hosts = $report->{ReportHost};
foreach my $report_host ( @$report_hosts ) {
    print "Report: $report_host->{name}\n";
    my $props = $report_host->{HostProperties}->{tag};
    print "  TAG_1: $props->{TAG_1}\n";
    print "  TAG_2: $props->{TAG_2}\n";
}

我会推荐 使用不同的模块:-)

You say you're having trouble getting Perl to "correctly parse" the XML But you don't say what sort of result you want. Putting aside the fact that your example XML is missing some closing tags, perhaps you're wanting something like this:

my $report = XMLin(\*DATA,
    ForceArray => [ 'ReportHost', 'tag' ],
    KeyAttr    => { tag => 'name' },
    ContentKey => '-content',
);

print Dumper($report);

Which gives:

$VAR1 = {
      'ReportHost' => [
                      {
                        'HostProperties' => {
                                            'tag' => {
                                                     'TAG_1' => 'tag_value',
                                                     'TAG_2' => 'tag_value'
                                                   }
                                          },
                        'name' => 'UNIQUE_1'
                      },
                      {
                        'HostProperties' => {
                                            'tag' => {
                                                     'TAG_1' => 'tag_value',
                                                     'TAG_2' => 'tag_value'
                                                   }
                                          },
                        'name' => 'UNIQUE_2'
                      }
                    ],
      'name' => 'NAME'
};

And you could loop through the data like this:

my $report_hosts = $report->{ReportHost};
foreach my $report_host ( @$report_hosts ) {
    print "Report: $report_host->{name}\n";
    my $props = $report_host->{HostProperties}->{tag};
    print "  TAG_1: $props->{TAG_1}\n";
    print "  TAG_2: $props->{TAG_2}\n";
}

I would recommend using a different module though :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文