List of usage examples for org.dom4j Element appendAttributes
void appendAttributes(Element element);
From source file:DAO.LocationDAO.java
public boolean addNewLocation(String webAppPath, String location) { try {/* ww w.j a v a 2 s. c o m*/ SAXReader reader = new SAXReader(); Document document = reader.read(webAppPath + "/xml/Locations.xml"); Element root = document.getRootElement(); int max = 0; for (Iterator i = root.elementIterator("Location"); i.hasNext();) { Element elt = (Element) i.next(); int cur = Integer.parseInt(elt.element("LocationId").getText()); if (cur > max) { max = cur; } } Integer newLocationId = max + 1; Element newLocation = root.addElement("Location"); newLocation.addElement("LocationId").setText(newLocationId.toString()); newLocation.addElement("Name").setText(location); root.appendAttributes(newLocation); try { FileOutputStream fos = new FileOutputStream(webAppPath + "/xml/Locations.xml"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlwriter = new XMLWriter(fos, format); xmlwriter.write(document); System.out.println("dead"); xmlwriter.close(); } catch (Exception ex) { System.out.println("Failed!"); ex.printStackTrace(); } //Node node = document.selectSingleNode("/Locations/Location[not(../Location/LocationId > LocationId)]"); //String id = node.valueOf("LocationId"); //System.out.println(id); return true; } catch (DocumentException ex) { System.out.println("Add New Location Failed!"); Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); return false; } }
From source file:net.unicon.toro.installer.tools.MergeConfiguration.java
License:Open Source License
private void replaceElement(Element sourceEl, Element replacementElement) { if (!sourceEl.getName().equals(replacementElement.getName())) { throw new RuntimeException("replacement element must have the same name: " + sourceEl.getName() + " and " + replacementElement.getName() + " differ."); }/*from w w w. j a v a2 s. c om*/ // remove all the existing sourceEl.attributes().clear(); // remove all child elements sourceEl.elements().clear(); // add all the new attributes sourceEl.appendAttributes(replacementElement); // add all the new child elements Iterator eItr = new ArrayList(replacementElement.elements()).iterator(); while (eItr.hasNext()) { Element newEl = (Element) eItr.next(); replacementElement.remove(newEl); sourceEl.add(newEl); } }