/***** 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
<?xml version="1.0" encoding="windows-1252"?> <!-- Edited by MesEXemple.com --> <note> <to>Sakoba</to> <from>Adams</from> <heading>Rappel</heading> <body>Ne m'oubliez pas ce week-end!</body> </note> |
Cet article Java XML: Modifier les attibuts des éléments est apparu en premier sur .