private static Result<List<Element>> readDocument(String rootElementName, String stringDoc) { final SAXBuilder builder = new SAXBuilder(); try { final Document document = builder.build(new StringReader(stringDoc)); final Element rootElement = document.getRootElement(); return Result.success(List.fromCollection(rootElement.getChildren(rootElementName))); } catch (IOException | JDOMException io) { return Result.failure( String.format( "Invalid root element name '%s' or XML data %s", rootElementName, stringDoc), io); } catch (Exception e) { return Result.failure( String.format("Unexpected error while reading XML data %s", stringDoc), e); } }
private static String processElement(Element element, Tuple<String, List<String>> format) { String formatString = format._1; List<String> parameters = format._2.map(element::getChildText); return String.format(formatString, parameters.toJavaList().toArray()); }
private static List<String> toStringList(List<Element> list, Tuple<String, List<String>> format) { return list.map(e -> processElement(e, format)); }