import java.io.*; import nu.xom.*; public class PropertyFileReader { String comment; String username; String browser; boolean showEmail; public PropertyFileReader() throws ParsingException, IOException { // get the XML document File propFile = new File("properties.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("browser")) { browser = entry.getValue(); } if (keyValue.equals("showEmail")) { String emailValue = entry.getValue(); if (emailValue.equals("yes")) { showEmail = true; } else { showEmail = false; } } } } public void showProperties() { System.out.println("\nProperties\n"); System.out.println("Comment: " + comment); System.out.println("Username: " + username); System.out.println("Browser: " + browser); System.out.println("Show Email: " + showEmail); } public static void main(String[] arguments) { try { PropertyFileReader reader = new PropertyFileReader(); reader.showProperties(); } catch (Exception exception) { System.out.println("Error: " + exception.getMessage()); } } }