Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class Main { public static Element getElement(Document document, String[] path, String value, String translate, String module) { Node node = document; NodeList list; for (int i = 0; i < path.length; ++i) { list = node.getChildNodes(); boolean found = false; for (int j = 0; j < list.getLength(); ++j) { Node testNode = list.item(j); if (testNode.getNodeName().equals(path[i])) { found = true; node = testNode; break; } } if (found == false) { Element element = document.createElement(path[i]); node.appendChild(element); node = element; } } if (value != null) { Text text = document.createTextNode(value); list = node.getChildNodes(); boolean found = false; for (int j = 0; j < list.getLength(); ++j) { Node testNode = list.item(j); if (testNode instanceof Text) { node.replaceChild(text, testNode); found = true; break; } } if (!found) node.appendChild(text); } if (node instanceof Element) { Element element = (Element) node; if (translate != null && element.getAttribute("translate").equals("")) { element.setAttribute("translate", translate); } if (module != null && element.getAttribute("module").equals("")) { element.setAttribute("module", module); } } return (Element) node; } public static Element getElement(Document document, String[] path, String value) { return getElement(document, path, value, null, null); } public static Element getElement(Document document, String path, String value, String translate, String module) { String[] pathArray = path.split("/"); return getElement(document, pathArray, value, translate, module); } public static Element getElement(Document document, String path, String value) { return getElement(document, path, value, null, null); } }