blib/lib/XML/TinyXML.pm

filter: Statementstmt 71.6% 106 / 148 Branchbran 43.2% 32 / 74 Conditioncond 12.5% 3 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 73.6% 28 / 38 Podpod 89.6% 26 / 29 totaltotal 59.4% 195 / 328 CC 46 SCAR 24.9
CC
46
Cov
57%
addChildNode
56.0
new
25.1
getChildNodeByName
20.0
CRAP 12.1
« blib/lib/XML/TinyXML/Selector/XPath.pm ↑ Summary blib/lib/XML/TinyXML/Selector/XPath/Functions.pm »
1 =head1 NAME
2  
3 XML::TinyXML - Little and efficient Perl module to manage xml data.
4  
5 =head1 SYNOPSIS
6  
7 use XML::TinyXML;
8  
9 # First create an XML Context
10 $xml = XML::TinyXML->new();
11
12 # and add a root node
13 $xml->addRootNode('nodelabel', 'somevalue', { attr1 => v1, attr2 => v2 });
14  
15 # if you want to reuse a previously created node object ...
16 #
17 # ( you can create one calling :
18 # * %attrs = ( attr1 => v1, attr2 => v2 );
19 # * $node = XML::TinyXML::Node->new('nodelabel', param => 'somevalue', attrs => \%attrs);
20 # )
21 #
22 # the new XML Context can be created giving the root node directly to the constructor
23 $xml = XML::TinyXML->new($node);
24  
25 ######
26
27 # A better (and easier) option is also to let the context constructor create the rootnode for you:
28 $xml = XML::TinyXML->new('rootnode', param => 'somevalue', attrs => { attr1 => v1, attr2 => v2 });
29  
30 # an empty root node can be created as well:
31 $xml = XML::TinyXML->new('rootnode');
32  
33 # You can later obtain a reference to the node object using the getNode() method
34 $node = $xml->getNode('/rootnode');
35  
36 # the leading '/' is optional ... since all paths will be considered absolute and
37 # first element is assumed to be always a root node
38 $node = $xml->getNode('rootnode');
39  
40 # xpath-like predicates are also supported so you can also do something like
41 $node->addChildNode('child', 'value1');
42 $node->addChildNode('child', 'value2');
43 $child2 = $xml->getNode('/rootnode/child[2]');
44  
45 # or even
46 $node->addChildNode('child', 'value3', { attr => 'val' });
47 # the 3rd 'child' has an attribute
48 $child3 = $xml->getNode('/rootnode/child[@attr="val"]');
49  
50 #### IMPORTANT NOTE: ####
51 # this is not xpath syntax. use XML::TinyXML::Selector::XPath
52 # if you want to use an xpath-compliant syntax
53  
54 # see XML::TinyXML::Node documentation for further details on possible
55 # operations on a node reference
56  
57 ######## #########
58 ########## hashref2xml and xml2hashref facilities ###########
59 ######## #########
60
61 # An useful facility is loading/dumping of hashrefs from/to xml
62 # for ex:
63 $hashref = { some => 'thing', someother => 'thing' };
64 my $xml = XML::TinyXML->new($hashref, param => 'mystruct');
65  
66 # or to load on an existing XML::TinyXML object
67 $xml->loadHash($hashref, 'mystruct');
68  
69 # we can also create and dump to string all at once :
70 my $xmlstring = XML::TinyXML->new($hashref, param => 'mystruct')->dump;
71  
72 # to reload the hashref back
73 my $hashref = $xml->toHash;
74  
75 =head1 DESCRIPTION
76  
77 Since in some environments it could be desirable to avoid installing
78 Expat, XmlParser and blahblahblah, needed by most XML-related perl modules,
79 my main scope was to obtain a fast xml library usable from perl
80 (so with the possibility to expose a powerful api) but without the need to install
81 a lot of other modules (or even C libraries) to have it working.
82  
83 The actual version of XML::TinyXML (0.18 when I'm writing this)
84 implements an xpath selector and supports encodings (through iconv).
85  
86 The OO Tree-based api allows to :
87  
88 - create a new document programmaticaly
89 - import an existing document from a file
90 - import an existing document by parsing a memory buffer
91 - modify the loaded/created document programmatically
92 - export the document to an xml file
93 - bidirectional conversion between xml documents and perl hashrefs
94  
95 There are other "no-dependencies" tiny xml implementations on CPAN.
96 Notably : XML::Easy and XML::Tiny but both are still missing some key
97 features which are actually implemented in this module and are required
98 by the projects where I use it, in particular :
99  
100 - an OO api to allow changing and re-exporting the xml document,
101 - an easy-to-use bidirectional xml<->hashref conversion
102 - encoding-conversion capabilities
103 - xpath selectors
104 - small memory footprint (well... it's always a perl module)
105  
106 The underlying xml implementation resides in txml.c and is imported in the perl
107 context through XS. It uses a linkedlist implementation took out of freebsd kernel
108 and is supposed to be fast enough to represent and access 'not-huge' xml trees.
109 If at some stage there will be need for more preformance in accessing the xml data
110 (especially when using the xpath selector) an hash-table based index could be built
111 on top of the tree structure to speed-up access to inner parts of the tree when using
112 paths.
113  
114 An Event-based api is actually missing, but will be provided in future releases.
115  
116 =head1 METHODS
117  
118 =over
119  
120 =cut
121  
122 package XML::TinyXML;
123  
124 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 10 require Exporter;
129 10 ▶use AutoLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 10 ▶use XML::TinyXML::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 10 our @ISA = qw(Exporter);
133  
134 # Items to export into callers namespace by default. Note: do not export
135 # names by default without a very good reason. Use EXPORT_OK instead.
136 # Do not simply export all your public functions/methods/constants.
137  
138 # This allows declaration use XML::TinyXML ':all';
139 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
140 # will save memory.
141 10 our %EXPORT_TAGS = ( 'all' => [ qw(
142 ) ] );
143  
144 10 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
145  
146 10 our @EXPORT = qw(
147 XML_BADARGS
148 XML_GENERIC_ERR
149 XML_LINKLIST_ERR
150 XML_MEMORY_ERR
151 XML_NOERR
152 XML_OPEN_FILE_ERR
153 XML_PARSER_GENERIC_ERR
154 XML_MROOT_ERR
155 XML_NODETYPE_SIMPLE
156 XML_NODETYPE_COMMENT
157 XML_NODETYPE_CDATA
158 XML_BAD_CHARS
159 XML_UPDATE_ERR
160 XXmlAddAttribute
161 XmlAddChildNode
162 XmlAddRootNode
163 XmlCountAttributes
164 XmlCountBranches
165 XmlCountChildren
166 XmlCreateContext
167 XmlDestroyContext
168 XmlResetContext
169 XmlCreateNode
170 XmlDestroyNode
171 XmlDump
172 XmlDumpBranch
173 XmlGetBranch
174 XmlGetChildNode
175 XmlGetChildNodeByName
176 XmlGetNode
177 XmlGetNodeValue
178 XmlNextSibling
179 XmlParseBuffer
180 XmlParseFile
181 XmlPrevSibling
182 XmlRemoveBranch
183 XmlRemoveChildNode
184 XmlRemoveChildNodeAtIndex
185 XmlRemoveNode
186 XmlSave
187 XmlSetNodeValue
188 XmlSetOutputEncoding
189 XmlSubstBranch
190 XmlCreateNamespace
191 XmlDestroyNamespace
192 XmlGetNamespaceByName
193 XmlGetNamespaceByUri
194 XmlAddNamespace
195 XmlGetNodeNamespace
196 XmlSetNodeNamespace
197 XmlSetNodeCNamespace
198 XmlSetCurrentNamespace
199 );
200  
201 10 our $VERSION = '0.34';
202  
203 17 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.1
204 # This AUTOLOAD is used to 'autoload' constants from the constant()
205 # XS function.
206  
207 17 my $constname;
208 17 our $AUTOLOAD;
209 17 ($constname = $AUTOLOAD) =~ s/.*:://;
210 17 ▶ croak "&XML::TinyXML::constant not defined" if $constname eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "constant" 0 17 17
211 17 my ($error, $val) = constant($constname);
212 0 ▶ if ($error) { croak $error; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 17 17
213 {
214 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
215 # Fixed between 5.005_53 and 5.005_61
216 #XXX if ($] >= 5.00561) {
217 #XXX *$AUTOLOAD = sub () { $val };
218 #XXX }
219 #XXX else {
220 17 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: called CC=1 SCAR=0.0
221 #XXX }
222 }
223 17 goto &$AUTOLOAD;
224 }
225  
226 10 require XSLoader;
227 10 XSLoader::load('XML::TinyXML', $VERSION);
228  
229 # Preloaded methods go here.
230  
231 =item * new ($arg, %params)
232  
233 Creates a new XML::TinyXML object.
234  
235 $root can be any of :
236  
237 XML::TinyXML::Node
238 XmlNodePtr
239 HASHREF
240 SCALAR
241  
242 and if present will be used as first root node of the newly created
243 xml document.
244  
245 %params is an optional hash parameter used only
246 if $arg is an HASHREF or a scalar
247  
248 %params = (
249 param => * if $root is an hashref, this will be
250 name of the root node (it will be passed
251 as second argument to loadHash())
252 * if $root is a scalar, this will be
253 the value of the root node.
254 attrs => attributes of the 'contextually added' $root node
255 encoding => output encoding to use (among iconv supported ones)
256 );
257  
258 =cut
259  
260 12 ▶sub new {
Subroutine new: called CC=12 SCAR=32.2
Pod documented
261 12 my ($class, $root, %params ) = @_;
262 12 my $self = {} ;
263 12 bless($self, $class);
264  
265 # default encoding (the same used as default in the C library
266 12 $self->{_encoding} = "utf-8";
267 12 $self->{_ctx} = XmlCreateContext();
268 12 ▶ $self->setOutputEncoding($params{encoding}) if ($params{encoding});
Branch: 1 branch
BranchTrueFalseTotal
if $params{"encoding"} 0 12 12
269 12 ▶ $self->allowMultipleRootNodes($params{multipleRootNodes}) if ($params{multipleRootNodes});
Branch: 1 branch
BranchTrueFalseTotal
if $params{"multipleRootNodes"} 0 12 12
270 12 ▶ $self->ignoreBlanks($params{ignoreBlanks}) if (defined($params{ignoreBlanks}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"ignoreBlanks"} 0 12 12
271 12 ▶ $self->ignoreWhiteSpaces($params{ignoreWhiteSpaces}) if (defined($params{ignoreWhiteSpaces}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"ignoreWhiteSpaces"} 0 12 12
272 12 ▶ if($root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root) 1 11 12
273 1 ▶ if(UNIVERSAL::isa($root, "XML::TinyXML::Node")) {
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($root, 'XML::TinyXML::Node')) { } 0 1 1
elsif (UNIVERSAL::isa($root, 'XmlNodePtr')) { } 0 1 1
elsif (ref $root eq 'HASH') { } 1 0 1
elsif (defined $root and !ref($root) || ref $root eq 'SCALAR') { } 0 0 0
274 0 XmlAddRootNode($self->{_ctx}, $root->{_node});
275 } elsif(UNIVERSAL::isa($root, "XmlNodePtr")) {
276 0 XmlAddRootNode($self->{_ctx}, $root);
277 } elsif(ref($root) eq "HASH") {
278 1 $self->loadHash($root, $params{param});
279 ▶ } elsif(defined($root) && (!ref($root) || ref($root) eq "SCALAR")) {
Condition0%0/6
defined $root and !ref($root) || ref $root eq "SCALAR"
!ll&&!rl&&r
000
not ref $root or ref $root eq "SCALAR"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $root and !ref($root) || ref $root eq "SCALAR"
defined $root not ref $root ref $root eq "SCALAR"
Truth table0%0/4
A and not(B) or C
A= defined $rootB= not ref $rootC= ref $root eq "SCALAR"
ABCresult
0XX0
1000
11X1
1011
280 0 $self->addRootNode($root, $params{param}, $params{attrs});
281 }
282 }
283
284 12 return $self;
285 }
286  
287 0 ▶sub open {
Subroutine open: not called CC=2 SCAR=17.9
Pod undocumented
288 0 my ($class, $file) = @_;
289 0 my $self = $class->new();
290 0 ▶ return $self->loadFile($file) == $self->XML_NOERR
Branch: 1 branch
BranchTrueFalseTotal
$self->loadFile($file) == $self->XML_NOERR ? : 0 0 0
291 ? $self
292 : undef;
293 }
294  
295 =item * addNodeAttribute ($node, $key, $value)
296  
297 Adds an attribute to a specific $node
298  
299 $node MUST be an XML::TinyXML::Node object.
300  
301 $key is the name of the attribute
302 $value is the value of the attribute
303  
304 This method is just an accessor. See XML::TinyXML::Node::addAttributes() instead.
305  
306 =cut
307  
308 0 ▶sub addNodeAttribute {
Subroutine addNodeAttribute: not called CC=3 SCAR=24.8
Pod documented
309 0 my ($self, $node, $key, $value) = @_;
310 0 ▶ return undef unless($node && UNIVERSAL::isa("XML::TinyXML::Node", $node));
Branch: 1 branch
BranchTrueFalseTotal
unless $node and UNIVERSAL::isa("XML::TinyXML::Node", $node) 0 0 0
Condition0%0/3
$node and UNIVERSAL::isa("XML::TinyXML::Node", $node)
!ll&&!rl&&r
000
MC/DC0%0/2
$node and UNIVERSAL::isa("XML::TinyXML::Node", $node)
$node UNIVERSAL::isa("XML::TinyXML::Node", $node)
Truth table0%0/3
A and B
A= $nodeB= UNIVERSAL::isa("XML::TinyXML::Node", $node)
ABresult
0X0
100
111
311 0 return $node->addAttributes($key => $value);
312 }
313  
314 =item * removeNodeAttribute ($node, $index)
315  
316 Removes from $node the attribute at $index if present.
317  
318 $node MUST be a XML::TinyXML::Node object.
319  
320 This method is just an accessor. See XML::TinyXML::Node::removeAttribute() instead.
321  
322 =cut
323  
324 0 ▶sub removeNodeAttribute {
Subroutine removeNodeAttribute: not called CC=3 SCAR=24.8
Pod documented
325 0 my ($self, $node, $index) = @_;
326 0 ▶ return undef unless($node && UNIVERSAL::isa("XML::TinyXML::Node", $node));
Branch: 1 branch
BranchTrueFalseTotal
unless $node and UNIVERSAL::isa("XML::TinyXML::Node", $node) 0 0 0
Condition0%0/3
$node and UNIVERSAL::isa("XML::TinyXML::Node", $node)
!ll&&!rl&&r
000
MC/DC0%0/2
$node and UNIVERSAL::isa("XML::TinyXML::Node", $node)
$node UNIVERSAL::isa("XML::TinyXML::Node", $node)
Truth table0%0/3
A and B
A= $nodeB= UNIVERSAL::isa("XML::TinyXML::Node", $node)
ABresult
0X0
100
111
327 0 return $node->removeAttribute($index);
328 }
329  
330 =item * addRootNode ($name, $val, $attrs)
331  
332 Adds a new root node.
333 (This can be considered both as a new tree in the forest represented in the xml document
334 or as a new branch in the xml tree represented by the document itself)
335  
336 =cut
337  
338 5 ▶sub addRootNode {
Subroutine addRootNode: called CC=5 SCAR=16.5
Pod documented
339 5 my ($self, $name, $val, $attrs) = @_;
340  
341 5 ▶ $val = ""
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 4 1 5
342 unless(defined($val));
343  
344 5 ▶ return $self->XML_BADARGS
Branch: 1 branch
BranchTrueFalseTotal
if $attrs and ref $attrs ne "HASH" 0 5 5
Condition66%2/3
$attrs and ref $attrs ne "HASH"
!ll&&!rl&&r
410
MC/DC0%0/2
$attrs and ref $attrs ne "HASH"
$attrs ref $attrs ne "HASH"
Truth table66%2/3
A and B
A= $attrsB= ref $attrs ne "HASH"
ABresult
0X0
100
111
345 if($attrs && ref($attrs) ne "HASH");
346  
347 5 my $node = XML::TinyXML::Node->new($name, $val, $attrs);
348  
349 5 ▶ return $self->XML_GENERIC_ERR
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 5 5
350 unless($node);
351  
352 5 return XmlAddRootNode($self->{_ctx}, $node->{_node});
353 }
354  
355 =item * addChildNode ($parent, $name, $val, $attrs)
356  
357 Adds a new child node.
358 This method is exactly like addRootNode but first argument must be a valid XML::TinyXML::Node
359 which will be the parent of the newly created node
360  
361 =cut
362  
363 0 ▶sub addChildNode {
Subroutine addChildNode: not called CC=7 SCAR=40.3
Pod documented
364 0 my ($self, $node, $name, $val, $attrs) = @_;
365  
366 0 ▶ return $self->XML_BADARGS
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node and UNIVERSAL::isa("XML::TinyXML::Node", $node) 0 0 0
Condition0%0/3
ref $node and UNIVERSAL::isa("XML::TinyXML::Node", $node)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $node and UNIVERSAL::isa("XML::TinyXML::Node", $node)
ref $node UNIVERSAL::isa("XML::TinyXML::Node", $node)
Truth table0%0/3
A and B
A= ref $nodeB= UNIVERSAL::isa("XML::TinyXML::Node", $node)
ABresult
0X0
100
111
367 unless (ref($node) && UNIVERSAL::isa("XML::TinyXML::Node", $node));
368  
369 0 ▶ return $self->XML_BADARGS
Branch: 1 branch
BranchTrueFalseTotal
if $attrs and ref $attrs ne "HASH" 0 0 0
Condition0%0/3
$attrs and ref $attrs ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$attrs and ref $attrs ne "HASH"
$attrs ref $attrs ne "HASH"
Truth table0%0/3
A and B
A= $attrsB= ref $attrs ne "HASH"
ABresult
0X0
100
111
370 if($attrs && ref($attrs) ne "HASH");
371  
372 0 ▶ $val = ""
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
373 unless(defined($val));
374  
375 0 my $child = XML::TinyXML::Node->new($name, $val, $attrs, $node);
376  
377 0 ▶ return $self->XML_GENERIC_ERR
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 0 0
378 unless($child);
379  
380 0 return XmlAddChildNode($self->{_ctx}, $node->{_node});
381 }
382  
383 =item * dump ()
384  
385 Returns a stringified version of the XML structure represented internally
386  
387 =cut
388  
389 3 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
390 3 my $self = shift;
391 3 return XmlDump($self->{_ctx});
392 }
393  
394 =item * loadFile ($path)
395  
396 Load the xml structure from a file
397  
398 =cut
399  
400 10 ▶sub loadFile {
Subroutine loadFile: called CC=1 SCAR=0.0
Pod documented
401 10 my ($self, $path) = @_;
402 10 return XmlParseFile($self->{_ctx}, $path);
403 }
404  
405 =item * loadHash ($hash, $root)
406  
407 Load the xml structure from an hashref (AKA: convert an hashref to an xml document)
408  
409 if $root is specified, it will be the entity name of the root node
410 in the resulting xml document.
411  
412 =cut
413  
414 1 ▶sub loadHash {
Subroutine loadHash: called CC=5 SCAR=18.7
Pod documented
415 1 my ($self, $hash, $root) = @_;
416 1 ▶ $root = "txml"
Branch: 1 branch
BranchTrueFalseTotal
unless $root 1 0 1
417 unless($root);
418  
419 1 my $cur = undef;
420 1 ▶ if(ref($root) && UNIVERSAL::isa("XML::TinyXML::Node", $root)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $root and UNIVERSAL::isa('XML::TinyXML::Node', $root)) { } 0 1 1
Condition33%1/3
ref $root and UNIVERSAL::isa("XML::TinyXML::Node", $root)
!ll&&!rl&&r
100
MC/DC0%0/2
ref $root and UNIVERSAL::isa("XML::TinyXML::Node", $root)
ref $root UNIVERSAL::isa("XML::TinyXML::Node", $root)
Truth table33%1/3
A and B
A= ref $rootB= UNIVERSAL::isa("XML::TinyXML::Node", $root)
ABresult
0X0
100
111
421 0 XmlAddRootNode($self->{_ctx}, $root->{_node});
422 0 $cur = $root;
423 } else {
424 1 $self->addRootNode($root);
425 1 ▶ $cur = $self->allowMultipleRootNodes
Branch: 1 branch
BranchTrueFalseTotal
$self->allowMultipleRootNodes ? : 0 1 1
426 ? $self->getNode($root)
427 : $self->getRootNode(0);
428 }
429 1 return $cur->loadHash($hash);
430 }
431  
432 =item * toHash ()
433  
434 Dump the xml structure represented internally in the form of an hashref
435  
436 =cut
437  
438 1 ▶sub toHash {
Subroutine toHash: called CC=1 SCAR=0.0
Pod documented
439 1 my ($self) = shift;
440 # only first branch will be parsed ... This means that if multiple root
441 # nodes are present (which is anyway not allowed by the xml spec), only
442 # the first one will be parsed and translated into an hashref
443 1 my $node = $self->getRootNode(0);
444 1 return $node->toHash;
445 }
446  
447 =item * loadBuffer ($buf)
448  
449 Load the xml structure from a preloaded memory buffer
450  
451 =cut
452  
453 1 ▶sub loadBuffer {
Subroutine loadBuffer: called CC=1 SCAR=0.0
Pod documented
454 1 my ($self, $buf) = @_;
455 1 return XmlParseBuffer($self->{_ctx}, $buf);
456 }
457  
458 =item * getNode ($path)
459  
460 Get a node at a specific path.
461  
462 $path must be of the form: '/rootnode/child1/child2/leafnod'
463 and the leading '/' is optional (since all paths will be interpreted
464 as absolute)
465  
466 Returns an XML::TinyXML::Node object
467  
468 =cut
469  
470 18 ▶sub getNode {
Subroutine getNode: called CC=1 SCAR=0.0
Pod documented
471 18 my ($self, $path) = @_;
472 18 return XML::TinyXML::Node->new(XmlGetNode($self->{_ctx}, $path));
473 }
474  
475 =item * getChildNode ($node, $index)
476  
477 Get the child of $node at index $index.
478  
479 Returns an XML::TinyXML::Node object
480  
481 =cut
482  
483 0 ▶sub getChildNode {
Subroutine getChildNode: not called CC=1 SCAR=6.9
Pod documented
484 0 my ($self, $node, $index) = @_;
485 0 return XML::TinyXML::Node->new(XmlGetChildNode($node, $index));
486 }
487  
488 =item * countChildren ()
489  
490 Returns the number of $node's children
491  
492 =cut
493  
494 3 ▶sub countChildren {
Subroutine countChildren: called CC=4 SCAR=14.0
Pod documented
495 3 my ($self, $node) = @_;
496 3 ▶ if(UNIVERSAL::isa($node, "XML::TinyXML::Node")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($node, 'XML::TinyXML::Node')) { } 1 2 3
elsif (UNIVERSAL::isa($node, 'XmlNodePtr')) { } 1 1 2
497 1 return XmlCountChildren($node->{_node});
498 } elsif(UNIVERSAL::isa($node, "XmlNodePtr")) {
499 1 return XmlCountChildren($node);
500 } else {
501 # assume we have been provided a path
502 1 my $node = XmlGetNode($self->{_ctx}, $node);
503 1 ▶ return XmlCountChildren($node)
Branch: 1 branch
BranchTrueFalseTotal
if $node 1 0 1
504 if ($node);
505 }
506 0 return undef;
507 }
508  
509 =item * removeNode ($path)
510  
511 Remove the node at specific $path, if present.
512 See getNode() documentation for some notes on the $path format.
513  
514 Returns XML_NOERR (0) if success, error code otherwise.
515  
516 See Exportable constants for a list of possible error codes
517  
518 =cut
519  
520 0 ▶sub removeNode {
Subroutine removeNode: not called CC=1 SCAR=6.9
Pod documented
521 0 my ($self, $path) = @_;
522 0 return XmlRemoveNode($self->{_ctx}, $path);
523 }
524  
525 =item * getBranch ($index)
526  
527 alias for getRootNode
528  
529 =cut
530  
531 3 ▶sub getBranch {
Subroutine getBranch: called CC=1 SCAR=0.0
Pod documented
532 3 my ($self, $index) = @_;
533 3 return XML::TinyXML::Node->new(XmlGetBranch($self->{_ctx}, $index));
534 }
535  
536 =item * getRootNode ($index)
537  
538 Get the root node at $index.
539  
540 Returns an XML::TinyXML::Node object if present, undef otherwise
541  
542 =cut
543  
544 3 ▶sub getRootNode {
Subroutine getRootNode: called CC=1 SCAR=0.0
Pod documented
545 3 my ($self, $index) = @_;
546 3 return $self->getBranch($index);
547 }
548  
549 =item * removeBranch ($index)
550  
551 Alias for removeRootNode
552  
553 =cut
554  
555 1 ▶sub removeBranch {
Subroutine removeBranch: called CC=1 SCAR=0.0
Pod documented
556 1 my ($self, $index) = @_;
557 1 return XmlRemoveBranch($self->{_ctx}, $index);
558 }
559  
560 =item * removeRootNode ($index)
561  
562 Remove the rootnode (and all his children) at $index.
563  
564 =cut
565  
566 1 ▶sub removeRootNode {
Subroutine removeRootNode: called CC=1 SCAR=0.0
Pod documented
567 1 my ($self, $index) = @_;
568 1 return $self->removeBranch($index);
569 }
570  
571 =item * countRootNodes
572  
573 Returns the number of root nodes within this context
574  
575 =cut
576  
577 4 ▶sub countRootNodes {
Subroutine countRootNodes: called CC=1 SCAR=0.0
Pod documented
578 4 my ($self) = @_;
579 4 return XmlCountBranches($self->{_ctx});
580 }
581  
582 =item * rootNodes ()
583  
584 Returns an array containing all rootnodes.
585 In scalar context returns an arrayref.
586  
587 =cut
588  
589 124 ▶sub rootNodes {
Subroutine rootNodes: called CC=3 SCAR=11.0
Pod documented
590 124 my ($self) = @_;
591 124 my @nodes;
592 124 ▶ for (my $i = 0; $i < XmlCountBranches($self->{_ctx}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < XmlCountBranches($self->{"_ctx"})) 125 124 249
593 125 push (@nodes, XML::TinyXML::Node->new(XmlGetBranch($self->{_ctx}, $i)));
594 }
595 124 ▶ return wantarray?@nodes:\@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 123 1 124
596 }
597  
598 =item * getChildNodeByName ($node, $name)
599  
600 Get the child of $node with name == $name.
601  
602 Returns an XML::TinyXML::Node object if there is such a child, undef otherwise
603  
604 =cut
605  
606 0 ▶sub getChildNodeByName {
Subroutine getChildNodeByName: not called CC=4 SCAR=30.0
Pod documented
607 0 my ($self, $node, $name) = @_;
608 0 ▶ if ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 0 0 0
609 0 return XML::TinyXML::Node->new(XmlGetChildNodeByName($node, $name));
610 } else {
611 0 my $count = XmlCountBranches($self->{_ctx});
612 0 ▶ for (my $i = 0 ; $i < $count; $i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 0 0 0
613 0 my $res = XmlGetChildNodeByName(XmlGetBranch($self->{_ctx}, $i), $name);
614 0 ▶ return XML::TinyXML::Node->new($res) if($res);
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
615  
616 }
617 }
618 0 return undef;
619 }
620  
621 0 ▶sub cNode {
Subroutine cNode: not called CC=2 SCAR=17.9
Pod undocumented
622 0 my ($self, $value) = @_;
623 return $value
624 ? $self->{_ctx}->cNode($value)
625 0 ▶ : $self->{_ctx}->cNode;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
626 }
627  
628 =item * save ($path)
629  
630 Save the xml document represented internally into $path.
631  
632 Returns XML_NOERR if success, a specific error code otherwise
633  
634 =cut
635  
636 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
637 0 my ($self, $path) = @_;
638 0 return XmlSave($self->{_ctx}, $path);
639 }
640  
641 =item * setOutputEncoding ($encoding)
642  
643 Sets the output encding to the specified one
644  
645 (any iconv-supported encoding is a valid argument for this method)
646  
647 Returns XML_NOERR if success, a specific error code otherwise
648  
649 =cut
650  
651 0 ▶sub setOutputEncoding {
Subroutine setOutputEncoding: not called CC=1 SCAR=6.9
Pod documented
652 0 my ($self, $encoding) = @_;
653 0 $self->{_encoding} = $encoding;
654 0 XmlSetOutputEncoding($self->{_ctx}, $encoding);
655 }
656  
657 =item * allowMultipleRootNodes ($bool)
658  
659 Allow to control if the document can contain multiple root nodes (out of xml spec)
660 or if it will support only one single root node. Default is 0
661  
662 =cut
663  
664 169 ▶sub allowMultipleRootNodes {
Subroutine allowMultipleRootNodes: called CC=2 SCAR=6.9
Pod documented
665 169 my ($self, $val) = @_;
666 return defined($val)
667 ? $self->{_ctx}->allowMultipleRootNodes($val)
668 169 ▶ : $self->{_ctx}->allowMultipleRootNodes;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 168 1 169
669 }
670  
671 =item * ignoreBlanks ($bool)
672  
673 Controls the behaviour of both the parser and the dumper.
674  
675 - Parser :
676  
677 If ignoreBlanks is true any 'tab', 'newline' and 'carriage-return' ("\t", "\n", "\r"),
678 between two tags will be ignored, unless surrounded by non-whitespace character.
679 For example, considering the following node:
680  
681 <parent>
682 <child>value</child>
683 </parent>
684  
685 ( literally : "<parent>\n\t<child>value</child>\n</parent>" )
686  
687 the parser will ignore the newlines and the tab between two nodes.
688 So, the resulting structure would be :
689  
690 $node = {
691 value => undef,
692 children => [ { child => "value" } ]
693 }
694  
695 If ignoreBlanks is false, all characters between the node tags will be part of the
696 value and the result would be :
697  
698 $node = {
699 value => "\n\t\n",
700 children => [ { child => "value" } ]
701 }
702  
703 - Dumper :
704  
705 If ignoreBlanks is true, both newlines and tabs will be used to prettify text formatting,
706 so the node in the previous example will be printed out as:
707  
708 <parent>
709 <child>value</child>
710 </parent>
711  
712 If ignoreBlanks is false, no extra characters will be added to the xml data
713 (so no newlines, indentation or such).
714 The previous example would be dumped as follows:
715  
716 <parent><child>value</child></parent>
717  
718 Default is 1.
719  
720 =cut
721  
722 2 ▶sub ignoreBlanks {
Subroutine ignoreBlanks: called CC=2 SCAR=7.2
Pod documented
723 2 my ($self, $val) = @_;
724 return defined($val)
725 ? $self->{_ctx}->ignoreBlanks($val)
726 2 ▶ : $self->{_ctx}->ignoreBlanks;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 2 0 2
727 }
728  
729 =item * ignoreWhiteSpaces ($bool)
730  
731 Controls the behaviour of the parser.
732  
733 This flag includes the literal whitespace ' ' among the ignored characters
734 commended by ignoreBlanks()
735  
736 consider the following examples:
737  
738 <child> </child>
739  
740 - if true:
741 value = ""
742  
743 - if false:
744 value = " "
745  
746 The parser will ignore the whitespace if this flag is true.
747  
748 <child> a value </child>
749  
750 <child> a value</child>
751  
752 <child>a value </child>
753  
754 - if true:
755 value = "a value"
756  
757 - if false:
758 value = " a value "
759 " a value"
760 "a value "
761  
762 The whitespace will be part of the value if this flag is false.
763  
764 The dumper is not affected by this flag.
765  
766 Default is 1
767  
768 =cut
769  
770 2 ▶sub ignoreWhiteSpaces {
Subroutine ignoreWhiteSpaces: called CC=2 SCAR=7.2
Pod documented
771 2 my ($self, $val) = @_;
772 return defined($val)
773 ? $self->{_ctx}->ignoreWhiteSpaces($val)
774 2 ▶ : $self->{_ctx}->ignoreWhiteSpaces;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 2 0 2
775 }
776  
777 1 ▶sub hasIconv {
Subroutine hasIconv: called CC=1 SCAR=0.0
Pod undocumented
778 1 my $self = shift;
779 1 return $self->{_ctx}->hasIconv;
780 }
781  
782 11 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
783 11 my $self = shift;
784 XmlDestroyContext($self->{_ctx})
785 11 ▶ if($self->{_ctx});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ctx"} 11 0 11
786 }
787  
788 # Autoload methods go after =cut, and are processed by the autosplit program.
789  
790 10 1;
791 __END__
« blib/lib/XML/TinyXML/Selector/XPath.pm ↑ Summary blib/lib/XML/TinyXML/Selector/XPath/Functions.pm »