1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package XML::Serializer;
use strict;
my $root_element = "root";
my $indent = " ";
#my $XML_HEADER = << "XMLHEADER";
#<?xml version="1.0" encoding="__CHARSET__"?>
#<?xml-stylesheet type="text/xsl" href="__XSLT__" ?>
#XMLHEADER
my $XML_HEADER = << "XMLHEADER";
<?xml version="1.0" encoding="__CHARSET__"?>
XMLHEADER
sub WITH_HEADER {
return 1;
}
sub new {
my ($this, $data, $root_name, $xslt) = @_;
my %fields = (
_charset => "utf-8",
_data => "",
_output => "",
_root_name => $root_name ? $root_name : "root",
_xslt => $xslt ? $xslt : ""
);
if (defined $data) {
$fields{_data} = $data;
}
return bless \%fields, $this;
}
sub set_root_name {
my ($this, $root_name) = @_;
$this->{_root_name} = $root_name;
}
sub set_data {
my ($this, $data) = @_;
$this->{_data} = $data;
}
sub set_charset {
my ($this, $charset) = @_;
$this->{_charset} = $charset;
}
sub set_xslt {
my ($this, $xslt) = @_;
$this->{_xslt} = $xslt;
}
sub to_string{
my ($this, $header) = @_;
if ($header) {
$this->{_output} = &_make_xml_header($this->{_charset}, $this->{_xslt});
}
$this->{_output} .= &_to_string($this->{_data}, $this->{_root_name});
}
sub to_formatted{
my ($this, $header) = @_;
if ($header) {
$this->{_output} = &_make_xml_header($this->{_charset}, $this->{_xslt});
}
$this->{_output} .= &_to_formatted($this->{_root_name}, $this->{_data});
}
sub _make_xml_header {
my $header = $XML_HEADER;
$header =~ s/__CHARSET__/$_[0]/;
$header =~ s/__XSLT__/$_[1]/;
return $header;
}
sub _to_string {
my ($obj, $name) = @_;
my $output = "";
if (ref($obj) eq "HASH") {
my $attr_list = "";
my $tmp_mid = "";
foreach (sort keys %$obj) {
if ($_ =~ /^@/) {
$attr_list = &_to_string($_, $obj->{$_});
}
$tmp_mid .= &_to_string($_, $obj->{$_});
}
$output = &_start_node($name, $attr_list) . $tmp_mid . &_end_node($name);
}
elsif (ref($obj) eq "ARRAY") {
foreach (@$obj) {
$output .= &_to_string($_, $name);
}
}
else {
if ($_ =~ /^@(.+)$/) {
return "$1=\"$obj\" ";
} else {
$output = &_start_node($name) . $obj . &_end_node($name);
}
}
return $output;
}
sub _to_formatted {
my ($name, $obj, $depth) = @_;
# if (!$obj) { $obj = ""; }
if (!defined($depth)) { $depth = 0; }
my $output = "";
if (ref($obj) eq "HASH") {
my $attr_list = "";
my $tmp_mid = "";
foreach (sort keys %$obj) {
if ($_ =~ /^@/) {
$attr_list = &_to_string($_, $obj->{$_});
}
$tmp_mid .= &_to_formatted($_, $obj->{$_}, $depth+1);
}
$output = &_start_node($name, $attr_list, $depth) . "\n" . $tmp_mid . &_end_node($name, $depth);
}
elsif (ref($obj) eq "ARRAY") {
foreach (@$obj) {
$output .= &_to_formatted($name, $_, $depth);
}
}
else {
if ($_ =~ /^@(.+)$/) {
#return "$1=\"$obj\" ";
} else {
$output .= &_start_node($name, "", $depth);
$output .= $obj;
$output .= &_end_node($name);
}
}
return $output;
}
sub _start_node {
my $ret = "";
if (defined $_[2]) {
for(1..$_[2]) { $ret .= $indent; }
}
my $tag = $_[0] ? $_[0] : "";
my $attr = $_[1] ? $_[1] : "";
$ret .= "<$tag $attr>";
return $ret;
}
sub _end_node {
my $ret = "";
if (defined $_[1]) {
for(1..$_[1]) { $ret .= $indent; }
}
if (defined $_[0]) {
$ret .= "</$_[0]>\n";
}
return $ret;
}
1;
|