I've written this code that connects to a .xml file at a given url, parses it and outputs the values of some specific elements to the console.
I want to write reusable and appropriate code accordingly to OOP standards, but I'm not sure where to start. How to improve this code?
public class XmlControl {
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
try {
URL xmlUrl = new URL("http://localhost/file.xml");
URLConnection connection = xmlUrl.openConnection();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(connection.getInputStream());
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Start");
NodeList kList = doc.getElementsByTagName("Article");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
for(int count = 0; count < eElement.getElementsByTagName("Article").getLength(); count++) {
Node kNode = kList.item(count);
Element kElement = (Element) kNode;
String title = eElement.getElementsByTagName("Title").item(count).getTextContent();
if(!"".equals(title)) {
System.out.println( title );
}
}
}
}
} catch (IOException | ParserConfigurationException | SAXException | DOMException e) { // Exception e
// e.printStackTrace();
System.out.println("Check your connection");
}
}
}