如何在 perl 中打印一个空的 XML 元素

How to print an empty XML element in perl

我偶尔需要编写简单的 perl 脚本来将 XML 文件中的数据导出到 CSV 文件中,以便加载到数据库中。

我在 "print" 处理没有值的元素时遇到问题。它不打印任何内容,而是打印字符串 "HASH(0x1ca05f8)"(或其兄弟)。

如何阻止它这样做?

下面是我正在使用的代码,以及我正在使用的数据。谢谢,--sw

parse.pl:

#!/usr/bin/perl
#use module
use XML::Simple;
use Data::Dumper;

#create object
$xml = new XML::Simple;

#read XML file
$data = $xml->XMLin("$ARGV[0]", ForceArray=>1);

foreach $pr (@{$data->{product}})
{
  foreach $rv (@{$pr->{reviews}})
  {
    foreach $fr (@{$rv->{fullreview}})
    {
      print "$ARGV[1]", ",";
      print "$ARGV[2]", ",";
      print "$ARGV[3]", ",";
      print "$ARGV[4]", ",";
      print $pr->{"pageid"}->[0], ",";
      print $fr->{"status"}->[0], ",";
      print $fr->{"source"}->[0], ",";
      print $fr->{"createddate"}->[0], ",";
      print $fr->{"overallrating"}->[0], ",";
      print $fr->{"email_address_from_user"}->[0], ",";
      foreach $csg (@{$fr->{confirmstatusgroup}})
      {
        print join(";", @{$csg->{"confirmstatus"}});
      }


      print "\n";
    }
  }
}

data.xml:

<?xml version="1.0" encoding="UTF-8"?>
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<product xsi:type="ProductWithReviews" locale="en_US">
<pageid>bshnbat612</pageid>
<reviews>
<fullreview>
<status>Approved</status>
<createddate>2014-03-28</createddate>
<source>email</source>
<confirmstatusgroup>
<confirmstatus>Verified Purchaser</confirmstatus>
<confirmstatus>Verified Reviewer</confirmstatus>
</confirmstatusgroup>
<overallrating>5</overallrating>
<email_address_from_user/>
</fullreview>
</reviews>
</product>
</products>

这创建的输出:

,,,,bshnbat612,Approved,email,2014-03-28,5,HASH(0xe9fee8),Verified Purchaser;Verified Reviewer

为了回应下面提出的建议,这里是 Dumper 输出:

$VAR1 = {
    'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
    'product' => [
    {
        'xsi:type' => 'ProductWithReviews',
        'reviews' => [
        {
            'fullreview' => [
            {
                'source' => [
                    'email'
                ],
                'email_address_from_user' => [
                {}
                ],
                    'overallrating' => [
                        '5'
                    ],
                    'confirmstatusgroup' => [
                    {
                        'confirmstatus' => [
                            'Verified Purchaser',
                        'Verified Reviewer'
                        ]
                    }
                    ],
                        'status' => [
                            'Approved'
                        ],
                        'createddate' => [
                            '2014-03-28'
                        ]
            }
            ]
        }
        ],
            'pageid' => [
                'bshnbat612'
            ],
            'locale' => 'en_US'
    }
    ]
};

看看可以传递给 XML::Simple 的 SuppressEmpty 选项。没有它,XML::Simple 将为空元素提供空散列。通过调用 XMLin("$ARGV[0]", ForceArray=>1, SuppressEmpty=>1); 你的输出应该是:,,,,bshnbat612,Approved,email,2014-03-28,5,,Verified Purchaser;Verified Reviewer

好的,XML::Simple 文档中有一个重要提示:

The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended.

不过就我个人而言,我喜欢 XML::Twig:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

sub print_full_review {
    my ( $twig, $full_review ) = @_;
    my $pageid =
        $twig->root->get_xpath( '/products/product/pageid', 0 )->text;

    print join(
        ",",
        @ARGV[ 1 .. 4 ],
        $pageid,
        $full_review->first_child_text('status'),
        $full_review->first_child_text('source'),
        $full_review->first_child_text('createddate'),
        $full_review->first_child_text('overallrating'),
        $full_review->first_child_text('email_address_from_user'),
        join( ";",
            map { $_->text }
                $full_review->first_child('confirmstatusgroup')->children() )
        ),
        "\n";
}

my $twig = XML::Twig->new(
    'pretty_print'  => 'indented_a',
    'twig_handlers' => { 'fullreview' => \&print_full_review }
);
$twig->parsefile( $ARGV[0] );

每次解析器遇到 fullreview 元素时都会触发处理程序 'print_full_review'(在树中的任何级别 - 您可以通过将其设置为处理 /product/products/reviews/fullreview 来更具体,如果这是个问题。

此处理程序通过 fullreview 元素进行处理。

我们从中提取您寻求的价值。

join( ";",
    map { $_->text }
        $full_review->first_child('confirmstatusgroup')->children() )

是稍微复杂一点的做法:

my $confirmstatusgroup = $full_review -> first_child('confirmstatusgroup');
foreach my $confirmstatus ( $confirmstatusgroup -> children ) { 
    print $confirmstatus -> text,";";
}

但是上面的代码产生了您想要的输出,但不必像使用 XML::Simple 那样做任何类型的 'suppressempty' 捏造。