You are correct, I did run into problems you are describing here. However, I found temporary remedy by instantiating an XML::Simple object with certain options that simplify the resulting perl structure somewhat. Here's an example code snippet:
use strict;
use XML::Simple;
use Data::Dumper;
my $xs = new XML::Simple(forcearray => 1,
forcecontent => 1,
contentkey => '_content',
keyattr => []);
my $xml;
{
local $/ = undef;
$xml = <DATA>;
}
my $xmlHash = $xs->XMLin($xml);
print "xmlHash:\n" . Dumper($xmlHash);
__DATA__
<?xml version="1.0" encoding="iso-8859-1"?>
<hosts>
<server os="linux" type="redhat" version="8.0">
<address>192.168.0.1</address>
<address>192.168.0.2</address>
</server>
<server address="192.168.2.100" os="linux" type="conectiva" version=
+"9.0"/>
</hosts>
And the ouptut is
xmlHash:
$VAR1 = {
'server' => [
{
'os' => 'linux',
'address' => [
{
'_content' => '192.168.0.1'
},
{
'_content' => '192.168.0.2'
}
],
'version' => '8.0',
'type' => 'redhat'
},
{
'os' => 'linux',
'address' => '192.168.2.100',
'version' => '9.0',
'type' => 'conectiva'
}
]
};
Note that I force the '_content' key which assures that there'll be much less inconsistancy in the xml structure (HASHREF or ARRAYREF etc).
I was also wondering if you had done any benchmarking on your module? I have a sizable script (~4000 lines) that makes extensive use of the XML::Simple module and was wondering if converting to, say, your smart module would slow it down considerably (and this being part of a larger web application may be the least I'd want to have :)?
Otherwise, your attempt does look worthwhile and promising :-)
_____________________
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true."
Robert Wilensky, University of California
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.