| 1 |
NAME |
| 2 |
XML::Hash::XS - Simple and fast hash to XML and XML to hash conversion |
| 3 |
written in C |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
use XML::Hash::XS; |
| 7 |
|
| 8 |
my $xmlstr = hash2xml \%hash; |
| 9 |
hash2xml \%hash, output => $fh; |
| 10 |
hash2xml \%hash, output_cb => sub { my ($chunk) = @_; print $chunk }; |
| 11 |
|
| 12 |
my $hash = xml2hash $xmlstr; |
| 13 |
my $hash = xml2hash \$xmlstr; |
| 14 |
my $hash = xml2hash 'test.xml', encoding => 'cp1251'; |
| 15 |
my $hash = xml2hash $fh; |
| 16 |
my $hash = xml2hash *STDIN; |
| 17 |
|
| 18 |
Or OOP way: |
| 19 |
|
| 20 |
use XML::Hash::XS qw(); |
| 21 |
|
| 22 |
my $conv = XML::Hash::XS->new(utf8 => 0, encoding => 'utf-8') |
| 23 |
my $xmlstr = $conv->hash2xml(\%hash, utf8 => 1); |
| 24 |
my $hash = $conv->xml2hash($xmlstr, encoding => 'cp1251'); |
| 25 |
|
| 26 |
For incremental XML input, create a parser: |
| 27 |
|
| 28 |
my $parser = XML::Hash::XS::Parser->new(keep_root => 1); |
| 29 |
$parser->feed('<root><item>one</item>'); |
| 30 |
$parser->feed('<item>two</item></root>'); |
| 31 |
my $hash = $parser->finish; |
| 32 |
|
| 33 |
Chunks must be UTF-8; bytes are retained only when a token spans two |
| 34 |
calls to "feed". |
| 35 |
|
| 36 |
DESCRIPTION |
| 37 |
This module implements simple hash to XML and XML to hash conversion |
| 38 |
written in C. |
| 39 |
|
| 40 |
During conversion uses minimum of memory, XML or hash is written |
| 41 |
directly without building DOM. |
| 42 |
|
| 43 |
Some features are optional and are available with appropriate libraries: |
| 44 |
|
| 45 |
* XML::LibXML library is required in order to build DOM |
| 46 |
|
| 47 |
* ICU or iconv library is required in order to perform charset |
| 48 |
conversions |
| 49 |
|
| 50 |
FUNCTIONS |
| 51 |
hash2xml $hash, [ %options ] |
| 52 |
$hash is reference to hash |
| 53 |
|
| 54 |
hash2xml |
| 55 |
{ |
| 56 |
node1 => 'value1', |
| 57 |
node2 => [ 'value21', { node22 => 'value22' } ], |
| 58 |
node3 => \'value3', |
| 59 |
node4 => sub { return 'value4' }, |
| 60 |
node5 => sub { return { node51 => 'value51' } }, |
| 61 |
}, |
| 62 |
canonical => 1, |
| 63 |
indent => 2, |
| 64 |
; |
| 65 |
|
| 66 |
will convert to: |
| 67 |
|
| 68 |
<?xml version="1.0" encoding="utf-8"?> |
| 69 |
<root> |
| 70 |
<node1>value1</node1> |
| 71 |
<node2>value21</node2> |
| 72 |
<node2> |
| 73 |
<node22>value22</node22> |
| 74 |
</node2> |
| 75 |
<node3>value3</node3> |
| 76 |
<node4>value4</node4> |
| 77 |
<node5> |
| 78 |
<node51>value51</node51> |
| 79 |
</node5> |
| 80 |
</root> |
| 81 |
|
| 82 |
and (use_attr=1): |
| 83 |
|
| 84 |
hash2xml |
| 85 |
{ |
| 86 |
node1 => 'value1', |
| 87 |
node2 => [ 'value21', { node22 => 'value22' } ], |
| 88 |
node3 => \'value3', |
| 89 |
node4 => sub { return 'value4' }, |
| 90 |
node5 => sub { return { node51 => 'value51' } }, |
| 91 |
}, |
| 92 |
use_attr => 1, |
| 93 |
canonical => 1, |
| 94 |
indent => 2, |
| 95 |
; |
| 96 |
|
| 97 |
will convert to: |
| 98 |
|
| 99 |
<?xml version="1.0" encoding="utf-8"?> |
| 100 |
<root node1="value1" node3="value3" node4="value4"> |
| 101 |
<node2>value21</node2> |
| 102 |
<node2 node22="value22"/> |
| 103 |
<node5 node51="value51"/> |
| 104 |
</root> |
| 105 |
|
| 106 |
xml2hash $xml, [ %options ] |
| 107 |
$xml may be string, reference to string, file handle or tied file |
| 108 |
handle: |
| 109 |
|
| 110 |
xml2hash '<root>text</root>'; |
| 111 |
# output: 'text' |
| 112 |
|
| 113 |
xml2hash '<root a="1" b="2">text</root>'; |
| 114 |
# output: { a => '1', b => '2', content => 'text' } |
| 115 |
|
| 116 |
open(my $fh, '<', 'test.xml'); |
| 117 |
xml2hash $fh; |
| 118 |
|
| 119 |
xml2hash *STDIN; |
| 120 |
|
| 121 |
OPTIONS |
| 122 |
doc [ => 0 ] *# hash2xml* |
| 123 |
if doc is '1', then returned value is XML::LibXML::Document. |
| 124 |
|
| 125 |
root [ = 'root' ] *# hash2xml* |
| 126 |
Root node name. |
| 127 |
|
| 128 |
version [ = '1.0' ] *# hash2xml* |
| 129 |
XML document version |
| 130 |
|
| 131 |
encoding [ = 'utf-8' ] *# hash2xml+xml2hash* |
| 132 |
XML input/output encoding |
| 133 |
|
| 134 |
indent [ = 0 ] *# hash2xml* |
| 135 |
if indent great than "0", XML output should be indented according to |
| 136 |
its hierarchic structure. This value determines the number of |
| 137 |
spaces. |
| 138 |
|
| 139 |
if indent is "0", XML output will all be on one line. |
| 140 |
|
| 141 |
output [ = undef ] *# hash2xml* |
| 142 |
XML output method |
| 143 |
|
| 144 |
if output is undefined, XML document dumped into string. |
| 145 |
|
| 146 |
if output is FH, XML document writes directly to a filehandle or a |
| 147 |
stream. |
| 148 |
|
| 149 |
output_cb [ = undef ] *# hash2xml* |
| 150 |
Callback invoked as "sub { my ($chunk) = @_ }" for each generated |
| 151 |
XML chunk. The complete XML string is not accumulated and "hash2xml" |
| 152 |
returns undef. Chunk size is controlled by "buf_size", except that a |
| 153 |
single value may require a larger chunk. "output" and "output_cb" |
| 154 |
are mutually exclusive. |
| 155 |
|
| 156 |
canonical [ = 0 ] *# hash2xml* |
| 157 |
if canonical is "1", converter will be write hashes sorted by key. |
| 158 |
|
| 159 |
if canonical is "0", order of the element will be pseudo-randomly. |
| 160 |
|
| 161 |
use_attr [ = 0 ] *# hash2xml* |
| 162 |
if use_attr is "1", converter will be use the attributes. |
| 163 |
|
| 164 |
if use_attr is "0", converter will be use tags only. |
| 165 |
|
| 166 |
content [ = undef ] *# hash2xml+xml2hash* |
| 167 |
if defined that the key name for the text content(used only if |
| 168 |
use_attr=1). |
| 169 |
|
| 170 |
force_array => [ = undef ] *# xml2hash* |
| 171 |
This option is similar to "ForceArray" from XML::Simple module: |
| 172 |
<https://metacpan.org/pod/XML::Simple#ForceArray-=%3E-1-%23-in-impor |
| 173 |
tant>. |
| 174 |
|
| 175 |
force_content => [ = 0 ] *# xml2hash* |
| 176 |
This option is similar to "ForceContent" from XML::Simple module: |
| 177 |
<https://metacpan.org/pod/XML::Simple#ForceContent-=%3E-1-%23-in-sel |
| 178 |
dom-used>. |
| 179 |
|
| 180 |
merge_text [ = 0 ] *# xml2hash* |
| 181 |
Setting this option to "1" will cause merge adjacent text nodes. |
| 182 |
|
| 183 |
xml_decl [ = 1 ] *# hash2xml* |
| 184 |
if xml_decl is "1", output will start with the XML declaration |
| 185 |
'<?xml version="1.0" encoding="utf-8"?>'. |
| 186 |
|
| 187 |
if xml_decl is "0", XML declaration will not be output. |
| 188 |
|
| 189 |
trim [ = 0 ] *# hash2xml+xml2hash* |
| 190 |
Trim leading and trailing whitespace from text nodes. |
| 191 |
|
| 192 |
suppress_empty => [ = 0 ] *# xml2hash* |
| 193 |
This option is similar to "SuppressEmpty" from XMl::Simple module: |
| 194 |
<https://metacpan.org/pod/XML::Simple#SuppressEmpty-=%3E-1-%7C-''-%7 |
| 195 |
C-undef-%23-in+out-handy>. |
| 196 |
|
| 197 |
utf8 [ = 1 ] *# hash2xml+xml2hash* |
| 198 |
Turn on utf8 flag for strings if enabled. |
| 199 |
|
| 200 |
max_depth [ = 1024 ] *# xml2hash* |
| 201 |
Maximum recursion depth. |
| 202 |
|
| 203 |
buf_size [ = 4096 ] *# hash2xml+xml2hash* |
| 204 |
Buffer size for reading end encoding data. |
| 205 |
|
| 206 |
keep_root [ = 1 ] *# hash2xml+xml2hash* |
| 207 |
Keep root element. |
| 208 |
|
| 209 |
filter [ = undef ] *# xml2hash* |
| 210 |
Filter nodes matched by pattern and return reference to array of |
| 211 |
nodes. |
| 212 |
|
| 213 |
Sample: |
| 214 |
|
| 215 |
my $xml = <<'XML'; |
| 216 |
<root> |
| 217 |
<item1>111</item1> |
| 218 |
<item2>222</item2> |
| 219 |
<item3>333</item3> |
| 220 |
</root> |
| 221 |
XML |
| 222 |
|
| 223 |
my $nodes = xml2hash($xml, filter => '/root/item1'); |
| 224 |
# $nodes = [ 111 ] |
| 225 |
|
| 226 |
my $nodes = xml2hash($xml, filter => ['/root/item1', '/root/item2']); |
| 227 |
# $nodes = [ 111, 222 ] |
| 228 |
|
| 229 |
my $nodes = xml2hash($xml, filter => qr[/root/item\d$]); |
| 230 |
# $nodes = [ 111, 222, 333 ] |
| 231 |
|
| 232 |
It may be used to parse large XML because does not require a lot of |
| 233 |
memory. |
| 234 |
|
| 235 |
cb [ = undef ] *# xml2hash* |
| 236 |
This option is used in conjunction with "filter" option and defines |
| 237 |
callback that will called for each matched node. |
| 238 |
|
| 239 |
Sample: |
| 240 |
|
| 241 |
xml2hash($xml, filter => qr[/root/item\d$], cb => sub { |
| 242 |
print $_[0], "\n"; |
| 243 |
}); |
| 244 |
# 111 |
| 245 |
# 222 |
| 246 |
# 333 |
| 247 |
|
| 248 |
The callback may return "XML_HASH_XS_STOP" to stop successfully |
| 249 |
after the current matched node. Other return values preserve the |
| 250 |
legacy behaviour and continue parsing. |
| 251 |
|
| 252 |
cb_mode [ = 'node' ] *# xml2hash* |
| 253 |
With "cb_mode => 'events'" the callback receives "($event, $value, |
| 254 |
$meta)". A "start" event contains element "name", "path", "depth" |
| 255 |
and "attributes" in $meta. Returning "XML_HASH_XS_SKIP" from "start" |
| 256 |
skips construction of the element contents and suppresses its "end" |
| 257 |
event. "end" receives the completed node in $value. |
| 258 |
|
| 259 |
The control constants are imported explicitly: |
| 260 |
|
| 261 |
use XML::Hash::XS qw( |
| 262 |
XML_HASH_XS_CONTINUE XML_HASH_XS_STOP XML_HASH_XS_SKIP |
| 263 |
); |
| 264 |
|
| 265 |
method [ = 'NATIVE' ] *# hash2xml* |
| 266 |
experimental support the conversion methods other libraries |
| 267 |
|
| 268 |
if method is 'LX' then conversion result is the same as using |
| 269 |
XML::Hash::LX library |
| 270 |
|
| 271 |
Note: for 'LX' method following additional options are available: |
| 272 |
attr cdata text comm |
| 273 |
|
| 274 |
OBJECT SERIALISATION(hash2xml) |
| 275 |
1. When object has a "toString" method |
| 276 |
In this case, the <toString> method of object is invoked in scalar |
| 277 |
context. It must return a single scalar that can be directly encoded |
| 278 |
into XML. |
| 279 |
|
| 280 |
Example: |
| 281 |
|
| 282 |
use XML::LibXML; |
| 283 |
local $XML::LibXML::skipXMLDeclaration = 1; |
| 284 |
my $doc = XML::LibXML->new->parse_string('<foo bar="1"/>'); |
| 285 |
print hash2xml({ doc => $doc }, indent => 2, xml_decl => 0); |
| 286 |
=> |
| 287 |
<root> |
| 288 |
<doc><foo bar="1"/></doc> |
| 289 |
</root> |
| 290 |
|
| 291 |
2. When object has overloaded stringification |
| 292 |
In this case, the stringification method of object is invoked and |
| 293 |
result is directly encoded into XML. |
| 294 |
|
| 295 |
Example: |
| 296 |
|
| 297 |
package Test { |
| 298 |
use overload '""' => sub { shift->stringify }, fallback => 1; |
| 299 |
sub new { |
| 300 |
my ($class, $str) = @_; |
| 301 |
bless { str => $str }, $class; |
| 302 |
} |
| 303 |
sub stringify { |
| 304 |
shift->{str} |
| 305 |
} |
| 306 |
} |
| 307 |
my $obj = Test->new('test string'); |
| 308 |
print hash2xml({ obj => $obj }, indent => 2, xml_decl => 0); |
| 309 |
=> |
| 310 |
<root> |
| 311 |
<obj>test string</obj> |
| 312 |
</root> |
| 313 |
|
| 314 |
3. When object has a "iternext" method ("NATIVE" method only) |
| 315 |
In this case, the <iternext> method method will invoke a few times |
| 316 |
until the return value is not undefined. |
| 317 |
|
| 318 |
Example: |
| 319 |
|
| 320 |
my $count = 0; |
| 321 |
my $o = bless {}, 'Iterator'; |
| 322 |
*Iterator::iternext = sub { $count++ < 3 ? { count => $count } : undef }; |
| 323 |
print hash2xml({ item => $o }, use_attr => 1, indent => 2, xml_decl => 0); |
| 324 |
=> |
| 325 |
<root> |
| 326 |
<item count="1"/> |
| 327 |
<item count="2"/> |
| 328 |
<item count="3"/> |
| 329 |
</root> |
| 330 |
|
| 331 |
This can be used to generate a large XML using minimum memory, example |
| 332 |
with DBI: |
| 333 |
|
| 334 |
my $sth = $dbh->prepare('SELECT * FROM foo WHERE bar=?'); |
| 335 |
$sth->execute(...); |
| 336 |
my $o = bless {}, 'Iterator'; |
| 337 |
*Iterator::iternext = sub { $sth->fetchrow_hashref() }; |
| 338 |
open(my $fh, '>', 'data.xml'); |
| 339 |
hash2xml({ row => $o }, use_attr => 1, indent => 2, xml_decl => 0, output => $fh); |
| 340 |
=> |
| 341 |
<root> |
| 342 |
<row bar="..." ... /> |
| 343 |
<row bar="..." ... /> |
| 344 |
... |
| 345 |
</root> |
| 346 |
|
| 347 |
BENCHMARK |
| 348 |
Performance benchmark in comparison with some popular modules(hash2xml): |
| 349 |
|
| 350 |
Rate XML::Hash XML::Hash::LX XML::Simple XML::Hash::XS |
| 351 |
XML::Hash 65.0/s -- -6% -37% -99% |
| 352 |
XML::Hash::LX 68.8/s 6% -- -33% -99% |
| 353 |
XML::Simple 103/s 58% 49% -- -98% |
| 354 |
XML::Hash::XS 4879/s 7404% 6988% 4658% -- |
| 355 |
|
| 356 |
Benchmark was done on <http://search.cpan.org/uploads.rdf> |
| 357 |
|
| 358 |
AUTHOR |
| 359 |
Yuriy Ustushenko, <[email protected]> |
| 360 |
|
| 361 |
COPYRIGHT AND LICENSE |
| 362 |
Copyright (C) 2012-2021 Yuriy Ustushenko |
| 363 |
|
| 364 |
This library is free software; you can redistribute it and/or modify it |
| 365 |
under the same terms as Perl itself. |
| 366 |
|