Java tutorial
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Find child element and create if not exists * @param element parent element * @param childName child name * @return */ public static Element getChildElement(Element element, String childName) { Element result = findChildElement(element, childName); if (element == null) { result = element.getOwnerDocument().createElement(childName); element.appendChild(result); } return result; } /** * Find child element by name * @param element parent element * @param childName child name * @return */ public static Element findChildElement(Element element, String childName) { for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof Element && childName.equals(child.getNodeName())) { return (Element) child; } } return null; } }