few months back I did a code test for a company and I quickly managed to solve it within 5-10 minutes. But, I got rejected because it wasnt "good enough".
What is good enough? they said its mainly how I wrote the code and that I was missing exception handling..
Anyways I was supposed to retrieve data from an XML file and store it into a file. Here is my code:
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException { xmlDataToFile("src/resources/xml-File.xml", "41"); } static void xmlDataToFile(String url, String value) throws ParserConfigurationException, IOException, SAXException { DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = documentBuilder.parse(url); NodeList nodeList = document.getElementsByTagName("target"); for (int i = 0; i < nodeList.getLength(); i++){ Node node = nodeList.item(i); if (node.getParentNode().getAttributes().item(0).getTextContent().equals(value)){ Files.write(Paths.get("textFile.txt"), Collections.singleton(nodeList.item(i).getTextContent())); } }
How can I improve this ?