如何访问 XML::Simple 返回的数据结构中的值?

发布于 2024-12-21 08:11:21 字数 2508 浏览 1 评论 0原文

大家好,
这对于 Perl 程序员来说非常简单,但对于像我这样的初学者来说却不是,
我有一个 xml 文件,我使用 XML::Simple 像这样

my $file="service.xml";
my $xml = new XML::Simple;
 my $data = $xml->XMLin("$file", ForceArray =>  ['Service','SystemReaction',  
                                      'Customers', 'Suppliers','SW','HW'],);

转储 $data 进行处理,它看起来像这样:

$data = {
   'Service' => [{
         'Suppliers' => [{
               'SW' => [
                  {'Path' => '/work/service.xml',  'Service' => 'b7a'},
                  {'Path' => '/work/service1.xml', 'Service' => 'b7b'},
                  {'Path' => '/work/service2.xml', 'Service' => 'b5'}]}
         ],
         'Id' => 'SKRM',
         'Customers' =>
            [{'SW' => [{'Path' => '/work/service.xml', 'Service' => 'ASOC'}]}],
         'Des'  => 'Control the current through the pipe',
         'Name' => ' Control unit'
      },
      {
         'Suppliers' => [{
               'HW' => [{
                     'Type'    => 'W',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '18',
                     'Service' => '1'
                  },
                  {
                     'Type'    => 'B',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '7',
                     'Service' => '1'
                  },
                  {
                     'Type'    => 'k',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '1',
                     'Service' => '1'
                  }]}
         ],
         'Id' => 'ADTM',
         'Customers' =>
            [{'SW' => [{'Path' => '/work/service.xml', 'Service' => 'SDCR'}]}],
         'Des'  => 'It delivers actual motor speed',
         'Name' => ' Motor Drivers and Diognostics'
      },
      # etc.
   ],
   'Systemreaction' => [
      # etc.
   ],
};

如何访问服务和 systemReaction 中的每个元素(未提供)。因为我在进一步处理中使用“$data”。所以我需要访问每个服务中的每个 ID、客户、供应商值。如何从服务获取特定值以使用该值执行某些处理。例如,我需要从服务获取所有 Id 值并为每个 id 值创建节点。

为了获取类型和 Nr 值,我尝试了这样的操作,

 foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW}[0] }) {
 say $service->{Nr};
 }
 foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW}[0] }) {
 say $service->{Type};

  }

您可以帮助我如何从供应商 -> HW 获取所有 Nr 和类型值。

Hi everyone,
This is very simple for perl programmers but not beginners like me,
I have one xml file and I processed using XML::Simple like this

my $file="service.xml";
my $xml = new XML::Simple;
 my $data = $xml->XMLin("$file", ForceArray =>  ['Service','SystemReaction',  
                                      'Customers', 'Suppliers','SW','HW'],);

Dumping out $data, it looks like this:

$data = {
   'Service' => [{
         'Suppliers' => [{
               'SW' => [
                  {'Path' => '/work/service.xml',  'Service' => 'b7a'},
                  {'Path' => '/work/service1.xml', 'Service' => 'b7b'},
                  {'Path' => '/work/service2.xml', 'Service' => 'b5'}]}
         ],
         'Id' => 'SKRM',
         'Customers' =>
            [{'SW' => [{'Path' => '/work/service.xml', 'Service' => 'ASOC'}]}],
         'Des'  => 'Control the current through the pipe',
         'Name' => ' Control unit'
      },
      {
         'Suppliers' => [{
               'HW' => [{
                     'Type'    => 'W',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '18',
                     'Service' => '1'
                  },
                  {
                     'Type'    => 'B',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '7',
                     'Service' => '1'
                  },
                  {
                     'Type'    => 'k',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '1',
                     'Service' => '1'
                  }]}
         ],
         'Id' => 'ADTM',
         'Customers' =>
            [{'SW' => [{'Path' => '/work/service.xml', 'Service' => 'SDCR'}]}],
         'Des'  => 'It delivers actual motor speed',
         'Name' => ' Motor Drivers and Diognostics'
      },
      # etc.
   ],
   'Systemreaction' => [
      # etc.
   ],
};

How to access each elements in the service and systemReaction(not provided). because I am using "$data" in further processing. So I need to access each Id,customers, suppliers values in each service. How to get particular value from service to do some process with that value.for example I need to get all Id values form service and create nodes for each id values.

To get Type and Nr value I tried like this

 foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW}[0] }) {
 say $service->{Nr};
 }
 foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW}[0] }) {
 say $service->{Type};

  }

can you help me how to get all Nr and Type values from Supplier->HW.

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

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

发布评论

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

评论(1

鼻尖触碰 2024-12-28 08:11:21

我建议阅读 perldocs 参考教程引用和嵌套数据结构。它们包含如何访问此类数据的介绍和完整解释。

但是,例如,您可以通过执行以下操作来访问服务 ID:

say $data->{Service}[0]{Id}   # prints SKRM

您可以通过循环遍历所有服务,打印其 ID:

foreach my $service (@{ $data->{Service} }) {
    say $service->{Id};
}

响应您的编辑

$data->{Service}[1]{ Supplys}[0]{HW}[0] 是一个哈希引用(您可以使用 Data::DumperData::Dump 就可以了,或者只是 ref 函数)。特别地,它是 { Nr => 18、路径=> “/work/hardware.xml”,服务=> 1、类型=> "W" }

换句话说,你几乎已经明白了——你只是走得太深了一层。它应该是:

foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW} }) {
 say $service->{Nr};
}

注意缺少最终的[0] 。

I suggest reading perldocs Reference Tutorial and References and Nested Data Structures. They contain an introduction and full explanation of how to access data like that.

But, for example, you can access the service ID by doing:

say $data->{Service}[0]{Id}   # prints SKRM

You could go through all the services, printing their ID, with a loop:

foreach my $service (@{ $data->{Service} }) {
    say $service->{Id};
}

In response to your edit

$data->{Service}[1]{Suppliers}[0]{HW}[0] is an hash reference (you can check this quickly by either using Data::Dumper or Data::Dump on it, or just the ref function). In particular, it is { Nr => 18, Path => "/work/hardware.xml", Service => 1, Type => "W" }

In other words, you've almost got it—you just went one level too deep. It should be:

foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW} }) {
 say $service->{Nr};
}

Note the lack of the final [0] that you had.

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