1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
# all
*INITIALIZATION METHODS
/new
+args element str, data ANY
+returns XML::NestArray node
-$node = narr_new();
-$node = XML::NestArray->new;
-$node = XML::NestArray->new(person => [[name=>$n], [phone=>$p]]);
creates a new instance of a XML::NestArray node
/nodify
+args data array-reference
+returns XML::NestArray node
-$node = narr_nodify([person => [[name=>$n], [phone=>$p]]]);
turns a perl array reference into a XML::NestArray node.
similar to B<new>
/parse
+args file str, [format str], [handler obj]
+returns XML::NestArray node
-$node = narr_parse($fn);
-$node = XML::NestArray->parse(-file=>$fn, -handler=>$myhandler);
slurps a file or string into a XML::NestArray node structure. Will
guess the format from the suffix if it is not given.
The format can also be the name of a parsing module, or an actual
parser object
/from
+args format str, source str
+returns XML::NestArray node
-$node = narr_from('xml', $fn);
-$node = narr_from('xmlstr', q[<top><x>1</x></top>]);
-$node = XML::NestArray->from($parser, $fn);
Similar to B<parse>
slurps a file or string into a XML::NestArray node structure.
The format can also be the name of a parsing module, or an actual
parser object
/unflatten
+args data array
+returns XML::NestArray node
-$node = narr_unflatten(person=>[name=>$n, phone=>$p, address=>[street=>$s, city=>$c]]);
Creates a node structure from a semi-flattened representation, in
which children of a node are represented as a flat list of data rather
than a list of array references.
This means a structure can be specified as:
person=>[name=>$n,
phone=>$p,
address=>[street=>$s,
city=>$c]]
Instead of:
[person=>[ [name=>$n],
[phone=>$p],
[address=>[ [street=>$s],
[city=>$c] ] ]
]
]
The former gets converted into the latter for the internal representation
# recursive
* RECURSIVE SEARCHING
/findnode fn
+args element str
+returns node[]
-@persons = narr_findnode($struct, 'person');
-@persons = $struct->findnode('person');
recursively searches tree for all elements of the given type, and
returns all nodes found.
/findval fv
+args element str
+returns ANY[] or ANY
-@names = narr_findval($struct, 'name');
-@names = $struct->findval('name');
recursively searches tree for all elements of the given type, and
returns all data values found. the data values could be primitive
scalars or nodes.
/sfindval sfv
+args element str
+returns ANY
-$name = narr_sfindval($struct, 'name');
-$name = $struct->sfindval('name');
as findval, but returns the first value found
/findvallist fvl
+args element str[]
+returns ANY[]
-($name, $phone) = narr_findvallist($personstruct, 'name', 'phone');
-($name, $phone) = $personstruct->findvallist('name', 'phone');
recursively searches tree for all elements in the list
# nonrecursive
*DATA ACCESSOR METHODS
these allow getting and setting of elements directly underneath the
current one
/get g
+args element str
+return ANY[] or ANY
-$name = $person->get('name');
-@phone_nos = $person->get('phone_no');
gets the data value of an element for any node
the examples above would work on a data structure like this:
[person => [ [name => 'fred'],
[phone_no => '1-800-111-2222'],
[phone_no => '1-415-555-5555']]]
will return an array or single value depending on the context
/sget sg
+args element str
+return ANY
-$name = $person->get('name');
-$phone = $person->get('phone_no');
as B<get> but always returns a single value
/gl getl getlist
+args element str[]
+return ANY[]
-($name, @phone) = $person->get('name', 'phone_no');
returns the data values for a list of sub-elements of a node
/getn gn getnode
+args element str
+return node[] or node
-$namestruct = $person->getn('name');
-@pstructs = $person->getn('phone_no');
as B<get> but returns the whole node rather than just the data valie
/sgetn sgn sgetnode
+args element str
+return node
-$pstruct = $person->sgetn('phone_no');
as B<getnode> but always returns a scalar
/set s
+args element str, datavalue ANY
+return ANY
-$person->set('name', 'fred');
-$person->set('phone_no', $cellphone, $homephone);
sets the data value of an element for any node. if the element is
multivalued, all the old values will be replaced with the new ones
specified.
ordering will be preserved, unless the element specified does not
exist, in which case, the new tag/value pair will be placed at the
end.
/unset u
+args element str, datavalue ANY
+return ANY
-$person->unset('name');
-$person->unset('phone_no');
prunes all nodes of the specified element from the current node
/add a
+args element str, datavalue ANY[]
+return ANY
-$person->add('phone_no', $cellphone, $homephone);
adds a datavalue or list of datavalues. appends if already existing,
creates new element value pairs if not already existing.
/element e name
+args
+return element str
-$element = $struct->element
returns the element name of the current node
/kids k children
+args
+return ANY or ANY[]
-@nodes = $person->kids
-$name = $namestruct->kids
returns the data value(s) of the current node; if it is a terminal
node, returns a single value which is the data. if it is non-terminal,
returns an array of nodes
/addkid ak addchild
+args kid node
+return ANY
-$person->addkid('job', $job);
adds a new child node to a non-terminal node, after all the existing child nodes
# querying
*QUERYING AND ADVANCED DATA MANIPULATION
/njoin nj j
+args element str
+return undef
does a relational style natural join - see previous example in this doc
/normalise norm
+args
+return node or node[]
normalises denormalised tables/rows
/qmatch qm
+args return-element str, match-element str, match-value str
+return node[]
-@persons = $s->qmatch('name', 'fred');
queries the node tree for all elements that satisfy the specified key=val match
/tmatch tm
+args element str, value str
+return bool
-@persons = grep {$_->tmatch('name', 'fred')} @persons
returns true if the the value of the specified element matches
/tmatchhash tmh
+args match hashref
+return bool
-@persons = grep {$_->tmatchhash({name=>'fred', hair_colour=>'green'})} @persons
returns true if the node matches a set of constraints, specified as hash
/tmatchnode tmn
+args match node
+return bool
-@persons = grep {$_->tmatchhash([person=>[[name=>'fred'], [hair_colour=>'green']]])} @persons
returns true if the node matches a set of constraints, specified as node
/cmatch cm
+args element str, value str
+return bool
-$n_freds = $personset->cmatch('name', 'fred');
counts the number of matches
/where w
+args element str, test CODE
+return Node[]
-@rich_persons = $data->where('person', sub {shift->get_salary > 100000});
the tree is queried for all elements of the specified type that
satisfy the coderef (must return a boolean)
my @rich_dog_or_cat_owners =
$data->where('person',
sub {my $p = shift;
$p->get_salary > 100000 &&
$p->where('pet',
sub {shift->get_type =~ /(dog|cat)/})});
/iterate i
+args code CODE
+return
-my @nodes=(); $data->iterate(sub {push(@nodes, shift->name}));
iterates through tree depth first, executing code
# experimental
#run
#collapse
#merge
#misc
*MISCELLANEOUS METHODS
/duplicate d
+args
+return Node
-$node2 = $node->duplicate;
/isanode
+args
+return bool
-if (narr_isanode($node)) { ... }
really only useful in non OO mode...
/hash
+args
+return hash
-$h = $node->hash;
turns a tree into a hash. all data values will be arrayrefs
/pairs
turns a tree into a hash. all data values will be scalar (IMPORTANT:
this means duplicate values will be lost)
*EXPORT
/write
+args filename str, format str[optional]
+return
-$node->write("myfile.xml");
-$node->write("myfile", "itext");
will try and guess the format from the extension if not specified
/xml
+args filename str, format str[optional]
+return
-$node->write("myfile.xml");
-$node->write("myfile", "itext");
# xml
+args
+return xml str
-print $node->xml;
*XML METHODS
/sax
+args saxhandler SAX-CLASS
+return
-$node->sax($mysaxhandler);
turns a tree into a series of SAX events
/xpath xp tree2xpath
+args
+return xpath object
-$xp = $node->xpath; $q = $xp->find($xpathquerystr);
/xpquery xpq xpathquery
+args xpathquery str
+return Node[]
-@nodes = $node->xqp($xpathquerystr);
# PROC ONLY: narr_load
|