如何从Perl创建XML ?

时间:2023-01-15 20:46:28

I need to create XML in Perl. From what I read, XML::LibXML is great for parsing and using XML that comes from somewhere else. Does anyone have any suggestions for an XML Writer? Is XML::Writer still maintained? Does anyone like/use it?

我需要用Perl创建XML。从我读到的内容来看,XML::LibXML非常适合解析和使用来自其他地方的XML。有人对XML作者有什么建议吗?XML::作家仍然保持?有人喜欢/使用它吗?

In addition to feature-completeness, I am interested an easy-to-use syntax, so please describe the syntax and any other reasons why you like that module in your answer.

除了特性完整性之外,我还对易于使用的语法感兴趣,因此请在回答中描述语法以及您喜欢该模块的其他原因。

Please respond with one suggestion per answer, and if someone has already answered with your favorite, please vote that answer up. Hopefully it will be easy to see what is most popular.

请回答一个建议,如果有人已经回答了你的最爱,请投票回答。希望能很容易地看出什么是最流行的。

Thanks!

谢谢!

6 个解决方案

#1


21  

XML::Writer is still maintained (at least, as of February of this year), and it's indeed one of the favorite Perl XML writers out there.

XML::Writer仍然被维护(至少到今年2月为止),它确实是最受欢迎的Perl XML作者之一。

As for describing the syntax, one is better to look at the module's documentation (the link is already in the question). To wit:

至于描述语法,最好查看模块的文档(这个链接已经有问题了)。即:

 use XML::Writer;

  my $writer = new XML::Writer();  # will write to stdout
  $writer->startTag("greeting", 
                    "class" => "simple");
  $writer->characters("Hello, world!");
  $writer->endTag("greeting");
  $writer->end();
  $output->close();

  # produces <greeting class='simple'>Hello world!</greeting>

#2


29  

Just for the record, here's a snippet that uses XML::LibXML.

为了便于记录,这里有一个使用XML::LibXML的代码片段。

#!/usr/bin/env perl

#
# Create a simple XML document
#

use strict;
use warnings;
use XML::LibXML;

my $doc = XML::LibXML::Document->new('1.0', 'utf-8');

my $root = $doc->createElement("my-root-element");
$root->setAttribute('some-attr'=> 'some-value');

my %tags = (
    color => 'blue',
    metal => 'steel',
);

for my $name (keys %tags) {
    my $tag = $doc->createElement($name);
    my $value = $tags{$name};
    $tag->appendTextNode($value);
    $root->appendChild($tag);
}

$doc->setDocumentElement($root);
print $doc->toString();

and this outputs:

这输出:

<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value">
    <color>blue</color>
    <metal>steel</metal>
</my-root-element>

#3


9  

If you want to take a data structure in Perl and turn it into XML, XML::Simple will do the job nicely.

如果您希望使用Perl中的数据结构并将其转换为XML,那么XML::Simple将很好地完成这项工作。

At its simplest:

在其最简单的:

my $hashref = { foo => 'bar', baz => [ 1, 2, 3 ] };
use XML::Simple;
my $xml = XML::Simple::XMLout($hashref);

As its name suggests, its basic usage is simple; however it does offer a lot of features if you need them.

顾名思义,它的基本用法很简单;但是,如果您需要,它确实提供了许多特性。

Naturally, it can also parse XML easily.

当然,它也可以很容易地解析XML。

#4


8  

I don't do much XML, but XML::Smart looks like it might do what you want. Take a look at the section Creating XML Data in the doc and it looks very simple and easy to use.

我不太使用XML,但是XML::Smart看起来可以做您想做的事情。看看在doc中创建XML数据的部分,它看起来非常简单和易于使用。

Paraphrasing the doc:

套用医生:

use XML::Smart;

## Create a null XML object:
my $XML = XML::Smart->new() ;

## Add a server to the list:
$XML->{server} = {
    os => 'Linux' ,
    type => 'mandrake' ,
    version => 8.9 ,
    address => [ '192.168.3.201', '192.168.3.202' ] ,
} ;

$XML->save('newfile.xml') ;

Which would put this in newfile.xml:

也就是newfile。xml:

<server os="Linux" type="mandrake" version="8.9">
  <address>192.168.3.201</address>
  <address>192.168.3.202</address>
</server>

Cool. I'm going to have to play with this :)

酷。我要玩这个:)

#5


3  

XML::Smart looks nice, but I don't remember it being available when I was using XML::Simple many years ago. Nice interface, and works well for reading and writing XML.

XML::Smart看起来不错,但我不记得多年前我使用XML::Simple时它是可用的。良好的界面,非常适合XML的读写。

#6


1  

I like XML::TreeBuilder because it fits the way I think. Historically, I've used it more for parsing than emitting.

我喜欢XML: TreeBuilder,因为它符合我的想法。在历史上,我更多地使用它进行解析,而不是发出。

A few weeks after this question was posted, I had occasion to generate some XML from Perl. I surveyed the other modules listed here, assuming one of them would work better than XML::TreeBuilder, but TreeBuilder was still the best choice for what I wanted.

在这个问题发布几周之后,我有机会从Perl中生成一些XML。我研究了这里列出的其他模块,假设其中一个模块的工作性能比XML::TreeBuilder更好,但TreeBuilder仍然是我想要的最佳选择。

One drawback was there is no way to represent processing instructions and declarations in an XML::TreeBuilder object.

其中一个缺点是无法在XML::TreeBuilder对象中表示处理指令和声明。

#1


21  

XML::Writer is still maintained (at least, as of February of this year), and it's indeed one of the favorite Perl XML writers out there.

XML::Writer仍然被维护(至少到今年2月为止),它确实是最受欢迎的Perl XML作者之一。

As for describing the syntax, one is better to look at the module's documentation (the link is already in the question). To wit:

至于描述语法,最好查看模块的文档(这个链接已经有问题了)。即:

 use XML::Writer;

  my $writer = new XML::Writer();  # will write to stdout
  $writer->startTag("greeting", 
                    "class" => "simple");
  $writer->characters("Hello, world!");
  $writer->endTag("greeting");
  $writer->end();
  $output->close();

  # produces <greeting class='simple'>Hello world!</greeting>

#2


29  

Just for the record, here's a snippet that uses XML::LibXML.

为了便于记录,这里有一个使用XML::LibXML的代码片段。

#!/usr/bin/env perl

#
# Create a simple XML document
#

use strict;
use warnings;
use XML::LibXML;

my $doc = XML::LibXML::Document->new('1.0', 'utf-8');

my $root = $doc->createElement("my-root-element");
$root->setAttribute('some-attr'=> 'some-value');

my %tags = (
    color => 'blue',
    metal => 'steel',
);

for my $name (keys %tags) {
    my $tag = $doc->createElement($name);
    my $value = $tags{$name};
    $tag->appendTextNode($value);
    $root->appendChild($tag);
}

$doc->setDocumentElement($root);
print $doc->toString();

and this outputs:

这输出:

<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value">
    <color>blue</color>
    <metal>steel</metal>
</my-root-element>

#3


9  

If you want to take a data structure in Perl and turn it into XML, XML::Simple will do the job nicely.

如果您希望使用Perl中的数据结构并将其转换为XML,那么XML::Simple将很好地完成这项工作。

At its simplest:

在其最简单的:

my $hashref = { foo => 'bar', baz => [ 1, 2, 3 ] };
use XML::Simple;
my $xml = XML::Simple::XMLout($hashref);

As its name suggests, its basic usage is simple; however it does offer a lot of features if you need them.

顾名思义,它的基本用法很简单;但是,如果您需要,它确实提供了许多特性。

Naturally, it can also parse XML easily.

当然,它也可以很容易地解析XML。

#4


8  

I don't do much XML, but XML::Smart looks like it might do what you want. Take a look at the section Creating XML Data in the doc and it looks very simple and easy to use.

我不太使用XML,但是XML::Smart看起来可以做您想做的事情。看看在doc中创建XML数据的部分,它看起来非常简单和易于使用。

Paraphrasing the doc:

套用医生:

use XML::Smart;

## Create a null XML object:
my $XML = XML::Smart->new() ;

## Add a server to the list:
$XML->{server} = {
    os => 'Linux' ,
    type => 'mandrake' ,
    version => 8.9 ,
    address => [ '192.168.3.201', '192.168.3.202' ] ,
} ;

$XML->save('newfile.xml') ;

Which would put this in newfile.xml:

也就是newfile。xml:

<server os="Linux" type="mandrake" version="8.9">
  <address>192.168.3.201</address>
  <address>192.168.3.202</address>
</server>

Cool. I'm going to have to play with this :)

酷。我要玩这个:)

#5


3  

XML::Smart looks nice, but I don't remember it being available when I was using XML::Simple many years ago. Nice interface, and works well for reading and writing XML.

XML::Smart看起来不错,但我不记得多年前我使用XML::Simple时它是可用的。良好的界面,非常适合XML的读写。

#6


1  

I like XML::TreeBuilder because it fits the way I think. Historically, I've used it more for parsing than emitting.

我喜欢XML: TreeBuilder,因为它符合我的想法。在历史上,我更多地使用它进行解析,而不是发出。

A few weeks after this question was posted, I had occasion to generate some XML from Perl. I surveyed the other modules listed here, assuming one of them would work better than XML::TreeBuilder, but TreeBuilder was still the best choice for what I wanted.

在这个问题发布几周之后,我有机会从Perl中生成一些XML。我研究了这里列出的其他模块,假设其中一个模块的工作性能比XML::TreeBuilder更好,但TreeBuilder仍然是我想要的最佳选择。

One drawback was there is no way to represent processing instructions and declarations in an XML::TreeBuilder object.

其中一个缺点是无法在XML::TreeBuilder对象中表示处理指令和声明。