Source:
<code>String getAllReviewsAsXml(List<Map<String, ?>> reviewLetterItems, String articleReceivedDate) throws IOException { List<String> reviewLetters = new ArrayList<>(); String acceptanceLetter = ""; for (Map<String, ?> reviewLetterItem : reviewLetterItems) { String xml = getContentXml(reviewLetterItem, "letter"); xml = xml.replaceAll("<\\?xml(.+?)\\?>", ""); Document parsedXml = Jsoup.parse(xml, "", Parser.xmlParser()); <START> parsedXml.outputSettings().escapeMode(Entities.EscapeMode.xhtml); <END> parsedXml.outputSettings().charset(CharEncoding.UTF_8); Elements reviewDates = parsedXml.select("named-content[content-type~=(letter-date|author-response-date)]"); for (Element reviewDate : reviewDates) { reviewDate.text(formatDate(reviewDate.text())); } Elements subArticle = parsedXml.select("sub-article"); if(!subArticle.isEmpty() && subArticle.attr("specific-use").equals("acceptance-letter")){ acceptanceLetter = parsedXml.toString(); continue; } reviewLetters.add(parsedXml.toString()); } Map<Integer, List<String>> lettersByRevision = new TreeMap<>(); for (String letter : reviewLetters) { String revisionAsString = XmlUtil.extractText(XmlUtil.extractElement(letter, "meta-value")); Integer revisionNumber = new Integer(revisionAsString); if (lettersByRevision.get(revisionNumber) == null) { lettersByRevision.put(revisionNumber, new ArrayList<>()); } lettersByRevision.get(revisionNumber).add(letter); } String peerReviewContent = ""; for (List<String> revisionLetters : lettersByRevision.values()) { peerReviewContent += "<revision>" + String.join("", revisionLetters) + "</revision>"; } peerReviewContent += acceptanceLetter; peerReviewContent += "<article-received-date>" + articleReceivedDate + "</article-received-date>"; peerReviewContent = "<peer-review>" + peerReviewContent + "</peer-review>"; return peerReviewContent; }</code><technical_language>Feels odd html parsing library xml, api is simple</technical_language>

Target:
String getAllReviewsAsXml(List<Map<String, ?>> reviewLetterItems, String articleReceivedDate) throws IOException { List<String> reviewLetters = new ArrayList<>(); String acceptanceLetter = ""; for (Map<String, ?> reviewLetterItem : reviewLetterItems) { String xml = getContentXml(reviewLetterItem, "letter"); Document doc = XmlUtil.createXmlDocument(new InputSource(new StringReader(xml))); String dateExpression = " NodeList dateNodes = XmlUtil.getNodes(doc, dateExpression); int length = dateNodes.getLength(); for (int i = 0; i < length; i++) { Node node = dateNodes.item(i); node.setTextContent(formatDate(node.getTextContent())); } xml = XmlUtil.createXmlString(doc); String acceptLetterExpr = "sub-article[@specific-use='acceptance-letter']"; NodeList acceptLetterNodes = XmlUtil.getNodes(doc, acceptLetterExpr); if (acceptLetterNodes.getLength() > 0) { acceptanceLetter = xml; } else { reviewLetters.add(xml); } } Map<Integer, List<String>> lettersByRevision = new TreeMap<>(); for (String letter : reviewLetters) { String revisionAsString = XmlUtil.extractText(XmlUtil.extractElement(letter, "meta-value")); Integer revisionNumber = new Integer(revisionAsString); if (lettersByRevision.get(revisionNumber) == null) { lettersByRevision.put(revisionNumber, new ArrayList<>()); } lettersByRevision.get(revisionNumber).add(letter); } String peerReviewContent = ""; for (List<String> revisionLetters : lettersByRevision.values()) { peerReviewContent += "<revision>" + String.join("", revisionLetters) + "</revision>"; } peerReviewContent += acceptanceLetter; peerReviewContent += "<article-received-date>" + articleReceivedDate + "</article-received-date>"; peerReviewContent = "<peer-review>" + peerReviewContent + "</peer-review>"; return peerReviewContent; }