/***** Code de MesExemples.com *******/
/**
* @(#)UpdateAttribute.java
*
*
* @author
*sakoba(java.mesexemples.com) @version 1.00 2013/7/4
*/
public class UpdateAttribute {
public void setAttributes(Document doc) {
Element root = doc.getDocumentElement();
Element element_x = (Element)root.getFirstChild();
Element element_y = (Element)element_x.getNextSibling();
Element element_z = (Element)element_y.getNextSibling();
Attr deptAttr = element_x.getAttributeNode("dept");
element_x.removeAttributeNode(deptAttr);
String deptString = deptAttr.getValue();
element_y.setAttribute("dept",deptString);
element_z.setAttribute("dept",deptString);
String mailString = element_x.getAttribute("mail");
element_y.setAttribute("mail",mailString);
String titleString = element_x.getAttribute("title");
element_x.removeAttribute("title");
element_z.setAttribute("title",titleString);
}
}
Code testé avec le fichier XML Suivant
Sakoba
Adams
Rappel
Ne m'oubliez pas ce week-end!