$ data = {id = & quot; Gt; 1, name = & gt; "A", user = & gt; [{Id = & gt; 1, name = & gt; "U1"}, {id = & gt; 2, name = & gt; "U2"}], group = & gt; [{Id = & gt; 1, name = & gt; "G1"}]}; I would like to convert it to an XML below:
& lt; Map & gt; & Lt; Item ID = "1" name = "A" & gt; & Lt; Users & gt; & Lt; User ID = "1" name = "u1" /> & Lt; User ID = "2" name = "u2" /> & Lt; / Users & gt; & Lt; Groups & gt; & Lt; Group id = "1" name = "g1" /> & Lt; / Groups & gt; & Lt; / Item & gt; & Lt; / Map & gt;
I could manually make each line manually but I am looking for any CPAN module based solution.
I tried XML :: stroll but did not go anywhere. I have used XML :: in the past for simplicity, but this time XML is trying to do something as simple: it is getting bad reviews.
Yes, intelligent choice ... is not it for simple XML
As mentioned in the comments - your data is a bit unclear - in particular, how can you tell what the elements within 'groups' or 'users' should be told?
It looks like you can parse some JSON (in fact, you can change it back in JSON directly:
print to_json ($ data, {beautiful = & Gt; 1});
The core problem - where JSON supports the arrays, XML does not. So actually there is very little you can do that Directly will convert your data structure to XML.
However, if you are doing some work:
In this way you use some XML cass Use
Strict; Use cautionary; XML :: Use the tweak; My $ twig = XML :: Twig-> New ( 'Pretty_print' = & gt; 'indent'); $ twig- & gt; set_root (XML :: Tweg :: Alt-> New ('map',)); $ $ = $ Item - & gt; My $ item = $ Twig- & gt; Root- & gt; insert_new_elt ('item', {'id' = & gt; 1, 'name' = & gt; 'A'}); $ Users - & gt; insert_new_elt (' User '& gt; insert_new_elt (' user ', {' id '= & gt; 1,' name '=>' u1 '}); {' id '=> 2, Name '= & gt; 'U2'}); My $ groups = $ item - & gt; Insert_new_elt ('last_child', 'groups'); $ Groups - & g; Insert_new_elt ('group', {'id' => 1, 'name' = & gt; 'g1'}); $ Twig- & gt; Set_xml_version ("1.0"); $ Twig- & gt; Set_encoding ('utf-8'); $ Twig- & gt; Print;
What prints:
& Lt; User ID = "1" name = "u1" /> & Lt; / Users & gt; & Lt; Groups & gt; & Lt; Group id = "1" name = "g1" /> & Lt; / Groups & gt; & Lt; / Item & gt; & Lt; / Map & gt;
To change your data structure is left as an exercise for the reader.
As Borodin notes correctly - you have map
from your data structure to items
group
or Users can guess Maybe based on plurals, but looking at your data set, the best I can come up with is something like this: < Pre> use strict; Use warnings; XML :: Jogging; My $ data = {id = & gt; 1, name = & gt; "A", user = & gt; [{Id = & gt; 1, name = & gt; "U1"}, {id = & gt; 2, name = & gt; "U2"}], group = & gt; [{Id = & gt; 1, name = & gt; "G1"}]}; My $ twig = XML :: Twig- & gt; New ('pretty_print' = & gt; 'indented'); $ Toggle- & gt; Set_rote (XML :: tweg :: ALT-> new ('map',)); My $ item = $ twig- & gt; Root- & gt; Insert_new_elt ('item'); Forward my $ key (key $% data) {if ($ ref $ data- & gt; {$ key}) {$ item- & gt; Set_at ($ key, $ data-> {$ key}); the upcoming; } If (ref ($ data-> gt; {key $}) "ARRAY") {my $ fakearray = $ item- & gt; Insert_new_elt ($ key); Prefer my $ element (@ {$ data- & gt; {$ key}}) {my $ name = $ key; $ Name = ~ s / s $ / g; $ Fakearray- & gt; Insert_new_elt ($ name, $ element); } the upcoming; } If (reference ($ data -> gt; {$ key}) eq "HASH") {$ item - & gt; Insert_new_elt ($ key, $ data - & gt; {$ key}); the upcoming; }} $ Twig- & gt; Set_xml_version ("1.0"); $ Twig- & gt; Set_encoding ('utf-8'); $ Twig- & gt; Print;
This is not ideal because - map
is hard, as is the object
and I take a very simplistic approach to accepting the array , Finally a s
, to make it plural