1XML::Hash::LX(3) User Contributed Perl Documentation XML::Hash::LX(3)
2
3
4
6 XML::Hash::LX - Convert hash to xml and xml to hash using LibXML
7
9 use XML::Hash::LX;
10
11 my $hash = xml2hash $xmlstring, attr => '.', text => '~';
12 my $hash = xml2hash $xmldoc;
13
14 my $xmlstr = hash2html $hash, attr => '+', text => '#text';
15 my $xmldoc = hash2html $hash, doc => 1, attr => '+';
16
17 # Usage with XML::LibXML
18
19 my $doc = XML::LibXML->new->parse_string($xml);
20 my $xp = XML::LibXML::XPathContext->new($doc);
21 $xp->registerNs('rss', 'http://purl.org/rss/1.0/');
22
23 # then process xpath
24 for ($xp->findnodes('//rss:item')) {
25 # and convert to hash concrete nodes
26 my $item = xml2hash($_);
27 print Dumper+$item
28 }
29
31 This module is a companion for "XML::LibXML". It operates with LibXML
32 objects, could return or accept LibXML objects, and may be used for
33 easy data transformations
34
35 It is faster in parsing then XML::Simple, XML::Hash, XML::Twig and of
36 course much slower than XML::Bare ;)
37
38 It is faster in composing than XML::Hash, but slower than XML::Simple
39
40 Parse benchmark:
41
42 Rate Simple Hash Twig Hash::LX Bare
43 Simple 11.3/s -- -2% -16% -44% -97%
44 Hash 11.6/s 2% -- -14% -43% -97%
45 Twig 13.5/s 19% 16% -- -34% -96%
46 Hash::LX 20.3/s 79% 75% 51% -- -95%
47 Bare 370/s 3162% 3088% 2650% 1721% --
48
49 Compose benchmark:
50
51 Rate Hash Hash::LX Simple
52 Hash 49.2/s -- -18% -40%
53 Hash::LX 60.1/s 22% -- -26%
54 Simple 81.5/s 66% 36% --
55
56 Benchmark was done on <http://search.cpan.org/uploads.rdf>
57
59 "xml2hash" and "hash2xml" are exported by default
60
61 :inject
62 Inject toHash method in the namespace of XML::LibXML::Node and allow to
63 call it on any subclass of XML::LibXML::Node directly
64
65 By default is disabled
66
67 use XML::Hash::LX ':inject';
68
69 my $doc = XML::LibXML->new->parse_string($xml);
70 my $hash = $doc->toHash(%opts);
71
73 xml2hash $xml, [ OPTIONS ]
74 XML could be XML::LibXML::Document, XML::LibXML::DocumentPart or string
75
76 hash2xml $hash, [ doc => 1, ] [ OPTIONS ]
77 Id "doc" option is true, then returned value is XML::LibXML::Document,
78 not string
79
81 Every option could be passed as arguments to function or set as global
82 variable in "XML::Hash::LX" namespace
83
84 %XML::Hash::LX::X2H
85 Options respecting convertations from xml to hash
86
87 order [ = 0 ]
88 Strictly keep the output order. When enabled, structures become
89 more complex, but xml could be completely reverted
90
91 attr [ = '-' ]
92 Attribute prefix
93
94 <node attr="test" /> => { node => { -attr => "test" } }
95
96 text [ = '#text' ]
97 Key name for storing text
98
99 <node>text<sub /></node> => { node => { sub => '', '#text' => "test" } }
100
101 join [ = '' ]
102 Join separator for text nodes, splitted by subnodes
103
104 Ignored when "order" in effect
105
106 # default:
107 xml2hash( '<item>Test1<sub />Test2</item>' )
108 : { item => { sub => '', '~' => 'Test1Test2' } };
109
110 # global
111 $XML::Hash::LX::X2H{join} = '+';
112 xml2hash( '<item>Test1<sub />Test2</item>' )
113 : { item => { sub => '', '~' => 'Test1+Test2' } };
114
115 # argument
116 xml2hash( '<item>Test1<sub />Test2</item>', join => '+' )
117 : { item => { sub => '', '~' => 'Test1+Test2' } };
118
119 trim [ = 1 ]
120 Trim leading and trailing whitespace from text nodes
121
122 cdata [ = undef ]
123 When defined, CDATA sections will be stored under this key
124
125 # cdata = undef
126 <node><![CDATA[ test ]]></node> => { node => 'test' }
127
128 # cdata = '#'
129 <node><![CDATA[ test ]]></node> => { node => { '#' => 'test' } }
130
131 comm [ = undef ]
132 When defined, comments sections will be stored under this key
133
134 When undef, comments will be ignored
135
136 # comm = undef
137 <node><!-- comm --><sub/></node> => { node => { sub => '' } }
138
139 # comm = '/'
140 <node><!-- comm --><sub/></node> => { node => { sub => '', '/' => 'comm' } }
141
142 $XML::Hash::LX::X2A [ = 0 ]
143 Global array casing
144
145 Ignored when "X2H{order}" in effect
146
147 As option should be passed as
148
149 xml2hash $xml, array => 1;
150
151 Effect:
152
153 # $X2A = 0
154 <node><sub/></node> => { node => { sub => '' } }
155
156 # $X2A = 1
157 <node><sub/></node> => { node => [ { sub => [ '' ] } ] }
158
159 %XML::Hash::LX::X2A
160 By element array casing
161
162 Ignored when "X2H{order}" in effect
163
164 As option should be passed as
165
166 xml2hash $xml, array => [ nodes list ];
167
168 Effect:
169
170 # %X2A = ()
171 <node><sub/></node> => { node => { sub => '' } }
172
173 # %X2A = ( sub => 1 )
174 <node><sub/></node> => { node => { sub => [ '' ] } }
175
176 %XML::Hash::LX::H2X
177 Options respecting convertations from hash to xml
178
179 encoding [ = 'utf-8' ]
180 XML output encoding
181
182 attr [ = '-' ]
183 Attribute prefix
184
185 { node => { -attr => "test", sub => 'test' } }
186 <node attr="test"><sub>test</sub></node>
187
188 text [ = '#text' ]
189 Key name for storing text
190
191 { node => { sub => '', '#text' => "test" } }
192 <node>text<sub /></node>
193 # or
194 <node><sub />text</node>
195 # order of keys is not predictable
196
197 trim [ = 1 ]
198 Trim leading and trailing whitespace from text nodes
199
200 # trim = 1
201 { node => { sub => [ ' ', 'test' ], '#text' => "test" } }
202 <node>test<sub>test</sub></node>
203
204 # trim = 0
205 { node => { sub => [ ' ', 'test' ], '#text' => "test" } }
206 <node>test<sub> test</sub></node>
207
208 cdata [ = undef ]
209 When defined, such key elements will be saved as CDATA sections
210
211 # cdata = undef
212 { node => { '#' => 'test' } } => <node><#>test</#></node> # it's bad ;)
213
214 # cdata = '#'
215 { node => { '#' => 'test' } } => <node><![CDATA[test]]></node>
216
217 comm [ = undef ]
218 When defined, such key elements will be saved as comment sections
219
220 # comm = undef
221 { node => { '/' => 'test' } } => <node></>test<//></node> # it's very bad! ;)
222
223 # comm = '/'
224 { node => { '/' => 'test' } } => <node><!-- test --></node>
225
227 None known
228
230 ยท XML::Parser::Style::EasyTree
231
232 With default settings should produce the same output as this
233 module. Settings are similar by effect
234
236 Mons Anderson, "<mons at cpan.org>"
237
239 Copyright 2009 Mons Anderson, all rights reserved.
240
241 This program is free software; you can redistribute it and/or modify it
242 under the same terms as Perl itself.
243
244
245
246perl v5.28.0 2011-08-19 XML::Hash::LX(3)