- public Optional<XmlElement> getOnlyChildElementWithSameNamespaceOptionally(String childName) {
- try {
- return Optional.of(getOnlyChildElement(childName, getNamespace()));
- } catch (Exception e) {
- return Optional.absent();
+ public Optional<XmlElement> getOnlyChildElementWithSameNamespaceOptionally(final String childName) {
+ Optional<String> namespace = getNamespaceOptionally();
+ if (namespace.isPresent()) {
+ List<XmlElement> children = getChildElementsWithinNamespace(namespace.get());
+ children = Lists.newArrayList(Collections2.filter(children, new Predicate<XmlElement>() {
+ @Override
+ public boolean apply(XmlElement xmlElement) {
+ return xmlElement.getName().equals(childName);
+ }
+ }));
+ if (children.size() != 1){
+ return Optional.absent();
+ }
+ return Optional.of(children.get(0));