1XML::Smart::Tutorial(3)User Contributed Perl DocumentatioXnML::Smart::Tutorial(3)
2
3
4
6 XML::Smart::Tutorial - Tutorial and examples for XML::Smart.
7
9 This document is a tutorial for XML::Smart and shows some examples of
10 usual things.
11
13 In XML::Smart the key CONTENT is reserved and shouldn't be used
14 directly, since XML::Smart will deal with the convertion of arguments
15 to node contents, including multiple node contents autimatically.
16
17 What happens when you set a value:
18 $xml->{root}{foo} = 'simple value' ;
19
20 Here foo will be a normal argument/attribute value, and will generate
21 this XML data:
22
23 <root foo="simple value"/>
24
25 But if you insert some tag or lines in the values by default XML::Smart
26 will convert it to a node content:
27
28 $xml->{root}{foo} = "line0\nlien1\nline2\n" ;
29
30 And will generate that XML data:
31
32 <root>
33 <foo>line0
34 lien1
35 line2
36 </foo>
37 </root>
38
39 But what you can do if you want to force some type, let's say, have a
40 node content with a simple value:
41
42 $xml->{root}{foo} = 'simple value' ;
43 $xml->{root}{foo}->set_node(1) ;
44
45 And will generate that XML data:
46
47 <root>
48 <foo>simple value</foo>
49 </root>
50
51 Multiple contents:
52 When you have interpolated content/data you need to work in a
53 different. Let's say that you load this XML data:
54
55 <root>
56 content0
57 <tag1 arg="1"/>
58 content1
59 </root>
60
61 If you access directly the root key as string you will get all the
62 content parts grouped. So, this code:
63
64 my $xml = new XML::Smart(q`
65 <root>
66 content0
67 <tag1 arg="1"/>
68 content1
69 </root>
70 `,'smart') ;
71
72 print "#$xml->{root}#" ;
73
74 Will print that:
75
76 #
77 content0
78
79 content1
80 #
81
82 To access each part of the content independently you should use an
83 array that receive the method content():
84
85 my @content = $xml->{root}->content ;
86
87 print "#$content[0]#\n" ;
88
89 And this will print that:
90
91 #
92 content0
93 #
94
95 Now to set the multiple content values you should use the method
96 content() with 2 arguments:
97
98 $xml->{root}->content(0,'new content') ;
99
100 And now the XML data produced will be:
101
102 <root>new content<tag1 arg="1"/>
103 content1
104 </root>
105
106 If you use the method content() with only one argument it will remove
107 all the multiple contents and will set the new value in the place of
108 the 1st content.
109
111 By defaul XML::Smart will use XML::Parser or XML::Smart::Parser (in
112 this order of preference) to load a XML data.
113
114 To force or define by your self the parser you can use the 2nd argument
115 option when creating a XML::Smart object:
116
117 my $xml = new XML::Smart( 'some.xml' , 'XML::Parser' ) ;
118
119 ## and
120
121 my $xml = new XML::Smart( 'some.xml' , 'XML::Smart::Parser' ) ;
122
123 XML::Smart also has an extra parser, XML::Smart::HTMLParser, that can
124 be used to load HTML as XML, or to load wild XML data:
125
126 my $xml = new XML::Smart( 'some.xml' , 'XML::Smart::HTMLParser' ) ;
127
128 Aliases for the parser options:
129
130 SMART|REGEXP => XML::Smart::Parser
131 HTML => XML::Smart::HTMLParser
132
133 So, you can use as:
134
135 my $xml = new XML::Smart( 'some.xml' , 'smart' ) ;
136 my $xml = new XML::Smart( 'some.xml' , 'html' ) ;
137
139 You can customize the way that the parser will treat the XML data:
140
141 Forcing nodes/tags and arguments/attributes to lowercase or upercase:
142 ## For lower case:
143
144 my $xml = new XML::Smart( 'some.xml' ,
145 lowtag => 1 ,
146 lowarg => 1 ,
147 ) ;
148
149 ## For uper case:
150
151 my $xml = new XML::Smart( 'some.xml' ,
152 upertag => 1 ,
153 uperarg => 1 ,
154 ) ;
155
156 Loading arguments without values (flags) as a TRUE boolean:
157 ** Note, this option will work only when the XML is parsed by
158 XML::Smart::HTMLParser, since only it accept arguments without values!
159
160 my $xml = new XML::Smart(
161 '<root><foo arg1="" flag></root>' ,
162 'XML::Smart::HTMLParser' ,
163 arg_single => 1 ,
164 ) ;
165
166 Here's the tree of the example above:
167
168 'root' => {
169 'foo' => {
170 'flag' => 1,
171 'arg1' => ''
172 },
173 },
174
175 Customizing the parse events:
176 XML::Smart can redirect the parsing process to personalized functions:
177
178 my $xml = XML::Smart->new( 'some.xml' ,
179 on_start => \&on_start ,
180 on_char => \&on_char ,
181 on_end => \&on_end ,
182 ) ;
183
184 sub on_start {
185 my ( $tag , $pointer , $pointer_back ) = @_ ;
186 $pointer->{$tag}{type_user} = 1 if $tag =~ /(?:name|age)/ ;
187 }
188
189 sub on_char {
190 my ( $tag , $pointer , $pointer_back , $content) = @_ ;
191 $$content =~ s/\s+/ /gs ;
192 }
193
194 sub on_end {
195 my ( $tag , $pointer , $pointer_back ) = @_ ;
196 $pointer->{$tag}{type_extra} = 1 if $tag =~ /(?:more|tel|address)/ ;
197 }
198
200 Graciliano M. P. <gm@virtuasites.com.br>
201
202 I will appreciate any type of feedback (include your opinions and/or
203 suggestions). ;-P
204
205 Enjoy and thanks for who are enjoying this tool and have sent e-mails!
206 ;-P
207
209 This document was written in ePod (easy-POD), than converted to POD,
210 and from here you know the way.
211
212
213
214perl v5.32.1 2021-01-27 XML::Smart::Tutorial(3)