简体   繁体   English

如何从 Perl 创建 XML?

[英]How can I create XML from Perl?

I need to create XML in Perl.我需要在 Perl 中创建 XML。 From what I read, XML::LibXML is great for parsing and using XML that comes from somewhere else.根据我的阅读, XML::LibXML非常适合解析和使用来自其他地方的 XML。 Does anyone have any suggestions for an XML Writer?有人对 XML Writer 有什么建议吗? Is XML::Writer still maintained? XML::Writer是否仍在维护? Does anyone like/use it?有人喜欢/使用它吗?

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!谢谢!

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 %elements = (
    color => 'blue',
    metal => 'steel',
);

for my $name (keys %elements) {
    my $tag = $doc->createElement($name);
    my $value = $elements{$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>

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>

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。

EDIT: I wrote this back in Oct 2008, 14 years ago this year.编辑:我在今年 14 年前的 2008 年 10 月写了这篇文章。 Things have changed since then.从那以后情况发生了变化。 XML::Simple's own documentation carries a clear warning: XML::Simple 自己的文档带有明确的警告:

The use of this module in new code is strongly discouraged. 强烈建议不要在新代码中使用此模块。 Other modules are available which provide more straightforward and consistent interfaces. 其他模块也可以提供更直接和一致的接口。 In particular, XML::LibXML is highly recommended and you can refer to for a tutorial introduction. 特别推荐使用XML::LibXML ,教程介绍可以参考。 XML::Twig is another excellent alternative. XML::Twig是另一个很好的选择。

These days, I'd strongly recommend checking those out rather than using XML::Simple in new code.这些天来,我强烈建议检查这些而不是在新代码中使用 XML::Simple。

I don't do much XML, but XML::Smart looks like it might do what you want.我没有做太多 XML,但XML::Smart看起来可能会做你想做的事。 Take a look at the section Creating XML Data in the doc and it looks very simple and easy to use.查看文档中的创建 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 :)我将不得不玩这个:)

XML::Smart looks nice, but I don't remember it being available when I was using XML::Simple many years ago. XML::Smart 看起来不错,但我不记得多年前使用XML::Simple时它是否可用。 Nice interface, and works well for reading and writing XML.界面不错,非常适合阅读和编写 XML。

I like XML::TreeBuilder because it fits the way I think.我喜欢XML::TreeBuilder因为它符合我的想法。 Historically, I've used it more for parsing than emitting.从历史上看,我更多地将它用于解析而不是发射。

A few weeks after this question was posted, I had occasion to generate some XML from Perl.这个问题发布几周后,我有机会从 Perl 生成一些 XML。 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.我调查了这里列出的其他模块,假设其中一个会比 XML::TreeBuilder 工作得更好,但 TreeBuilder 仍然是我想要的最佳选择。

One drawback was there is no way to represent processing instructions and declarations in an XML::TreeBuilder object.一个缺点是无法在 XML::TreeBuilder 对象中表示处理指令和声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM