【问题标题】:How to use XML::Simple when XML is in an object?当 XML 在对象中时如何使用 XML::Simple?
【发布时间】:2010-11-25 14:10:13
【问题描述】:

我有这个代码

#!/usr/bin/perl -w

use strict;
use URI;
use LWP::UserAgent;
use Data::Dumper;
use XML::Simple;

my $DBVersion = '';

my $url = URI->new('https://example.com');

$url->query_form(
    'sql' => 'select email,firstname from account for xml auto',
    'DBVersion' => $DBVersion
    );

my $response = LWP::UserAgent->new->get($url);
die "Error: ", $response->status_line unless $response->is_success;

print $response->content;

现在我想在$response->content 上使用XML::Simple

From我可以看到他们使用

my $doc = $xs1->XMLin($file);

foreach my $key (keys (%{$doc->{species}})){
   print $doc->{species}->{$key}->{'common-name'} . ' (' . $key . ') ';
   print $doc->{species}->{$key}->{conservation}->final . "\n";
}

但我的 XML 数据不在文件中,而是在 LWP 模块创建的对象中。

如何使用XML::Simple 解析该数据?

【问题讨论】:

    标签: xml perl lwp


    【解决方案1】:

    XMLin()可以接受一串XML作为参数:

    use XML::Simple;
    my $ref = XMLin($response->content);
    

    详情请见perldoc XML::Simple

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      相关资源
      最近更新 更多