.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "XML::Hash::LX 3pm" .TH XML::Hash::LX 3pm "2021-01-05" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" XML::Hash::LX \- Convert hash to xml and xml to hash using LibXML .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use XML::Hash::LX; \& \& my $hash = xml2hash $xmlstring, attr => \*(Aq.\*(Aq, text => \*(Aq~\*(Aq; \& my $hash = xml2hash $xmldoc; \& \& my $xmlstr = hash2html $hash, attr => \*(Aq+\*(Aq, text => \*(Aq#text\*(Aq; \& my $xmldoc = hash2html $hash, doc => 1, attr => \*(Aq+\*(Aq; \& \& # Usage with XML::LibXML \& \& my $doc = XML::LibXML\->new\->parse_string($xml); \& my $xp = XML::LibXML::XPathContext\->new($doc); \& $xp\->registerNs(\*(Aqrss\*(Aq, \*(Aqhttp://purl.org/rss/1.0/\*(Aq); \& \& # then process xpath \& for ($xp\->findnodes(\*(Aq//rss:item\*(Aq)) { \& # and convert to hash concrete nodes \& my $item = xml2hash($_); \& print Dumper+$item \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module is a companion for \f(CW\*(C`XML::LibXML\*(C'\fR. It operates with LibXML objects, could return or accept LibXML objects, and may be used for easy data transformations .PP It is faster in parsing then XML::Simple, XML::Hash, XML::Twig and of course much slower than XML::Bare ;) .PP It is faster in composing than XML::Hash, but slower than XML::Simple .PP Parse benchmark: .PP .Vb 6 \& Rate Simple Hash Twig Hash::LX Bare \& Simple 11.3/s \-\- \-2% \-16% \-44% \-97% \& Hash 11.6/s 2% \-\- \-14% \-43% \-97% \& Twig 13.5/s 19% 16% \-\- \-34% \-96% \& Hash::LX 20.3/s 79% 75% 51% \-\- \-95% \& Bare 370/s 3162% 3088% 2650% 1721% \-\- .Ve .PP Compose benchmark: .PP .Vb 4 \& Rate Hash Hash::LX Simple \& Hash 49.2/s \-\- \-18% \-40% \& Hash::LX 60.1/s 22% \-\- \-26% \& Simple 81.5/s 66% 36% \-\- .Ve .PP Benchmark was done on .SH "EXPORT" .IX Header "EXPORT" \&\f(CW\*(C`xml2hash\*(C'\fR and \f(CW\*(C`hash2xml\*(C'\fR are exported by default .SS ":inject" .IX Subsection ":inject" Inject toHash method in the namespace of XML::LibXML::Node and allow one to call it on any subclass of XML::LibXML::Node directly .PP By default is disabled .PP .Vb 1 \& use XML::Hash::LX \*(Aq:inject\*(Aq; \& \& my $doc = XML::LibXML\->new\->parse_string($xml); \& my $hash = $doc\->toHash(%opts); .Ve .SH "FUNCTIONS" .IX Header "FUNCTIONS" .ie n .SS "xml2hash $xml, [ \s-1OPTIONS\s0 ]" .el .SS "xml2hash \f(CW$xml\fP, [ \s-1OPTIONS\s0 ]" .IX Subsection "xml2hash $xml, [ OPTIONS ]" \&\s-1XML\s0 could be XML::LibXML::Document, XML::LibXML::DocumentPart or string .ie n .SS "hash2xml $hash, [ doc => 1, ] [ \s-1OPTIONS\s0 ]" .el .SS "hash2xml \f(CW$hash\fP, [ doc => 1, ] [ \s-1OPTIONS\s0 ]" .IX Subsection "hash2xml $hash, [ doc => 1, ] [ OPTIONS ]" Id \f(CW\*(C`doc\*(C'\fR option is true, then returned value is XML::LibXML::Document, not string .SH "OPTIONS" .IX Header "OPTIONS" Every option could be passed as arguments to function or set as global variable in \f(CW\*(C`XML::Hash::LX\*(C'\fR namespace .ie n .SS "%XML::Hash::LX::X2H" .el .SS "\f(CW%XML::Hash::LX::X2H\fP" .IX Subsection "%XML::Hash::LX::X2H" Options respecting convertations from xml to hash .IP "order [ = 0 ]" 4 .IX Item "order [ = 0 ]" \&\fBStrictly\fR keep the output order. When enabled, structures become more complex, but xml could be completely reverted .IP "attr [ = '\-' ]" 4 .IX Item "attr [ = '-' ]" Attribute prefix .Sp .Vb 1 \& => { node => { \-attr => "test" } } .Ve .IP "text [ = '#text' ]" 4 .IX Item "text [ = '#text' ]" Key name for storing text .Sp .Vb 1 \& text => { node => { sub => \*(Aq\*(Aq, \*(Aq#text\*(Aq => "test" } } .Ve .IP "join [ = '' ]" 4 .IX Item "join [ = '' ]" Join separator for text nodes, split by subnodes .Sp Ignored when \f(CW\*(C`order\*(C'\fR in effect .Sp .Vb 3 \& # default: \& xml2hash( \*(AqTest1Test2\*(Aq ) \& : { item => { sub => \*(Aq\*(Aq, \*(Aq~\*(Aq => \*(AqTest1Test2\*(Aq } }; \& \& # global \& $XML::Hash::LX::X2H{join} = \*(Aq+\*(Aq; \& xml2hash( \*(AqTest1Test2\*(Aq ) \& : { item => { sub => \*(Aq\*(Aq, \*(Aq~\*(Aq => \*(AqTest1+Test2\*(Aq } }; \& \& # argument \& xml2hash( \*(AqTest1Test2\*(Aq, join => \*(Aq+\*(Aq ) \& : { item => { sub => \*(Aq\*(Aq, \*(Aq~\*(Aq => \*(AqTest1+Test2\*(Aq } }; .Ve .IP "trim [ = 1 ]" 4 .IX Item "trim [ = 1 ]" Trim leading and trailing whitespace from text nodes .IP "cdata [ = undef ]" 4 .IX Item "cdata [ = undef ]" When defined, \s-1CDATA\s0 sections will be stored under this key .Sp .Vb 2 \& # cdata = undef \& => { node => \*(Aqtest\*(Aq } \& \& # cdata = \*(Aq#\*(Aq \& => { node => { \*(Aq#\*(Aq => \*(Aqtest\*(Aq } } .Ve .IP "comm [ = undef ]" 4 .IX Item "comm [ = undef ]" When defined, comments sections will be stored under this key .Sp When undef, comments will be ignored .Sp .Vb 2 \& # comm = undef \& => { node => { sub => \*(Aq\*(Aq } } \& \& # comm = \*(Aq/\*(Aq \& => { node => { sub => \*(Aq\*(Aq, \*(Aq/\*(Aq => \*(Aqcomm\*(Aq } } .Ve .ie n .SS "$XML::Hash::LX::X2A [ = 0 ]" .el .SS "\f(CW$XML::Hash::LX::X2A\fP [ = 0 ]" .IX Subsection "$XML::Hash::LX::X2A [ = 0 ]" Global array casing .PP Ignored when \f(CW\*(C`X2H{order}\*(C'\fR in effect .PP As option should be passed as .PP .Vb 1 \& xml2hash $xml, array => 1; .Ve .PP Effect: .PP .Vb 2 \& # $X2A = 0 \& => { node => { sub => \*(Aq\*(Aq } } \& \& # $X2A = 1 \& => { node => [ { sub => [ \*(Aq\*(Aq ] } ] } .Ve .ie n .SS "%XML::Hash::LX::X2A" .el .SS "\f(CW%XML::Hash::LX::X2A\fP" .IX Subsection "%XML::Hash::LX::X2A" By element array casing .PP Ignored when \f(CW\*(C`X2H{order}\*(C'\fR in effect .PP As option should be passed as .PP .Vb 1 \& xml2hash $xml, array => [ nodes list ]; .Ve .PP Effect: .PP .Vb 2 \& # %X2A = () \& => { node => { sub => \*(Aq\*(Aq } } \& \& # %X2A = ( sub => 1 ) \& => { node => { sub => [ \*(Aq\*(Aq ] } } .Ve .ie n .SS "%XML::Hash::LX::H2X" .el .SS "\f(CW%XML::Hash::LX::H2X\fP" .IX Subsection "%XML::Hash::LX::H2X" Options respecting convertations from hash to xml .IP "encoding [ = 'utf\-8' ]" 4 .IX Item "encoding [ = 'utf-8' ]" \&\s-1XML\s0 output encoding .IP "attr [ = '\-' ]" 4 .IX Item "attr [ = '-' ]" Attribute prefix .Sp .Vb 2 \& { node => { \-attr => "test", sub => \*(Aqtest\*(Aq } } \& test .Ve .IP "text [ = '#text' ]" 4 .IX Item "text [ = '#text' ]" Key name for storing text .Sp .Vb 5 \& { node => { sub => \*(Aq\*(Aq, \*(Aq#text\*(Aq => "test" } } \& text \& # or \& text \& # order of keys is not predictable .Ve .IP "trim [ = 1 ]" 4 .IX Item "trim [ = 1 ]" Trim leading and trailing whitespace from text nodes .Sp .Vb 3 \& # trim = 1 \& { node => { sub => [ \*(Aq \*(Aq, \*(Aqtest\*(Aq ], \*(Aq#text\*(Aq => "test" } } \& testtest \& \& # trim = 0 \& { node => { sub => [ \*(Aq \*(Aq, \*(Aqtest\*(Aq ], \*(Aq#text\*(Aq => "test" } } \& test test .Ve .IP "cdata [ = undef ]" 4 .IX Item "cdata [ = undef ]" When defined, such key elements will be saved as \s-1CDATA\s0 sections .Sp .Vb 2 \& # cdata = undef \& { node => { \*(Aq#\*(Aq => \*(Aqtest\*(Aq } } => <#>test # it\*(Aqs bad ;) \& \& # cdata = \*(Aq#\*(Aq \& { node => { \*(Aq#\*(Aq => \*(Aqtest\*(Aq } } => .Ve .IP "comm [ = undef ]" 4 .IX Item "comm [ = undef ]" When defined, such key elements will be saved as comment sections .Sp .Vb 2 \& # comm = undef \& { node => { \*(Aq/\*(Aq => \*(Aqtest\*(Aq } } => test # it\*(Aqs very bad! ;) \& \& # comm = \*(Aq/\*(Aq \& { node => { \*(Aq/\*(Aq => \*(Aqtest\*(Aq } } => .Ve .SH "BUGS" .IX Header "BUGS" None known .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 XML::Parser::Style::EasyTree .Sp With default settings should produce the same output as this module. Settings are similar by effect .SH "AUTHOR" .IX Header "AUTHOR" Mons Anderson, \f(CW\*(C`\*(C'\fR .SH "COPYRIGHT & LICENSE" .IX Header "COPYRIGHT & LICENSE" Copyright 2009 Mons Anderson, all rights reserved. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.