📄 faq.html
字号:
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../../displayToc.js"></script>
<script language="JavaScript" src="../../../tocParas.js"></script>
<script language="JavaScript" src="../../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../../scineplex.css">
<title>Frequently Asked Questions about XML::Simple</title>
<link rel="stylesheet" href="../../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',3);</script>
<h1><a>Frequently Asked Questions about XML::Simple</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#frequently_asked_questions_about_xml__simple">Frequently Asked Questions about XML::Simple</a></li>
<li><a href="#basics">Basics</a></li>
<ul>
<li><a href="#what_is_xml__simple_designed_to_be_used_for">What is XML::Simple designed to be used for?</a></li>
<li><a href="#why_store_configuration_data_in_xml_anyway">Why store configuration data in XML anyway?</a></li>
<li><a href="#what_isn_t_xml__simple_good_for">What isn't XML::Simple good for?</a></li>
<li><a href="#what_is_mixed_content">What is mixed content?</a></li>
<li><a href="#why_doesn_t_xml__simple_handle_mixed_content">Why doesn't XML::Simple handle mixed content?</a></li>
<li><a href="#which_perl_modules_do_handle_mixed_content">Which Perl modules do handle mixed content?</a></li>
</ul>
<li><a href="#installation">Installation</a></li>
<ul>
<li><a href="#how_do_i_install_xml__simple">How do I install XML::Simple?</a></li>
<li><a href="#i_m_trying_to_install_xml__simple_and__make_test__fails">I'm trying to install XML::Simple and 'make test' fails</a></li>
<li><a href="#why_is_xml__simple_so_slow">Why is XML::Simple so slow?</a></li>
</ul>
<li><a href="#usage">Usage</a></li>
<ul>
<li><a href="#how_do_i_use_xml__simple">How do I use XML::Simple?</a></li>
<li><a href="#there_are_so_many_options__which_ones_do_i_really_need_to_know_about">There are so many options, which ones do I really need to know about?</a></li>
<li><a href="#what_is_the_forcearray_option_all_about">What is the forcearray option all about?</a></li>
<li><a href="#what_is_the_keyattr_option_all_about">What is the keyattr option all about?</a></li>
<li><a href="#so_what_s_the_catch_with__keyattr_">So what's the catch with 'keyattr'?</a></li>
<li><a href="#how_do_i_know_what_my_data_structure_should_look_like">How do I know what my data structure should look like?</a></li>
<li><a href="#i_m_getting__use_of_uninitialized_value__warnings">I'm getting 'Use of uninitialized value' warnings</a></li>
<li><a href="#i_m_getting_a__not_an_array_reference__error">I'm getting a 'Not an ARRAY reference' error</a></li>
<li><a href="#i_m_getting_a__no_such_array_field__error">I'm getting a 'No such array field' error</a></li>
<li><a href="#i_m_getting_an__out_of_memory__error">I'm getting an 'Out of memory' error</a></li>
<li><a href="#my_element_order_is_getting_jumbled_up">My element order is getting jumbled up</a></li>
<li><a href="#xml__simple_turns_nested_elements_into_attributes">XML::Simple turns nested elements into attributes</a></li>
<li><a href="#why_does_xmlout___insert__name__elements__or_attributes_">Why does <code>XMLout()</code> insert <name> elements (or attributes)?</a></li>
<li><a href="#why_are_empty_elements_represented_as_empty_hashes">Why are empty elements represented as empty hashes?</a></li>
<li><a href="#why_is_parseropts_deprecated">Why is ParserOpts deprecated?</a></li>
</ul>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="frequently_asked_questions_about_xml__simple">Frequently Asked Questions about XML::Simple</a></h1>
<p>
</p>
<hr />
<h1><a name="basics">Basics</a></h1>
<p>
</p>
<h2><a name="what_is_xml__simple_designed_to_be_used_for">What is XML::Simple designed to be used for?</a></h2>
<p>XML::Simple is a Perl module that was originally developed as a tool for
reading and writing configuration data in XML format. You can use it for
many other purposes that involve storing and retrieving structured data in
XML.</p>
<p>You might also find XML::Simple a good starting point for playing with XML
from Perl. It doesn't have a steep learning curve and if you outgrow its
capabilities there are plenty of other Perl/XML modules to 'step up' to.</p>
<p>
</p>
<h2><a name="why_store_configuration_data_in_xml_anyway">Why store configuration data in XML anyway?</a></h2>
<p>The many advantages of using XML format for configuration data include:</p>
<ul>
<li>
<p>Using existing XML parsing tools requires less development time, is easier
and more robust than developing your own config file parsing code</p>
</li>
<li>
<p>XML can represent relationships between pieces of data, such as nesting of
sections to arbitrary levels (not easily done with .INI files for example)</p>
</li>
<li>
<p>XML is basically just text, so you can easily edit a config file (easier than
editing a Win32 registry)</p>
</li>
<li>
<p>XML provides standard solutions for handling character sets and encoding
beyond basic ASCII (important for internationalization)</p>
</li>
<li>
<p>If it becomes necessary to change your configuration file format, there are
many tools available for performing transformations on XML files</p>
</li>
<li>
<p>XML is an open standard (the world does not need more proprietary binary
file formats)</p>
</li>
<li>
<p>Taking the extra step of developing a DTD allows the format of configuration
files to be validated before your program reads them (not directly supported
by XML::Simple)</p>
</li>
<li>
<p>Combining a DTD with a good XML editor can give you a GUI config editor for
minimal coding effort</p>
</li>
</ul>
<p>
</p>
<h2><a name="what_isn_t_xml__simple_good_for">What isn't XML::Simple good for?</a></h2>
<p>The main limitation of XML::Simple is that it does not work with 'mixed
content' (see the next question). If you consider your XML files contain
marked up text rather than structured data, you should probably use another
module.</p>
<p>If you are working with very large XML files, XML::Simple's approach of
representing the whole file in memory as a 'tree' data structure may not be
suitable.</p>
<p>
</p>
<h2><a name="what_is_mixed_content">What is mixed content?</a></h2>
<p>Consider this example XML:</p>
<pre>
<document>
<para>This is <em>mixed</em> content.</para>
</document></pre>
<p>This is said to be mixed content, because the <para> element contains
both character data (text content) and nested elements.</p>
<p>Here's some more XML:</p>
<pre>
<person>
<first_name>Joe</first_name>
<last_name>Bloggs</last_name>
<dob>25-April-1969</dob>
</person></pre>
<p>This second example is not generally considered to be mixed content. The
<first_name>, <last_name> and <dob> elements contain
only character data and the <person> element contains only nested
elements. (Note: Strictly speaking, the whitespace between the nested
elements is character data, but it is ignored by XML::Simple).</p>
<p>
</p>
<h2><a name="why_doesn_t_xml__simple_handle_mixed_content">Why doesn't XML::Simple handle mixed content?</a></h2>
<p>Because if it did, it would no longer be simple :-)</p>
<p>Seriously though, there are plenty of excellent modules that allow you to
work with mixed content in a variety of ways. Handling mixed content
correctly is not easy and by ignoring these issues, XML::Simple is able to
present an API without a steep learning curve.</p>
<p>
</p>
<h2><a name="which_perl_modules_do_handle_mixed_content">Which Perl modules do handle mixed content?</a></h2>
<p>Every one of them except XML::Simple :-)</p>
<p>If you're looking for a recommendation, I'd suggest you look at the Perl-XML
FAQ at:</p>
<pre>
<a href="http://perl-xml.sourceforge.net/faq/">http://perl-xml.sourceforge.net/faq/</a></pre>
<p>
</p>
<hr />
<h1><a name="installation">Installation</a></h1>
<p>
</p>
<h2><a name="how_do_i_install_xml__simple">How do I install XML::Simple?</a></h2>
<p>If you're running ActiveState Perl, you've probably already got XML::Simple
(although you may want to upgrade to version 1.09 or better for SAX support).</p>
<p>If you do need to install XML::Simple, you'll need to install an XML parser
module first. Install either XML::Parser (which you may have already) or
XML::SAX. If you install both, XML::SAX will be used by default.</p>
<p>Once you have a parser installed ...</p>
<p>On Unix systems, try:</p>
<pre>
perl -MCPAN -e 'install XML::Simple'</pre>
<p>If that doesn't work, download the latest distribution from
<a href="ftp://ftp.cpan.org/pub/CPAN/authors/id/G/GR/GRANTM">ftp://ftp.cpan.org/pub/CPAN/authors/id/G/GR/GRANTM</a> , unpack it and run these
commands:</p>
<pre>
perl Makefile.PL
make
make test
make install</pre>
<p>On Win32, if you have a recent build of ActiveState Perl (618 or better) try
this command:</p>
<pre>
ppm install XML::Simple</pre>
<p>If that doesn't work, you really only need the Simple.pm file, so extract it
from the .tar.gz file (eg: using WinZIP) and save it in the \site\lib\XML
directory under your Perl installation (typically C:\Perl).</p>
<p>
</p>
<h2><a name="i_m_trying_to_install_xml__simple_and__make_test__fails">I'm trying to install XML::Simple and 'make test' fails</a></h2>
<p>Is the directory where you've unpacked XML::Simple mounted from a file server
using NFS, SMB or some other network file sharing? If so, that may cause
errors in the the following test scripts:</p>
<pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -