1XML::Smart::FAQ(3) User Contributed Perl Documentation XML::Smart::FAQ(3)
2
3
4
6 XML::Smart::FAQ - Frequently Asked Questions about XML::Smart.
7
9 This is the Frequently Asked Questions list for XML::Smart.
10
12 For new questions send an e-mail to the author, but please, read first
13 all the F.A.Q.
14
15 Do I need to install XML::Parser to can use XML::Smart?
16 No! XML::Smart already comes with 2 independent parsers,
17 XML::Smart::Parser and XML::Smart::HTMLParser.
18
19 If XML::Parser is found XML::Smart will use it by default, and the 2nd
20 options will be XML::Smart::Parser.
21
22 Note that for complex parsing XML::Parser is recommended, but
23 XML::Smart::Parser will work fine too.
24
25 What is the best version of XML::Smart to install?
26 Is always the last! Always take a look for new versions before aks for
27 help on XML::Smart.
28
29 Note that internally XML::Smart is complex, since the main idea of it
30 is to remove the complexity from the hand of the programmer. Actually
31 the idea is to enable the Perl programmer to use and create XML data
32 without really know the XML format.
33
34 Where can I learn about XML?
35 http://www.xml.com
36
37 How to apply a DTD to a XML::Smart object tree?
38 Take a look in the method apply_dtd(). Example of use:
39
40 $xml->apply_dtd(q`
41 <!DOCTYPE cds [
42 <!ELEMENT cds (album+)>
43 <!ATTLIST cds
44 creator CDATA
45 date CDATA #REQUIRED
46 >
47 <!ELEMENT album (artist , tracks+)>
48 <!ELEMENT artist (#PCDATA)>
49 <!ELEMENT tracks (#PCDATA)>
50 ]>
51 `);
52
53 This will format automatically elements, attributes, etc...
54
55 How XML::Smart works?
56 To create XML::Smart, first I have created the module
57 Object::MultiType. With it you can have an object that works at the
58 same time as a HASH, ARRAY, SCALAR, CODE & GLOB. So you can do things
59 like this with the same object:
60
61 $obj = Object::MultiType->new() ;
62
63 $obj->{key} ;
64 $obj->[0] ;
65 $obj->method ;
66
67 @l = @{$obj} ;
68 %h = %{$obj} ;
69
70 &$obj(args) ;
71
72 print $obj "send data\n" ;
73
74 Seems to be crazy, and can be more if you use tie() inside it, and this
75 is what XML::Smart does.
76
77 For XML::Smart, the access in the Hash and Array way paste through
78 tie(). In other words, you have a tied HASH and tied ARRAY inside it.
79 This tied Hash and Array work together, soo you can access a Hash key
80 as the index 0 of an Array, or access an index 0 as the Hash key:
81
82 %hash = (
83 key => ['a','b','c']
84 ) ;
85
86 $hash->{key} ## return $hash{key}[0]
87 $hash->{key}[0] ## return $hash{key}[0]
88 $hash->{key}[1] ## return $hash{key}[1]
89
90 ## Inverse:
91
92 %hash = ( key => 'a' ) ;
93
94 $hash->{key} ## return $hash{key}
95 $hash->{key}[0] ## return $hash{key}
96 $hash->{key}[1] ## return undef
97
98 The best thing of this new resource is to avoid wrong access to the
99 data and warnings when you try to access a Hash having an Array (and
100 the inverse). Thing that generally make the script die().
101
102 Once having an easy access to the data, you can use the same resource
103 to create data! For example:
104
105 ## Previous data:
106 <hosts>
107 <server address="192.168.2.100" os="linux" type="conectiva" version="9.0"/>
108 </hosts>
109
110 ## Now you have {address} as a normal key with a string inside:
111 $XML->{hosts}{server}{address}
112
113 ## And to add a new address, the key {address} need to be an ARRAY ref!
114 ## So, XML::Smart make the convertion: ;-P
115 $XML->{hosts}{server}{address}[1] = '192.168.2.101' ;
116
117 ## Adding to a list that you don't know the size:
118 push(@{$XML->{hosts}{server}{address}} , '192.168.2.102') ;
119
120 ## The data now:
121 <hosts>
122 <server os="linux" type="conectiva" version="9.0"/>
123 <address>192.168.2.100</address>
124 <address>192.168.2.101</address>
125 <address>192.168.2.102</address>
126 </server>
127 </hosts>
128
129 Than after changing your XML tree using the Hash and Array resources
130 you just get the data remade (through the Hash tree inside the object):
131
132 my $xmldata = $XML->data ;
133
134 But note that XML::Smart always return an object! Even when you get a
135 final key. So this actually returns another object, pointhing (inside
136 it) to the key:
137
138 $addr = $XML->{hosts}{server}{address}[0] ;
139
140 ## Since $addr is an object you can TRY to access more data:
141 $addr->{foo}{bar} ; ## This doens't make warnings! just return UNDEF.
142
143 ## But you can use it like a normal SCALAR too:
144
145 print "$addr\n" ;
146
147 $addr .= ':80' ; ## After this $addr isn't an object any more, just a SCALAR!
148
149 When I generate the XML data new lines (\n) are added to the content!
150 You should use the options for the method data() and save() to not add
151 identation to the generated data:
152
153 $XML->data( noident => 1 ) ;
154
155 ## or better:
156
157 $XML->data( nospace => 1 ) ;
158
159 Your question is not here?
160 Just send me an e-mail. ;-P
161
163 Graciliano M. P. <gm@virtuasites.com.br>
164
165 I will appreciate any type of feedback (include your opinions and/or
166 suggestions). ;-P
167
168 Enjoy and thanks for who are enjoying this tool and have sent e-mails!
169 ;-P
170
172 This document was written in ePod (easy-POD), than converted to POD,
173 and from here you know the way.
174
175
176
177perl v5.34.0 2022-01-21 XML::Smart::FAQ(3)