差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

下次修改
前次修改
tech:perl:xml_simple [2008/10/23 11:32] – 建立 jonathantech:perl:xml_simple [2021/01/16 14:38] (目前版本) – 更換 plugin jonathan
行 1: 行 1:
 +====== XML::Simple ======
 +XML::Simple - Easy API to maintain XML (esp config files)
  
 +  * http://cpan.uwinnipeg.ca/htdocs/XML-Simple/XML/Simple.html
 +
 +  * 可正常透過 perl -MCPAN -e shell 安裝
 +  * 執行時會出現 could not find ParserDetails.ini 訊息, 可透過以下語法解決 <cli>
 +perl -MXML::SAX -e "XML::SAX->add_parser(q(XML::SAX::PurePerl))->save_parsers()"
 +</cli>
 +===== 使用 Sample =====
 +<code perl>
 +#!/usr/bin/perl
 +
 +use XML::Simple qw(:strict);
 +
 +$ref = XMLin('/tmp/t.xml',
 +    ForceArray => 1,
 +    KeyAttr    => {},
 +);
 +
 +$receiverlist=$ref->{receiverlist}[0];
 +$i=0;
 +foreach $receiverinfo (@{$receiverlist->{object}}) {
 +        $i++;
 +        print("$i:");
 +        $orgid=$receiverinfo->{orgid}[0];
 +        $unitid=ref($receiverinfo->{unitid}[0]) eq "HASH"?"":$receiverinfo->{unitid}[0];
 +        print("[$orgid$unitid]\n");
 +}
 +exit;
 +</code>
 +
 +**/tmp/t.xml**
 +<code xml>
 +<?xml version="1.0" encoding="UTF-8"?>
 +<!DOCTYPE g2b2c-envelope SYSTEM "envelope.dtd">
 +<g2b2c-envelope>
 +:
 +:
 +<receiverlist>
 +<object type="addressbook">
 +<orgid>341000000A</orgid>
 +<unitid>U000020</unitid>
 +<orgname>行政院研究發展考核委員會</orgname>
 +<note></note>
 +</object>
 +<object type="addressbook">
 +<orgid>313000000G</orgid>
 +<unitid></unitid>
 +<orgname>經濟部</orgname>
 +<note></note>
 +</object>
 +</receiverlist>
 +:
 +:
 +</g2b2c-envelope>
 +</code>
 +
 +**執行結果**
 +<cli>
 +[root@rdtest02 tmp]# perl t1.pl
 +1:[341000000AU000020]
 +2:[313000000G]
 +</cli>
 +
 +<note>
 +安裝 XML::LibXML 過程出現找不到 libxml2 的問題, 但在 CentOS 內已經安裝完成 libxml2 以及 libxml2-devel 套件, 所以就放棄使用 XML::LibXML 這模組.
 +</note>
 +
 +===== 參考網頁 =====
 +  * [[http://perl-xml.sourceforge.net/faq/#parserdetails.ini|3.18. "could not find ParserDetails.ini"]]
 +  * [[http://search.cpan.org/dist/perl/pod/perlfunc.pod#ref|ref EXPR]]
 +  * [[http://www.perlmonks.org/index.pl?node_id=490846|Stepping up from XML::Simple to XML::LibXML]]
 +
 +{{tag>perl cpan xml}}