Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://969641]
help
Chatterbox?
[Corion]: "zero monetary cost", because of course, it's gratis only if your time has no value :)
[marto]: I used to keep up to date with my android ROMs, updating to the nightly every other day
[marto]: in my experience the jump in major releases has been painless, and performed better
[marto]: Corion if your device has an official Oreo ROM I'd do a nandroid backup and give it a shot

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-12-14 10:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (389 votes). Check out past polls.

    Notices?