| 359 | | then the function will croak with errors. |
|---|
| | 359 | then the function will issue a warning and return undef. |
|---|
| | 360 | |
|---|
| | 361 | If the variable found is an element that contains child elements, then |
|---|
| | 362 | a corresponding hashtable will be returned. For example, if we perform |
|---|
| | 363 | a getVar(name => "foo") on the following XML: |
|---|
| | 364 | |
|---|
| | 365 | <HoneyClient> |
|---|
| | 366 | <foo> |
|---|
| | 367 | <bar>123</bar> |
|---|
| | 368 | <bar>456</bar> |
|---|
| | 369 | <yok>789</yok> |
|---|
| | 370 | <yok>xxx</yok> |
|---|
| | 371 | </foo> |
|---|
| | 372 | </HoneyClient> |
|---|
| | 373 | |
|---|
| | 374 | Then the following $hashref will be returned: |
|---|
| | 375 | |
|---|
| | 376 | $hashref = { |
|---|
| | 377 | 'bar' => [ '123', '456' ], |
|---|
| | 378 | 'yok' => [ '789', 'xxx' ], |
|---|
| | 379 | } |
|---|
| 369 | | I<Output>: The variable's element/attribute value, if found; warns and |
|---|
| 370 | | returns undef otherwise. |
|---|
| | 389 | I<Output>: The variable's element/attribute value or hashtable (for |
|---|
| | 390 | multi-value elements), if found; warns and returns undef otherwise. |
|---|
| | 391 | |
|---|
| | 392 | B<Note>: If the target variable to return is an element that contains |
|---|
| | 393 | B<combinations> of text and sub-elements, then only the text within |
|---|
| | 394 | the sub-elements will be returned in the previously mentioned |
|---|
| | 395 | $hashref format. |
|---|
| | 396 | |
|---|
| | 397 | For example, if we perform a getVar(name => "foo") on the following XML: |
|---|
| | 398 | |
|---|
| | 399 | <HoneyClient> |
|---|
| | 400 | <foo> |
|---|
| | 401 | THIS_TEXT_WILL_BE_LOST |
|---|
| | 402 | <bar>123</bar> |
|---|
| | 403 | <bar>456</bar> |
|---|
| | 404 | <yok>789</yok> |
|---|
| | 405 | <yok>xxx</yok> |
|---|
| | 406 | <yok><CHILD>zzz</CHILD></yok> |
|---|
| | 407 | </foo> |
|---|
| | 408 | </HoneyClient> |
|---|
| | 409 | |
|---|
| | 410 | Then the following $hashref will be returned: |
|---|
| | 411 | |
|---|
| | 412 | $hashref = { |
|---|
| | 413 | 'bar' => [ '123', '456' ], |
|---|
| | 414 | 'yok' => [ '789', 'xxx', 'zzz' ], |
|---|
| | 415 | } |
|---|
| | 416 | |
|---|
| | 417 | Notice how the B<THIS_TEXT_WILL_BE_LOST> string got dropped and that |
|---|
| | 418 | the B<E<lt>CHILDE<gt>> tags were silently stripped from the B<zzz> |
|---|
| | 419 | string. In other words, in each target element, B<don't mix text |
|---|
| | 420 | with sub-elements> and B<don't nest sub-elements> if you want the |
|---|
| | 421 | nested structure preserved when a getVar() is called on the |
|---|
| | 422 | B<grandparent element>. |
|---|
| 442 | | my $val = $nodeset->pop->string_value; |
|---|
| 443 | | |
|---|
| 444 | | # Trail leading and trailing whitespace |
|---|
| 445 | | $val =~ s/^\s+|\s+$//g; |
|---|
| 446 | | |
|---|
| 447 | | # For some reason attributes return attribute_name="value" |
|---|
| 448 | | $val =~ s/.*"(.*)".*/$1/; |
|---|
| | 494 | |
|---|
| | 495 | # Figure out if the (most specific) node has any children. |
|---|
| | 496 | my $parent = $nodeset->pop(); |
|---|
| | 497 | $nodeset = $xp->findnodes("*", $parent); |
|---|
| | 498 | my $val = undef; |
|---|
| | 499 | if ($nodeset->size() <= 0) { |
|---|
| | 500 | # There are no child elements, thus stingify |
|---|
| | 501 | # all textual components. |
|---|
| | 502 | |
|---|
| | 503 | $val = $parent->string_value(); |
|---|
| | 504 | |
|---|
| | 505 | # Trail leading and trailing whitespace |
|---|
| | 506 | $val =~ s/^\s+|\s+$//g; |
|---|
| | 507 | } else { |
|---|
| | 508 | |
|---|
| | 509 | # There are child elements; return a |
|---|
| | 510 | # hashtable accordingly. |
|---|
| | 511 | my @children = $nodeset->get_nodelist(); |
|---|
| | 512 | |
|---|
| | 513 | # Now, build the hashtable of array references. |
|---|
| | 514 | $val = {}; |
|---|
| | 515 | foreach my $child (@children) { |
|---|
| | 516 | push (@{$val->{$child->getName()}}, $child->string_value()); |
|---|
| | 517 | } |
|---|
| | 518 | } |
|---|