使用 XML::Simple 将 Perl 对象转换为 XML
我正在尝试使用 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 输出。提前致谢!
I'm attempting to use the XML::Simple CPAN module to convert output from our database into a simple XML structure. The problem is that the output being returned isn't what I was hoping for no matter what options I attempt to pass to XML::Simple.
The database table we're trying to output is just a bunch of items with the definition:
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`)
);
Here is the Perl code we're currently using:
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);
This outputs the following 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" />
I instead want it output as follows:
<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>
I'm sure there is some minor thing I'm overlooking so please let me know how I can change our current implementation to produce our desired XML output. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
请注意,我将其更改为使用推送,而不是计数并分配给最后一个数组索引,并且还初始化了项目,因此当没有时,您不会得到
数据行。Try:
Note that I changed it to use push instead of counting and assigning to the last array index and also initialized items so you don't get
<item></item>
when there are no data rows.