I have a collection of XML files that have one or more &lt;result&gt; elements in each file.  The goal is have a script where I can pass one or more files on the command line which will gather up all the &lt;result&gt; elements from each file and combine into a single XML output file.<div>


<br></div><div>I pulled XML::TreeBuilder out (I find TreeBuilder pretty easy for quick scripts) and did the following.  I seem to not work with XML that much (which I think is a bit lucky), so there may be easier ways to do this.</div>


<div><br></div><div><div><font face="&#39;courier new&#39;, monospace">#!/usr/bin/perl</font></div><div><font face="&#39;courier new&#39;, monospace">use strict;</font></div>
<div><font face="&#39;courier new&#39;, monospace">use warnings;</font></div><div><font face="&#39;courier new&#39;, monospace">use XML::TreeBuilder;</font></div><div><font face="&#39;courier new&#39;, monospace">use XML::Element;</font></div>


<div><font face="&#39;courier new&#39;, monospace">use Encode;</font></div><div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font face="&#39;courier new&#39;, monospace">my $doc = XML::Element-&gt;new( &#39;testResults&#39; );</font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font face="&#39;courier new&#39;, monospace">for my $path ( @ARGV ) {</font></div><div><font face="&#39;courier new&#39;, monospace">    my $tree = XML::TreeBuilder-&gt;new;</font></div>
<div><font face="&#39;courier new&#39;, monospace">    $tree-&gt;parse_file( $path );</font></div><div><font face="&#39;courier new&#39;, monospace"><br></font></div><div>
<font face="&#39;courier new&#39;, monospace">    $doc-&gt;push_content( $tree-&gt;look_down( &#39;_tag&#39;, &#39;result&#39; ) );</font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font face="&#39;courier new&#39;, monospace">    $tree-&gt;delete;</font></div><div><font face="&#39;courier new&#39;, monospace">}</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>

</font></div><div><font face="&#39;courier new&#39;, monospace">print join &quot;\n&quot;, </font></div>
<div><font face="&#39;courier new&#39;, monospace">    &#39;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#39;,</font></div><div><font face="&#39;courier new&#39;, monospace">    encode_utf8( $doc-&gt;as_XML );</font></div>


<div><br></div><div><br></div><div>That seems to work ok.  But, then I ended up with a file that had a CDATA section (which happened to hold a snippet of HTML).  That&#39;s fine, but $doc-&gt;as_XML then encoded the entities.</div>


<div><br></div><div>With this source file:</div><div><br></div><div><div><font face="&#39;courier new&#39;, monospace">$ cat test.xml</font></div><div><font face="&#39;courier new&#39;, monospace">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</font></div>


<div><font face="&#39;courier new&#39;, monospace">&lt;testResults&gt;</font></div><div><font face="&#39;courier new&#39;, monospace">    &lt;result&gt;</font></div><div><font face="&#39;courier new&#39;, monospace">        &lt;content&gt;&lt;![CDATA[&lt;strong&gt;this is&amp;nbsp;strong&lt;/strong&gt;]]&gt;&lt;/content&gt;</font></div>


<div><font face="&#39;courier new&#39;, monospace">    &lt;/result&gt;</font></div><div><font face="&#39;courier new&#39;, monospace">&lt;/testResults&gt;</font></div></div>
<div><br></div><div>I run through the script I get:</div><div><br></div><div><div><font face="&#39;courier new&#39;, monospace">$ cat new.xml</font></div><div><font face="&#39;courier new&#39;, monospace">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</font></div>


<div><font face="&#39;courier new&#39;, monospace">&lt;testResults&gt;&lt;result&gt;</font></div><div><font face="&#39;courier new&#39;, monospace">        &lt;content&gt;&amp;#60;strong&amp;#62;this is&amp;nbsp;strong&amp;#60;/strong&amp;#62;&lt;/content&gt;</font></div>


<div><font face="&#39;courier new&#39;, monospace">    &lt;/result&gt;&lt;/testResults&gt;</font></div></div><div><br></div><div>And if I then run *that* file back through the script I get:</div><div><br></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">

<div><div><div>undefined entity at line 3, column 40, byte 101 at /usr/lib/perl5/XML/Parser.pm line 187</div>
</div></div></blockquote><div><div><div><br></div><div>which is choking at the &amp;nbsp;</div><div><br></div><div><br></div><div>My questions are:</div><div><br></div><div>1) Is there a better approach to doing this that preserves the CDATA sections?</div>


<div><br></div><div>2) Is there a way to define the &amp;nbsp; entity?  I tried adding DTD to defined the &amp;nbsp;, but wasn&#39;t able to make the parser happy in my attempts.</div><div><br></div><div><br></div><div><br>

-- <br>Bill Moseley<br>
<a href="mailto:moseley@hank.org" target="_blank">moseley@hank.org</a><br>
</div></div></div>