Commit da217ba5ee548bac04cffff3884f88e829fd2072
1 parent
bec3ff2c
element(std:string) method no longer throws an exception if the element does not…
… exist. Element existence is verified by a NULL pointer.
Showing
2 changed files
with
17 additions
and
1 deletions
src/xml/GXml.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GXml.cpp - XML class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2010-2015 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * | ... | ... |
src/xml/GXmlNode.cpp
| ... | ... | @@ -564,12 +564,20 @@ GXmlElement* GXmlNode::element(const std::string& name) |
| 564 | 564 | std::string tag = gammalib::strip_whitespace(tags[i]); |
| 565 | 565 | int index = extract_index(tag); |
| 566 | 566 | |
| 567 | + // Break if the requested node does not exist | |
| 568 | + int n = current->elements(tag); | |
| 569 | + if (n < 1 || index < 0 || index >= n) { | |
| 570 | + element = NULL; | |
| 571 | + break; | |
| 572 | + } | |
| 573 | + | |
| 567 | 574 | // Get node |
| 568 | 575 | element = current->element(tag, index); |
| 569 | 576 | current = element; |
| 570 | 577 | |
| 571 | 578 | // Break if node has not been found |
| 572 | 579 | if (current == NULL) { |
| 580 | + element = NULL; | |
| 573 | 581 | break; |
| 574 | 582 | } |
| 575 | 583 | |
| ... | ... | @@ -618,12 +626,20 @@ const GXmlElement* GXmlNode::element(const std::string& name) const |
| 618 | 626 | std::string tag = gammalib::strip_whitespace(tags[i]); |
| 619 | 627 | int index = extract_index(tag); |
| 620 | 628 | |
| 629 | + // Break if the requested node does not exist | |
| 630 | + int n = current->elements(tag); | |
| 631 | + if (n < 1 || index < 0 || index >= n) { | |
| 632 | + element = NULL; | |
| 633 | + break; | |
| 634 | + } | |
| 635 | + | |
| 621 | 636 | // Get node |
| 622 | 637 | element = current->element(tag, index); |
| 623 | 638 | current = element; |
| 624 | 639 | |
| 625 | 640 | // Break if node has not been found |
| 626 | 641 | if (current == NULL) { |
| 642 | + element = NULL; | |
| 627 | 643 | break; |
| 628 | 644 | } |
| 629 | 645 | ... | ... |