如何从 Perl 创建 XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/154762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:07:15  来源:igfitidea点击:

How can I create XML from Perl?

xmlperl

提问by pkaeding

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

我需要在 Perl 中创建 XML。从我读到的内容来看,XML::LibXML非常适合解析和使用来自其他地方的 XML。有人对 XML Writer 有什么建议吗?是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!

谢谢!

采纳答案by Yanick

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();

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

回答by Cosimo

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>

回答by David Precious

If you want to take a data structure in Perl and turn it into XML, XML::Simplewill 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。

回答by Matt Siegman

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

我不做太多的 XML,但XML::Smart看起来它可以做你想做的。查看文档中的创建 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 :)

凉爽的。我将不得不玩这个:)

回答by Bill Turner

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

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

回答by skiphoppy

I like XML::TreeBuilderbecause 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 对象中表示处理指令和声明。