package XML::Hash::XS;
use 5.008008;
use strict;
use warnings;
use vars qw($VERSION @EXPORT @EXPORT_OK);
use base 'Exporter';
@EXPORT = qw( hash2xml xml2hash );
@EXPORT_OK = (@EXPORT, qw(
XML_HASH_XS_CONTINUE XML_HASH_XS_STOP XML_HASH_XS_SKIP
));
sub XML_HASH_XS_CONTINUE () { 'XML_HASH_XS_CONTINUE' }
sub XML_HASH_XS_STOP () { 'XML_HASH_XS_STOP' }
sub XML_HASH_XS_SKIP () { 'XML_HASH_XS_SKIP' }
$VERSION = '0.66';
require XSLoader;
XSLoader::load('XML::Hash::XS', $VERSION);
use vars qw($method $output $output_cb $root $version $encoding $utf8 $indent $canonical
$use_attr $content $xml_decl $doc $max_depth $attr $text $trim $cdata
$comm $buf_size $keep_root $force_array $force_content $merge_text
$suppress_empty $cb_mode
);
# 'NATIVE' or 'LX'
$method = 'NATIVE';
# native options
$output = undef;
$output_cb = undef;
$root = 'root';
$version = '1.0';
$encoding = '';
$utf8 = 1;
$indent = 0;
$canonical = 0;
$use_attr = 0;
$content = undef;
$xml_decl = 1;
$keep_root = 1;
$doc = 0;
$max_depth = 1024;
$buf_size = 4096;
$trim = 0;
$force_array = undef;
$force_content = 0;
$merge_text = 0;
$suppress_empty = 0;
$cb_mode = 'node';
# XML::Hash::LX options
$attr = '-';
$text = '#text';
$cdata = undef;
$comm = undef;
1;
__END__
=head1 NAME
XML::Hash::XS - Simple and fast hash to XML and XML to hash conversion written in C
=begin HTML

=end HTML
=head1 SYNOPSIS
use XML::Hash::XS;
my $xmlstr = hash2xml \%hash;
hash2xml \%hash, output => $fh;
hash2xml \%hash, output_cb => sub { my ($chunk) = @_; print $chunk };
my $hash = xml2hash $xmlstr;
my $hash = xml2hash \$xmlstr;
my $hash = xml2hash 'test.xml', encoding => 'cp1251';
my $hash = xml2hash $fh;
my $hash = xml2hash *STDIN;
Or OOP way:
use XML::Hash::XS qw();
my $conv = XML::Hash::XS->new(utf8 => 0, encoding => 'utf-8')
my $xmlstr = $conv->hash2xml(\%hash, utf8 => 1);
my $hash = $conv->xml2hash($xmlstr, encoding => 'cp1251');
For incremental XML input, create a parser:
my $parser = XML::Hash::XS::Parser->new(keep_root => 1);
$parser->feed('- one
');
$parser->feed('- two
');
my $hash = $parser->finish;
Chunks must be UTF-8; bytes are retained only when a token spans two calls
to C.
=head1 DESCRIPTION
This module implements simple hash to XML and XML to hash conversion written in C.
During conversion uses minimum of memory, XML or hash is written directly without building DOM.
Some features are optional and are available with appropriate libraries:
=over 2
=item * XML::LibXML library is required in order to build DOM
=item * ICU or iconv library is required in order to perform charset conversions
=back
=head1 FUNCTIONS
=head2 hash2xml $hash, [ %options ]
$hash is reference to hash
hash2xml
{
node1 => 'value1',
node2 => [ 'value21', { node22 => 'value22' } ],
node3 => \'value3',
node4 => sub { return 'value4' },
node5 => sub { return { node51 => 'value51' } },
},
canonical => 1,
indent => 2,
;
will convert to:
value1
value21
value22
value3
value4
value51
and (use_attr=1):
hash2xml
{
node1 => 'value1',
node2 => [ 'value21', { node22 => 'value22' } ],
node3 => \'value3',
node4 => sub { return 'value4' },
node5 => sub { return { node51 => 'value51' } },
},
use_attr => 1,
canonical => 1,
indent => 2,
;
will convert to:
value21
=head2 xml2hash $xml, [ %options ]
$xml may be string, reference to string, file handle or tied file handle:
xml2hash 'text';
# output: 'text'
xml2hash 'text';
# output: { a => '1', b => '2', content => 'text' }
open(my $fh, '<', 'test.xml');
xml2hash $fh;
xml2hash *STDIN;
=head1 OPTIONS
=over 4
=item doc [ => 0 ] I<# hash2xml>
if doc is '1', then returned value is L.
=item root [ = 'root' ] I<# hash2xml>
Root node name.
=item version [ = '1.0' ] I<# hash2xml>
XML document version
=item encoding [ = 'utf-8' ] I<# hash2xml+xml2hash>
XML input/output encoding
=item indent [ = 0 ] I<# hash2xml>
if indent great than "0", XML output should be indented according to its hierarchic structure.
This value determines the number of spaces.
if indent is "0", XML output will all be on one line.
=item output [ = undef ] I<# hash2xml>
XML output method
if output is undefined, XML document dumped into string.
if output is FH, XML document writes directly to a filehandle or a stream.
=item output_cb [ = undef ] I<# hash2xml>
Callback invoked as C<< sub { my ($chunk) = @_ } >> for each generated XML
chunk. The complete XML string is not accumulated and C returns
undef. Chunk size is controlled by C, except that a single value may
require a larger chunk. C