Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: XMLout and keys/attributes

by Anonymous Monk
on May 09, 2012 at 14:48 UTC ( #969641=note: print w/replies, xml ) Need Help??

in reply to XMLout and keys/attributes

What are you using for XMLin?

Replies are listed 'Best First'.
Re^2: XMLout and keys/attributes
by atreyu (Sexton) on May 09, 2012 at 14:56 UTC
    I am not using XMLin for anything. What I am trying to do (generate an XML file from a hash/hashref of my own making) has been done elsewhere with no problems. It is just that I am being picky about what is an attribute and what is not that is tripping me up.
      I forgot to mention an important point: in the $userref hashref, there will be many values for 'name', and they could start with a number, which is why I want to use them as attributes, and not as keys. example:
      $userref->{2}{'hash'}{'name'} = '12345'; $userref->{3}{'hash'}{'name'} = '10foo';

      I am not using XMLin for anything.

      Then I would ditch XMLout ASAP :) seriously

      The way I use XML::Simple, is start with XML I want, then use XMLin to get data I want, then XMLout to get back xml I started with

      If XMLin can't be made into data structure you want, then XMLout can't be made into xml you want

      All that massaging of $userref you do is busywork :)

      So with XML::Rules ( XML::Simple on steriods :) steroids) I might write

      #!/usr/bin/perl -- use strict; use warnings; use XML::Rules; use Data::Dump qw/ dd /; my $wanted = <<'__XML__'; <?xml version="1.0" encoding="utf-8"?> <opt> <key0> <devices> <dev0> <param id="name">joe</param> <param id="type">user</param> </dev0> </devices> </key0> <key1> <devices> <dev0> <param id="name">mary</param> <param id="type">user</param> </dev0> </devices> </key1> </opt> __XML__ use XML::Rules; my $t = XML::Rules->new( qw/ stripspaces 8 /, rules => { 'dev0,devices,key0,key1,opt' => 'no content', 'param' => 'as array', }, ); my $XmlRef = $t->parse( $wanted ); dd $XmlRef; print $t->toXML( opt => $XmlRef->{opt}, undef, " ", ); __END__ { opt => { key0 => { devices => { dev0 => { param => [ { _content => "joe", id => "name" }, { _content => "user", id => "type" }, ], }, }, }, key1 => { devices => { dev0 => { param => [ { _content => "mary", id => "name" }, { _content => "user", id => "type" }, ], }, }, }, }, } <opt> <key0> <devices> <dev0> <param id="name">joe</param> <param id="type">user</param> </dev0> </devices> </key0> <key1> <devices> <dev0> <param id="name">mary</param> <param id="type">user</param> </dev0> </devices> </key1> </opt>

      You still get to have a hairy hash, you still get your xml, and you don't spend days trying to make it "Simple"

        Okay, that is very interesting, and it does give you more control, granted. but i want a more on-the-fly approach, which sauoq's solution grants me. i'm not saying you can't do that with XML::Rules of course (i'm sure you can), but his simple edit to my hashref manipulation was easy and ideal and I got to keep XML::Simple and its simple ways. thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://969641]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2018-06-18 19:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.