Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * Delete child element by name
     * @param element parent element
     * @param childName child name
     */
    public static void removeChild(Element element, String childName) {
        Element childElement = findChildElement(element, childName);
        if (childElement != null) {
            childElement.removeChild(element);
        }
    }

    /**
     * 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;
    }
}