Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

To answer your question: it seems short-sighted to me. But not disallowed by the XML spec. XML explicitly allows ordering to matter (think of an XHTML doc - very important that the ndoes are in the right order), but for data transfer between programs, I'd not use it unless there are duplicate elements (e.g., more than one "model" tag).

As to solving it rather than griping about it, without a huge amount of thought applied here, the way I would likely approach this is to have an XML template file, say:

<newPhone> <name></name> <description></description> <product></product> <model></model> </newPhone>
Then I would have XML::Twig load it, and then insert the data. I am not sure that there really is a generic solution here, so here's my offering using my favourite XML editor package ;-)
use strict; use XML::Twig; my $hash = { name => 'bob', product => 'blah', model => 'blah model', description => 'too much money? gimme!', }; my $twig = XML::Twig->new(pretty_print => 'indented'); ## mirod pointed out that I overlooked that parse can ## indeed handle file handles. Woops. :-) $twig->parse(\*DATA); my $elt = create_elements($twig->root(), $hash); $twig->print(); sub create_elements { my $parent = shift; my $data = shift; while (my ($k,$v) = each(%$data)) { # get_xpath returns a list. We should only every find 1 # node, so extract that. my @els = $parent->get_xpath($k); my $el = $els[0]; if ($el) { $el->set_text($v); } else { $el = XML::Twig::Elt($k => $v); $el->paste(last_child => $parent); } } } __END__ <newPhone> <name></name> <description></description> <product></product> <model></model> </newPhone>

Update: Simplified my parse call, thanks mirod!


In reply to Re^2: Outputting a hash as XML by Tanktalus
in thread Outputting a hash as XML by smeenz

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

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found