⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faq.html

📁 perl教程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
      <span class="keyword">last</span><span class="operator">;</span>
    <span class="operator">}</span>
  <span class="operator">}</span>
</pre>
<p>The knowledge that each &lt;part&gt; element has a unique partnum attribute
allows you to eliminate this search.  You can pass this knowledge on to
XML::Simple like this:</p>
<pre>
  <span class="keyword">my</span> <span class="variable">$catalog</span> <span class="operator">=</span> <span class="variable">XMLin</span><span class="operator">(</span><span class="variable">$xml</span><span class="operator">,</span> <span class="string">keyattr</span> <span class="operator">=&gt;</span> <span class="operator">[</span><span class="string">'partnum'</span><span class="operator">]</span><span class="operator">);</span>
</pre>
<p>Which will return a data structure like this:</p>
<pre>
  <span class="variable">$catalog</span> <span class="operator">=</span> <span class="operator">{</span>
    <span class="string">'part'</span> <span class="operator">=&gt;</span> <span class="operator">{</span>
      <span class="string">'5634896'</span> <span class="operator">=&gt;</span> <span class="operator">{</span> <span class="string">'desc'</span> <span class="operator">=&gt;</span> <span class="string">'Low voltage washer'</span><span class="operator">,</span>   <span class="string">'price'</span> <span class="operator">=&gt;</span> <span class="string">'12.00'</span> <span class="operator">}</span><span class="operator">,</span>
      <span class="string">'1842334'</span> <span class="operator">=&gt;</span> <span class="operator">{</span> <span class="string">'desc'</span> <span class="operator">=&gt;</span> <span class="string">'High pressure flange'</span><span class="operator">,</span> <span class="string">'price'</span> <span class="operator">=&gt;</span> <span class="string">'24.50'</span> <span class="operator">}</span><span class="operator">,</span>
      <span class="string">'9344675'</span> <span class="operator">=&gt;</span> <span class="operator">{</span> <span class="string">'desc'</span> <span class="operator">=&gt;</span> <span class="string">'Threaded gasket'</span><span class="operator">,</span>      <span class="string">'price'</span> <span class="operator">=&gt;</span> <span class="string">'9.25'</span>  <span class="operator">}</span>
    <span class="operator">}</span>
  <span class="operator">}</span><span class="operator">;</span>
</pre>
<p>XML::Simple has been able to transform $catalog-&gt;{part} from an arrayref to
a hashref (keyed on partnum).  This transformation is called 'array folding'.</p>
<p>Through the use of array folding, you can now index directly to the
description of the part you want:</p>
<pre>
  <span class="keyword">print</span> <span class="variable">$catalog</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">part</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="number">9344675</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">desc</span><span class="operator">}</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>The 'keyattr' option also enables array folding when the unique key is in a
nested element rather than an attribute.  eg:</p>
<pre>
  &lt;catalog&gt;
    &lt;part&gt;
      &lt;partnum&gt;1842334&lt;/partnum&gt;
      &lt;desc&gt;High pressure flange&lt;/desc&gt;
      &lt;price&gt;24.50&lt;/price&gt;
    &lt;/part&gt;
    &lt;part&gt;
      &lt;partnum&gt;9344675&lt;/partnum&gt;
      &lt;desc&gt;Threaded gasket&lt;/desc&gt;
      &lt;price&gt;9.25&lt;/price&gt;
    &lt;/part&gt;
    &lt;part&gt;
      &lt;partnum&gt;5634896&lt;/partnum&gt;
      &lt;desc&gt;Low voltage washer&lt;/desc&gt;
      &lt;price&gt;12.00&lt;/price&gt;
    &lt;/part&gt;
  &lt;/catalog&gt;</pre>
<p>See the XML::Simple manual page for more information.</p>
<p>
</p>
<h2><a name="so_what_s_the_catch_with__keyattr_">So what's the catch with 'keyattr'?</a></h2>
<p>One thing to watch out for is that you might get array folding even if you
don't supply the keyattr option.  The default value for this option is:</p>
<pre>
  [ 'name', 'key', 'id']</pre>
<p>Which means if your XML elements have a 'name', 'key' or 'id' attribute (or
nested element) then they may get folded on those values.  This means that
you can take advantage of array folding simply through careful choice of
attribute names.  On the hand, if you really don't want array folding at all,
you'll need to set 'key attr to an empty list:</p>
<pre>
  <span class="keyword">my</span> <span class="variable">$ref</span> <span class="operator">=</span> <span class="variable">XMLin</span><span class="operator">(</span><span class="variable">$xml</span><span class="operator">,</span> <span class="string">keyattr</span> <span class="operator">=&gt;</span> <span class="operator">[]</span><span class="operator">);</span>
</pre>
<p>A second 'gotcha' is that array folding only works on arrays.  That might
seem obvious, but if there's only one record in your XML and you didn't set
the 'forcearray' option then it won't be represented as an array and
consequently won't get folded into a hash.  The moral is that if you're
using array folding, you should always turn on the forcearray option.</p>
<p>You probably want to be as specific as you can be too.  For instance, the
safest way to parse the &lt;catalog&gt; example above would be:</p>
<pre>
  <span class="keyword">my</span> <span class="variable">$catalog</span> <span class="operator">=</span> <span class="variable">XMLin</span><span class="operator">(</span><span class="variable">$xml</span><span class="operator">,</span> <span class="string">keyattr</span> <span class="operator">=&gt;</span> <span class="operator">{</span> <span class="string">part</span> <span class="operator">=&gt;</span> <span class="string">'partnum'</span><span class="operator">}</span><span class="operator">,</span>
                            <span class="string">forcearray</span> <span class="operator">=&gt;</span> <span class="operator">[</span><span class="string">'part'</span><span class="operator">]</span><span class="operator">);</span>
</pre>
<p>By using the hashref for keyattr, you can specify that only &lt;part&gt;
elements should be folded on the 'partnum' attribute (and that the
&lt;part&gt; elements should not be folded on any other attribute).</p>
<p>By supplying a list of element names for forcearray, you're ensuring that
folding will work even if there's only one &lt;part&gt;.  You're also
ensuring that if the 'partnum' unique key is supplied in a nested element
then that element won't get forced to an array too.</p>
<p>
</p>
<h2><a name="how_do_i_know_what_my_data_structure_should_look_like">How do I know what my data structure should look like?</a></h2>
<p>The rules are fairly straightforward:</p>
<ul>
<li>
<p>each element gets represented as a hash</p>
</li>
<li>
<p>unless it contains only text, in which case it'll be a simple scalar value</p>
</li>
<li>
<p>or unless there's more than one element with the same name, in which case
they'll be represented as an array</p>
</li>
<li>
<p>unless you've got array folding enabled, in which case they'll be folded into
a hash</p>
</li>
<li>
<p>empty elements (no text contents <strong>and</strong> no attributes) will either be
represented as an empty hash, an empty string or undef - depending on the value
of the 'suppressempty' option.</p>
</li>
</ul>
<p>If you're in any doubt, use Data::Dumper, eg:</p>
<pre>
  <span class="keyword">use</span> <span class="variable">XML::Simple</span><span class="operator">;</span>
  <span class="keyword">use</span> <span class="variable">Data::Dumper</span><span class="operator">;</span>
  
  <span class="keyword">my</span> <span class="variable">$ref</span> <span class="operator">=</span> <span class="variable">XMLin</span><span class="operator">(</span><span class="variable">$xml</span><span class="operator">);</span>
</pre>
<pre>
  <span class="keyword">print</span> <span class="variable">Dumper</span><span class="operator">(</span><span class="variable">$ref</span><span class="operator">);</span>
</pre>
<p>
</p>
<h2><a name="i_m_getting__use_of_uninitialized_value__warnings">I'm getting 'Use of uninitialized value' warnings</a></h2>
<p>You're probably trying to index into a non-existant hash key - try
Data::Dumper.</p>
<p>
</p>
<h2><a name="i_m_getting_a__not_an_array_reference__error">I'm getting a 'Not an ARRAY reference' error</a></h2>
<p>Something that you expect to be an array is not.  The two most likely causes
are that you forgot to use 'forcearray' or that the array got folded into a
hash - try Data::Dumper.</p>
<p>
</p>
<h2><a name="i_m_getting_a__no_such_array_field__error">I'm getting a 'No such array field' error</a></h2>
<p>Something that you expect to be a hash is actually an array.  Perhaps array
folding failed because one element was missing the key attribute - try
Data::Dumper.</p>
<p>
</p>
<h2><a name="i_m_getting_an__out_of_memory__error">I'm getting an 'Out of memory' error</a></h2>
<p>Something in the data structure is not as you expect and Perl may be trying
unsuccessfully to autovivify things - try Data::Dumper.</p>
<p>If you're already using Data::Dumper, try calling <code>Dumper()</code> immediately after
<code>XMLin()</code> - ie: before you attempt to access anything in the data structure.</p>
<p>
</p>
<h2><a name="my_element_order_is_getting_jumbled_up">My element order is getting jumbled up</a></h2>
<p>If you read an XML file with <code>XMLin()</code> and then write it back out with
XMLout(), the order of the elements will likely be different.  (However, if
you read the file back in with <code>XMLin()</code> you'll get the same Perl data
structure).</p>
<p>The reordering happens because XML::Simple uses hashrefs to store your data
and Perl hashes do not really have any order.</p>
<p>It is possible that a future version of XML::Simple will use Tie::IxHash
to store the data in hashrefs which do retain the order.  However this will
not fix all cases of element order being lost.</p>
<p>If your application really is sensitive to element order, don't use
XML::Simple (and don't put order-sensitive values in attributes).</p>
<p>
</p>
<h2><a name="xml__simple_turns_nested_elements_into_attributes">XML::Simple turns nested elements into attributes</a></h2>
<p>If you read an XML file with <code>XMLin()</code> and then write it back out with
XMLout(), some data which was originally stored in nested elements may end up
in attributes.  (However, if you read the file back in with <code>XMLin()</code> you'll
get the same Perl data structure).</p>
<p>There are a number of ways you might handle this:</p>
<ul>
<li>
<p>use the 'forcearray' option with <code>XMLin()</code></p>
</li>
<li>
<p>use the 'noattr' option with <code>XMLout()</code></p>
</li>
<li>
<p>live with it</p>
</li>
<li>
<p>don't use XML::Simple</p>
</li>
</ul>
<p>
</p>
<h2><a name="why_does_xmlout___insert__name__elements__or_attributes_">Why does <code>XMLout()</code> insert &lt;name&gt; elements (or attributes)?</a></h2>
<p>Try setting keyattr =&gt; [].</p>
<p>When you call <code>XMLin()</code> to read XML, the 'keyattr' option controls whether arrays
get 'folded' into hashes.  Similarly, when you call XMLout(), the 'keyattr'
option controls whether hashes get 'unfolded' into arrays.  As described above,
'keyattr' is enabled by default.</p>
<p>
</p>
<h2><a name="why_are_empty_elements_represented_as_empty_hashes">Why are empty elements represented as empty hashes?</a></h2>
<p>An element is always represented as a hash unless it contains only text, in
which case it is represented as a scalar string.</p>
<p>If you would prefer empty elements to be represented as empty strings or the
undefined value, set the 'suppressempty' option to '' or undef respectively.</p>
<p>
</p>
<h2><a name="why_is_parseropts_deprecated">Why is ParserOpts deprecated?</a></h2>
<p>The <code>ParserOpts</code> option is a remnant of the time when XML::Simple only worked
with the XML::Parser API.  Its value is completely ignored if you're using a
SAX parser, so writing code which relied on it would bar you from taking
advantage of SAX.</p>
<p>Even if you are using XML::Parser, it is seldom necessary to pass options to
the parser object.  A number of people have written to say they use this option
to set XML::Parser's <code>ProtocolEncoding</code> option.  Don't do that, it's wrong,
Wrong, WRONG!  Fix the XML document so that it's well-formed and you won't have
a problem.</p>
<p>Having said all of that, as long as XML::Simple continues to support the
XML::Parser API, this option will not be removed.  There are currently no plans
to remove support for the XML::Parser API.</p>

</body>

</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -