import java.io.*; import nu.xom.*; public class PropertyFileReader2 { String comment; String username; String password; String browser; boolean showEmail; int visits; public PropertyFileReader2() throws ParsingException, IOException { // get the XML document File propFile = new File("properties2.xml"); Builder builder = new Builder(); Document doc = builder.build(propFile); // get the root element, Element root = doc.getRootElement(); // get the element and store it in a variable Element commentElement = root.getFirstChildElement("comment"); comment = commentElement.getValue(); // get the elements Elements entries = root.getChildElements("entry"); for (int current = 0; current < entries.size(); current++) { // get current Element entry = entries.get(current); // get entry's key attribute Attribute key = entry.getAttribute("key"); String keyValue = key.getValue(); // store attribute value in the proper variable if (keyValue.equals("username")) { username = entry.getValue(); } if (keyValue.equals("password")) { password = entry.getValue(); } if (keyValue.equals("browser")) { browser = entry.getValue(); } if (keyValue.equals("showEmail")) { String emailValue = entry.getValue(); if (emailValue.equals("yes")) { showEmail = true; } else { showEmail = false; } } if (keyValue.equals("visits")) { String visitValue = entry.getValue(); try { visits = Integer.parseInt(visitValue); } catch (NumberFormatException exception) { visits = -1; } } } } public void showProperties() { System.out.println("\nProperties\n"); System.out.println("Comment: " + comment); System.out.println("Username: " + username); System.out.println("Password: " + password); System.out.println("Browser: " + browser); System.out.println("Show Email: " + showEmail); System.out.println("Visits: " + visits); } public static void main(String[] arguments) { try { PropertyFileReader2 reader = new PropertyFileReader2(); reader.showProperties(); } catch (Exception exception) { System.out.println("Error: " + exception.getMessage()); } } }