I am using the Perl Apache2::REST
and the standard way of returning data is to issue $resp->data()
and assign a value. I have something like this
my $text = {
'tag1' => 4,
'tag2' => 5,
'tag3' => 6,
};
$resp->data()->{'text'} = {map { $_ => [$text ->{$_}] } keys %$text};
which gives me a response like this
<response message="" status="200">
<data>
<tag1>4</tag1>
<tag2>5</tag2>
<tag3>6</tag3>
</data>
</response>
I would like to get to know how i create a response with an attribute in XML node tag1
and can create tags of the same type on the same level?
Desired output is
<response message="" status="200">
<data>
<tag1 id="abcd"> 4 </tag1>
<tag1>
<tag3 id="xyz"> 6 </tag3>
</tag1>
</data>
</response>
I think this will work, but it will produce a bit different output
my $text2->{tag1} = [4,{tag3 => 6}];
$resp->data()->{'text'} = $text2;
regards,
EDIT:
my $text2->{tag1} = [4,['val',{tag3 => 6}]];
The module uses XML::Simple with no options but RootName
. Knowing that, we know the following data structure will produce the output you want.
my $data = {
'tag1' => [
{
id => 'abcd',
content => '4',
},
{
'tag3' => [
{
id => 'xyz',
content => '6',
},
],
},
],
};
Test:
use XML::Simple qw( XMLout );
print XMLout($data , RootName => 'data');
Output:
<data>
<tag1 id="abcd">4</tag1>
<tag1>
<tag3 id="xyz">6</tag3>
</tag1>
</data>
(It will provide the response
element.)
The solution to this is to use the perl module XML::Simple
with ForceArray => 1
$xml = '<tag1 a="' . '4' . '"b="4">';
$xml .= '<tag3 id="' . '3' . '">';
$xml .= '<tag2>' . '5' . '</tag2>';
$xml .= '</tag3>';
$xml .= '</tag1>';
my $tree = $simple->XMLin($xml, ForceArray => 1, KeyAttr => [ ]);
$resp->data()->{'xml'} = $tree;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.