Java XML Child Remove All removeAllNamedChild(Element parent, String name)

Here you can find the source of removeAllNamedChild(Element parent, String name)

Description

remove All Named Child

License

Apache License

Declaration

public static void removeAllNamedChild(Element parent, String name) 

Method Source Code

//package com.java2s;
/*/*from w w  w.ja  v  a  2  s  .  c  o  m*/
 * Copyright 2013 Keith D Swenson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors Include: Shamim Quader, Sameer Pradhan, Kumar Raja, Jim Farris,
 * Sandia Yang, CY Chen, Rajiv Onat, Neal Wang, Dennis Tam, Shikha Srivastava,
 * Anamika Chaudhari, Ajay Kakkar, Rajeev Rastogi
 */

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static void removeAllNamedChild(Element parent, String name) {
        NodeList childNdList = parent.getChildNodes();
        for (int i = 0; i < childNdList.getLength(); i++) {
            org.w3c.dom.Node n = childNdList.item(i);
            if (n == null) {
                continue; //there are strange cases where it can be null
            }
            if (n.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
                continue;
            }
            if (name.equals(n.getLocalName())) {
                parent.removeChild(n);
            }
            if (name.equals(n.getNodeName())) {
                parent.removeChild(n);
            }
        }
    }
}

Related

  1. removeAllChildren(Node node)
  2. removeAllChildren(Node node)
  3. removeAllChildren(org.w3c.dom.Element element)
  4. removeAllChildrenNotElement(Element parentElem)
  5. removeAllChildrenOfNode(Node node)