Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I've got a simple xml document ...
<?xml version="1.0" encoding="UTF-8" ?> <charge-type-control> <charge code="FRT" name="frtamt"/> <charge code="SV1" name="savamt"/> <charge code="SV2" name="savamt"/> <charge code="SV3" name="savamt"/> <charge code="SV4" name="savamt"/> <charge code="SV5" name="savamt"/> </charge-type-control>
that I would like to parse into the data structure ...
{ 'SV1' => 'savamt', 'SV5' => 'savamt', 'SV2' => 'savamt', 'FRT' => 'frtamt', 'SV3' => 'savamt', 'SV4' => 'savamt' };
However, the closest I've been able to get with XML::Simple is ...
{ 'charge' => { 'SV1' => { 'name' => 'savamt' }, 'SV5' => { 'name' => 'savamt' }, 'SV2' => { 'name' => 'savamt' }, 'SV4' => { 'name' => 'savamt' }, 'SV3' => { 'name' => 'savamt' }, 'FRT' => { 'name' => 'frtamt' } } };
using this XMLin line ...
$ref = XMLin('charge-type-control.xml', KeyAttr => ['code']);
the docs allude to using ValueAttr like...
$ref = XMLin('charge-type-control.xml', KeyAttr => ['code'], ValueAttr + => ['name']);

but the results are the same. So either it doesn't do what I think it does or what I want to do is impossible. Can I coerce XML::Simple to do exactly what I want or do I have to write my own transformer like...

my %myref; for my $key (keys %{$ref->{charge}}) { $myref{$key} = $ref->{charge}->{$key}->{name}; }
to get exactly what I want?

In reply to XML::Simple help by mifflin

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2023-06-04 11:49 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (21 votes). Check out past polls.