import java.io.*; import nu.xom.*; public class ChangeDomains { public static void main(String[] arguments) throws IOException { try { // Create a tree from the XML document domains.xml Builder builder = new Builder(); File xmlFile = new File("domains.xml"); Document doc = builder.build(xmlFile); // Get the root element Element root = doc.getRootElement(); // Loop through of its elements Elements rootChildren = root.getChildElements("domain"); for (int i = 0; i < rootChildren.size(); i++) { // Get a element Element domain = rootChildren.get(i); // Get its element and the text it encloses Element name = domain.getFirstChildElement("name"); Text nameText = (Text) name.getChild(0); // Update any domain with the text "java21days.com" if (nameText.getValue().equals("java21days.com")) updateDomain(domain); } // Display the XML document System.out.println(doc.toXML()); } catch (ParsingException pe) { System.out.println("Error: " + pe.getMessage()); pe.printStackTrace(); System.exit(-1); } } private static void updateDomain(Element domain) { // Get the domain's element Element dns = domain.getFirstChildElement("dns"); // Get its element Element ttl = dns.getFirstChildElement("ttl"); // Replace its Text child with "604800" ttl.removeChild(0); ttl.appendChild("604800"); // Create new elements and attributes to add Element email = new Element("email"); Element address = new Element("address"); Attribute user = new Attribute("user", "postmaster@java21days.com"); Attribute destination = new Attribute("destination", "rcade"); // Add them to the element domain.appendChild(email); email.appendChild(address); address.addAttribute(user); address.addAttribute(destination); } }