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 load_ext_dtd [ = 0 ]
143 Load the external DTD
144
145 # load_ext_dtd = 0
146 <!DOCTYPE foo [<!ENTITY % ent1 SYSTEM "rm -rf /">%ent1; ]><node> text</node>
147
148 # load_ext_dtd = 1
149 <!DOCTYPE foo [<!ENTITY % ent1 SYSTEM "rm -rf /">%ent1; ]><node> text</node>
150 oops!
151
152 expand_entities [ = 0 ]
153 Enable XInclude substitution. (See XML::LibXML::Parser)
154
155 expand_xinclude [ = 0 ]
156 Enable entities expansion. (See XML::LibXML::Parser). (Enabling
157 also enables load_ext_dtd)
158
159 validation [ = 0 ]
160 Enable validating with the DTD. (See XML::LibXML::Parser)
161
162 no_network [ = 1 ]
163 Forbid network access; (See XML::LibXML::Parser)
164
165 If true, all attempts to fetch non-local resources (such as DTD or
166 external entities) will fail
167
168 $XML::Hash::LX::X2A [ = 0 ]
169 Global array casing
170
171 Ignored when "X2H{order}" in effect
172
173 As option should be passed as
174
175 xml2hash $xml, array => 1;
176
177 Effect:
178
179 # $X2A = 0
180 <node><sub/></node> => { node => { sub => '' } }
181
182 # $X2A = 1
183 <node><sub/></node> => { node => [ { sub => [ '' ] } ] }
184
185 %XML::Hash::LX::X2A
186 By element array casing
187
188 Ignored when "X2H{order}" in effect
189
190 As option should be passed as
191
192 xml2hash $xml, array => [ nodes list ];
193
194 Effect:
195
196 # %X2A = ()
197 <node><sub/></node> => { node => { sub => '' } }
198
199 # %X2A = ( sub => 1 )
200 <node><sub/></node> => { node => { sub => [ '' ] } }
201
202 %XML::Hash::LX::H2X
203 Options respecting convertations from hash to xml
204
205 encoding [ = 'utf-8' ]
206 XML output encoding
207
208 attr [ = '-' ]
209 Attribute prefix
210
211 { node => { -attr => "test", sub => 'test' } }
212 <node attr="test"><sub>test</sub></node>
213
214 text [ = '#text' ]
215 Key name for storing text
216
217 { node => { sub => '', '#text' => "test" } }
218 <node>text<sub /></node>
219 # or
220 <node><sub />text</node>
221 # order of keys is not predictable
222
223 trim [ = 1 ]
224 Trim leading and trailing whitespace from text nodes
225
226 # trim = 1
227 { node => { sub => [ ' ', 'test' ], '#text' => "test" } }
228 <node>test<sub>test</sub></node>
229
230 # trim = 0
231 { node => { sub => [ ' ', 'test' ], '#text' => "test" } }
232 <node>test<sub> test</sub></node>
233
234 cdata [ = undef ]
235 When defined, such key elements will be saved as CDATA sections
236
237 # cdata = undef
238 { node => { '#' => 'test' } } => <node><#>test</#></node> # it's bad ;)
239
240 # cdata = '#'
241 { node => { '#' => 'test' } } => <node><![CDATA[test]]></node>
242
243 comm [ = undef ]
244 When defined, such key elements will be saved as comment sections
245
246 # comm = undef
247 { node => { '/' => 'test' } } => <node></>test<//></node> # it's very bad! ;)
248
249 # comm = '/'
250 { node => { '/' => 'test' } } => <node><!-- test --></node>
251
253 None known
254
256 • XML::Parser::Style::EasyTree
257
258 With default settings should produce the same output as this
259 module. Settings are similar by effect
260
262 Mons Anderson, "<mons at cpan.org>"
263
265 Copyright 2009-2020 Mons Anderson, all rights reserved.
266
267 This program is free software; you can redistribute it and/or modify it
268 under the same terms as Perl itself.
269
270
271
272perl v5.32.1 2021-01-27 XML::Hash::LX(3)