-
Notifications
You must be signed in to change notification settings - Fork 0
/
SugarXML.pm
188 lines (148 loc) · 4.78 KB
/
SugarXML.pm
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# SugarXML.pm
# Syntactic sugar for LibXML.
#
# Copyright 2011 C John Klehm
# Licensed under the AGPL version 3
#
package SugarXML;
use strict;
use warnings;
use autodie;
our $VERSION = '1.00';
use base 'Exporter';
our @EXPORT = qw(createDoc createDocCDATA addNode addNodeCDATA prependNode prependNodeCDATA);
use XML::LibXML;
##
# Creates the xml document and root element
#
# $encoding The character encoding of the xml document.
# $version The xml version of the document.
# $rootName The name of the root element.
# $rootData The data to place in the root element as #text.
#
# Usage: my ($doc, $root) = createDoc('1.0', 'UTF-8', 'RootName', 'RootData',
# attribName1 => '2010/10',
# atrrib2 => 12
# );
# You can also add the root data later with this:
# $root->appendText('RootData');
#
sub createDoc {
my ($version, $encoding, $rootName, $rootData, %attributes) = @_;
my $doc = XML::LibXML::Document->new($version, $encoding);
my $root = $doc->createElement($rootName);
$root->appendText($rootData);
$doc->setDocumentElement($root);
# add atributes
while (my ($key, $keyValue) = each(%attributes)) {
$root->setAttribute($key, $keyValue);
}
return ($doc, $root);
}
##
# Creates the xml document and root element
# Use the cdata version when you want to embed html in your xml
#
# $encoding The character encoding of the xml document.
# $version The xml version of the document.
# $rootName The name of the root element.
# $rootData The data to place in the root element as cdata.
#
# Usage: my ($doc, $root) = createDoc('1.0', 'UTF-8', 'RootName', 'RootData',
# attribName1 => '2010/10',
# atrrib2 => 12
# );
# You can also add the root data later with this:
# $root->appendText('RootData');
#
sub createDocCDATA {
my ($version, $encoding, $rootName, $rootData, %attributes) = @_;
my $doc = XML::LibXML::Document->new($version, $encoding);
my $root = $doc->createElement($rootName);
my $cdata = $doc->createCDATASection($rootData);
$root->appendChild($cdata);
$doc->setDocumentElement($root);
# add atributes
while (my ($key, $keyValue) = each(%attributes)) {
$root->setAttribute($key, $keyValue);
}
return ($doc, $root);
}
##
# Adds a node to a parent element.
#
# $doc The owning document.
# $parent The parent element of this node.
# $nodeName The name of this node.
# $nodeData The data to place in this node as text.
# %attributes The attributes of this node.
# return The created node.
#
# Usage: addNode($doc, $parent, 'NodeName', 'This is a node.', attribName1 => '2010/10', atrrib2 => 12);
#
sub addNode {
my ($doc, $parent, $nodeName, $nodeData, %attributes) = @_;
my $el = $doc->createElement($nodeName);
$el->appendText($nodeData);
# add attributes
while (my ($key, $keyValue) = each(%attributes)) {
$el->setAttribute($key, $keyValue);
}
$parent->appendChild($el);
return $el;
}
##
# Adds a node to a parent element.
# Use the cdata version when you want to embed html in xml.
#
# $doc The owning document.
# $parent The parent element of this node.
# $nodeName The name of this node.
# $nodeData The data to place in this node as CDATA.
# %attributes The attributes of this node.
# return The created node.
#
# Usage: addNode($doc, $parent, 'NodeName', 'This is a node.', attribName1 => '2010/10', atrrib2 => 12);
#
sub addNodeCDATA {
my ($doc, $parent, $nodeName, $nodeData, %attributes) = @_;
my $cdata = $doc->createCDATASection($nodeData);
my $el = $doc->createElement($nodeName);
$el->appendChild($cdata);
# add attributes
while (my ($key, $keyValue) = each(%attributes)) {
$el->setAttribute($key, $keyValue);
}
$parent->appendChild($el);
return $el;
}
##
# Same style as the others but insetad of appending to the parent it prepends.
#
sub prependNode {
my ($doc, $parent, $nodeName, $nodeData, %attributes) = @_;
my $el = $doc->createElement($nodeName);
$el->appendText($nodeData);
# add attributes
while (my ($key, $keyValue) = each(%attributes)) {
$el->setAttribute($key, $keyValue);
}
$parent->insertBefore($el, $parent->firstChild());
return $el;
}
##
# Same style as the others but instead of appending to the parent it prepends.
#
sub prependNodeCDATA {
my ($doc, $parent, $nodeName, $nodeData, %attributes) = @_;
my $cdata = $doc->createCDATASection($nodeData);
my $el = $doc->createElement($nodeName);
$el->appendChild($cdata);
# add attributes
while (my ($key, $keyValue) = each(%attributes)) {
$el->setAttribute($key, $keyValue);
}
$parent->insertBefore($el, $parent->firstChild());
return $el;
}
1;