Perl XML处理模块-XML::Simple
2017-09-02 09:10:51 阿炯

本站赞助商链接,请多关照。 XML::Simple - An API for simple XML files
 
简单的XML处理模块,提出的比较简单的接口来处理xml文档。
 
XML::Simple requires either XML::Parser or XML::SAX.
 
use XML::Simple qw(:strict);
 
my $xs = XML::Simple->new([<options>]);
 
my $ref = $xs->XMLin([<xml file or string>] [, <options>]);
 
my $xml = $xs->XMLout($hashref [, <options>]);

The XML::Simple module provides a simple API layer on top of an underlying XML parsing module (either XML::Parser or one of the SAX2 parser modules). Two functions are exported: XMLin() and XMLout(). Note: you can explicitly request the lower case versions of the function names: xml_in() and xml_out().

XML::Simple模块在底层XML解析模块之上提供了一个简单的API(XML::Parser或SAX2解析器模块)。有两个导出函数:XMLin()和XMLout()。注意:您可以显式地以小写名称来调用这两个函数:xml_in()和xml_out()。


XMLin()

Parses XML formatted data and returns a reference to a data structure which contains the same information in a more readily accessible form.

解析XML格式的数据并返回一个数据结构的引用,该数据类型将会以更容易访问的形式包含相同的信息。

XMLin() accepts an optional XML specifier followed by zero or more 'name => value' option pairs.

XMLin()接受一个可选的XML指示符,后面跟着零或更多的"key=>value"选项对来控制处理过程。


XMLout()


Takes a data structure (generally a hashref) and returns an XML encoding of that structure. If the resulting XML is parsed using XMLin(), it should return a data structure equivalent to the original.

获取数据结构(通常是hashref)并返回该结构的XML编码。如果使用XMLin()解析生成的XML,它应该返回相当于原来的数据结构。

When translating hashes to XML, hash keys which have a leading '-' will be silently skipped. This is the approved method for marking elements of a data structure which should be ignored by XMLout.

当将哈希转换为XML时,有一个“-”的哈希键将被无声地跳过。这是用于标记数据结构元素的批准方法,该方法应该被XMLout忽略。

XML::Simple is able to present a simple API because it makes some assumptions on your behalf. These include:

You're not interested in text content consisting only of whitespace

You don't mind that when things get slurped into a hash the order is lost

You don't want fine-grained control of the formatting of generated XML

You would never use a hash key that was not a legal XML element name

You don't need help converting between different encodings

由于功能支持的原因,如果需要更多的处理功能,作者推荐使用这两个模块:In particular, XML::LibXML is highly recommended and XML::Twig is an excellent alternative.

另注:如果只是将perl变量与xml文档间快速的转换,也可考虑使用XML::Dumper模块:XML::Dumper dumps Perl data to XML format. XML::Dumper can also read XML data that was previously dumped by the module and convert it back to Perl. You can use the module read the XML from a file and write the XML to a file.

最新版本:2.2


项目主页:https://metacpan.org/release/XML-Simple