Source:
public ITextFile parseTextXML(String xml, String wsId, String projId) throws TextFileParseException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document document = null; try { builder = factory.newDocumentBuilder(); document = builder.parse(new InputSource(new StringReader(xml))); } catch (ParserConfigurationException | IOException e) { logger.error("Error while parsing the XML", e); throw new TextFileParseException("Error while parsing the XML"); } catch (SAXException sae) { logger.error("Error while parsing the XML", sae); throw new TextFileParseException("This XML file is not well formed"); } NodeList textNodeList = document.getElementsByTagName("text"); NodeList handleNode = document.getElementsByTagName("handle"); NodeList fileNameNode = document.getElementsByTagName("file_name"); if (handleNode.getLength() == 0 && fileNameNode.getLength() == 0) { throw new TextFileParseException("Handle and file name must be specified in the input XML"); } else if (handleNode.getLength() == 0) { throw new TextFileParseException("Handle must be specified in the input XML"); } else if (fileNameNode.getLength() == 0) { throw new TextFileParseException("File name must be specified in the input XML"); } else if (textNodeList.getLength() == 0) { throw new TextFileParseException("File content must be specified in the input XML"); } String fileContent = textNodeList.item(0).getTextContent(); String fileName = fileNameNode.item(0).getTextContent(); String refId = handleNode.item(0).getTextContent(); if (fileContent.isEmpty()) { throw new TextFileParseException("Specify File Content in the XML"); } else if (fileName.isEmpty()) { throw new TextFileParseException("Filename cannot be empty"); } else if (refId.isEmpty()) { throw new TextFileParseException("Handle cannot be empty"); } ITextFile txtFile = txtFileFactory.createTextFileObject(); txtFile.setFileContent(fileContent); txtFile.setFileName(fileName); txtFile.setRefId(refId); txtFile.setWorkspaceId(wsId); txtFile.setProjectId(projId); return txtFile; }

Target:
public ITextFile parseTextXML(String xml, String wsId, String projId) throws TextFileParseException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document document = null; try { builder = factory.newDocumentBuilder(); document = builder.parse(new InputSource(new StringReader(xml))); } catch (ParserConfigurationException | IOException e) { logger.error("Error while parsing the XML"); throw new TextFileParseException(e); } catch (SAXException sae) { logger.error("Error while parsing the XML"); throw new TextFileParseException(sae); } NodeList textNodeList = document.getElementsByTagName("text"); NodeList handleNode = document.getElementsByTagName("handle"); NodeList fileNameNode = document.getElementsByTagName("file_name"); if (handleNode.getLength() == 0 && fileNameNode.getLength() == 0) { throw new TextFileParseException("Handle and file name must be specified in the input XML"); } else if (handleNode.getLength() == 0) { throw new TextFileParseException("Handle must be specified in the input XML"); } else if (fileNameNode.getLength() == 0) { throw new TextFileParseException("File name must be specified in the input XML"); } else if (textNodeList.getLength() == 0) { throw new TextFileParseException("File content must be specified in the input XML"); } String fileContent = textNodeList.item(0).getTextContent(); String fileName = fileNameNode.item(0).getTextContent(); String refId = handleNode.item(0).getTextContent(); if (fileContent.isEmpty()) { throw new TextFileParseException("Specify File Content in the XML"); } else if (fileName.isEmpty()) { throw new TextFileParseException("Filename cannot be empty"); } else if (refId.isEmpty()) { throw new TextFileParseException("Handle cannot be empty"); } ITextFile txtFile = txtFileFactory.createTextFileObject(); txtFile.setFileContent(StringEscapeUtils.unescapeXml(fileContent)); txtFile.setFileName(fileName); txtFile.setRefId(refId); txtFile.setWorkspaceId(wsId); txtFile.setProjectId(projId); return txtFile; }