in reply to Outputting a hash as XML

Oh dear.. I've run into a new problem now. It seems that the XML parser at the other end is quite picky about the order the tags appear in. In other words, in the following two examples, only the second is valid (it expects to see name before description)

<newPhone> <description>Test AXL addPhone</description> <name>SEP0028A9B0B21</name> <product>Cisco 7970</product> <model>Cisco 7970</model> </newPhone> <newPhone> <name>SEP0028A9B0B21</name> <description>Test AXL addPhone</description> <product>Cisco 7970</product> <model>Cisco 7970</model> </newPhone>

Is this expected ? It seems to be 'the wrong thing' to me.

Replies are listed 'Best First'.
Re^2: Outputting a hash as XML
by Tanktalus (Canon) on Mar 16, 2005 at 22:19 UTC

    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!

Re^2: Outputting a hash as XML
by grantm (Parson) on Mar 16, 2005 at 20:32 UTC

    Yes, the order of elements is significant in XML. Some applications are tolerant of different element ordering, but others are picky. It seems your application is picky, so XML::Simple is definitely not the right tool for the job.

      right.. so.. I either kludge this by storing a static list so I know what order to output the tags in, or I read parse the DTD every time to be completely correct, but with the drawback of the additional overhead that would create.

      I think I'll just go for a static list indicating the order the tags need to appear.