使用 XML::Simple 将 Perl 对象转换为 XML

发布于 2024-09-16 14:19:52 字数 1822 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(1

只是偏爱你 2024-09-23 14:19:52

尝试:

my $SQL = qq| select * from items |;
my $sth = main::DatabaseQuery($SQL);

my $items = [];
while(my $item = $sth->fetchrow_hashref()) {
    push @$items, $item;
}

use XML::Simple;
my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1, NoAttr => 1);
my $xml = $xs->XMLout( { items => { item => $items } } );

请注意,我将其更改为使用推送,而不是计数并分配给最后一个数组索引,并且还初始化了项目,因此当没有时,您不会得到 数据行。

Try:

my $SQL = qq| select * from items |;
my $sth = main::DatabaseQuery($SQL);

my $items = [];
while(my $item = $sth->fetchrow_hashref()) {
    push @$items, $item;
}

use XML::Simple;
my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1, NoAttr => 1);
my $xml = $xs->XMLout( { items => { item => $items } } );

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.

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