我正在尝试使用 XML::Simple CPAN 模块将我们数据库的输出转换为简单的 XML 结构。问题是,无论我尝试将什么选项传递给 XML::Simple,返回的输出都不是我所希望的。
我们试图输出的数据库表只是一堆具有定义的项目:
CREATE TABLE `items` (
`id` int(8) NOT NULL,
`name` varchar(40) NOT NULL,
`manufacturer` varchar(40) NOT NULL,
`added` datetime NOT NULL,
PRIMARY KEY (`id`)
);
这是我们当前使用的 Perl 代码:
my $SQL = qq| select * from items |;
my $sth = main::DatabaseQuery($SQL);
my $items;
my $count = 0;
while(my $item = $sth->fetchrow_hashref()) {
$items->[$count] = $item;
$count++;
}
require XML::Simple;
my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1);
my $xml = $xs->XMLout($ref);
这将输出以下 XML:
<anon id="10000" name="Item 1" manufacturer="Manufacturer 1" added="2009-01-01" />
<anon id="10001" name="Item 2" manufacturer="Manufacturer 2" added="2009-01-01" />
<anon id="10002" name="Item 3" manufacturer="Manufacturer 3" added="2009-01-01" />
我希望它输出如下:
<items>
<item>
<id>10000</id>
<name>Item 1</name>
<manufacturer>Manufacturer 1</manufacturer>
<added>2009-01-01</added>
</item>
<item>
<id>10001</id>
<name>Item 2</name>
<manufacturer>Manufacturer 2</manufacturer>
<added>2009-01-01</added>
</item>
<item>
<id>10002</id>
<name>Item 3</name>
<manufacturer>Manufacturer 3</manufacturer>
<added>2009-01-01</added>
</item>
</items>
我确信我忽略了一些小事情,所以请告诉我如何更改我们当前的实现以产生我们想要的 XML 输出。提前致谢!