如何使用Perl的XML::Simple生成XML ?

时间:2021-10-21 14:31:32

I am just trying to create the XML

我只是在尝试创建XML

use XML::Simple;

my %element = ( "a" => "10" , 
                "b" => "20" ,);

my $xs = new XML::Simple();
my $ref = $xs->XMLin(%element);
my $xml = $xs->XMLout($ref);
print $xml;

What is wrong on this code ? ( This is got Resolved )

这段代码有什么问题?(问题解决了)

use XML::Simple;

my %element = ( "a" => "10" , 
                "b" => "20" ,);

my $xs = new XML::Simple();
my $xml = $xs->XMLout(\%element);
print $xml;

This produces the following output:

这将产生以下输出:

<opt a="10" b="20" />

But what I'd really like to get is this:

但我真正想要的是:

<a> 10 </a>
<b> 20 </b>

How can I achieve this?

我如何做到这一点?

2 个解决方案

#1


4  

XMLin takes XML and transforms it into a Perl data structure.

XMLin将XML转换为Perl数据结构。

XMLout takes a Perl data strcuture and transforms it to XML.

XMLout获取Perl数据字符串并将其转换为XML。

You are trying to feed a Perl data structure to XMLin and feed the result to XMLout.

您正在尝试向XMLin提供Perl数据结构,并将结果提供给XMLout。

Have you considered reading the documentation?

你考虑过阅读文档吗?


Update: The documentation (yes, we know it's boring) offers quite a lot of options that you can pass to XML::Simple::new(). One of them is NoAttr => 1. You might want to check that one out, but a look at the others (some of which are marked "important") won't hurt.

更新:文档(是的,我们知道它很无聊)提供了很多可以传递给XML的选项::Simple::new()。其中一个是NoAttr => 1。您可能想要检查一下,但是看看其他的(有些标记为“重要的”)不会有什么坏处。

#2


2  

You're using it backwards. XMLin is an XML decoder (takes an XML-encoded document, returns Perl structures); XMLout is an XML encoder (takes Perl structures, returns an XML-encoded document).

你使用它向后。XMLin是一个XML解码器(采用XML编码的文档,返回Perl结构);XMLout是一个XML编码器(采用Perl结构,返回XML编码的文档)。

#1


4  

XMLin takes XML and transforms it into a Perl data structure.

XMLin将XML转换为Perl数据结构。

XMLout takes a Perl data strcuture and transforms it to XML.

XMLout获取Perl数据字符串并将其转换为XML。

You are trying to feed a Perl data structure to XMLin and feed the result to XMLout.

您正在尝试向XMLin提供Perl数据结构,并将结果提供给XMLout。

Have you considered reading the documentation?

你考虑过阅读文档吗?


Update: The documentation (yes, we know it's boring) offers quite a lot of options that you can pass to XML::Simple::new(). One of them is NoAttr => 1. You might want to check that one out, but a look at the others (some of which are marked "important") won't hurt.

更新:文档(是的,我们知道它很无聊)提供了很多可以传递给XML的选项::Simple::new()。其中一个是NoAttr => 1。您可能想要检查一下,但是看看其他的(有些标记为“重要的”)不会有什么坏处。

#2


2  

You're using it backwards. XMLin is an XML decoder (takes an XML-encoded document, returns Perl structures); XMLout is an XML encoder (takes Perl structures, returns an XML-encoded document).

你使用它向后。XMLin是一个XML解码器(采用XML编码的文档,返回Perl结构);XMLout是一个XML编码器(采用Perl结构,返回XML编码的文档)。