Java tutorial
//package com.java2s; /** * Copyright 2014 * SMEdit https://github.com/StarMade/SMEdit * SMTools https://github.com/StarMade/SMTools * * 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. **/ import java.util.ArrayList; import java.util.List; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { /** * @param n a root node to search beneath * @param name a node name to look for * @return the first instance of that node name in the tree beneath the root * (depth first) */ public static Node findFirstNodeRecursive(Node n, String name) { List<Node> nodes = new ArrayList<>(); findRecursive(n, name, nodes, true); if (nodes.isEmpty()) { return null; } return nodes.get(0); } private static void findRecursive(Node parent, String name, List<Node> nodes, boolean onlyOne) { String nn = parent.getNodeName(); int off = nn.indexOf(':'); if (off >= 0) { nn = nn.substring(off + 1); } if (nn.equals(name)) { nodes.add(parent); if (onlyOne) { return; } } for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { findRecursive(child, name, nodes, onlyOne); if (onlyOne && (nodes.size() > 0)) { return; } } } /** * @param n1 first Node to test * @param n2 second Node to test * @return true if a deep compare show the same children and attributes in * the same order */ public static boolean equals(Node n1, Node n2) { // compare type if (!n1.getNodeName().equals(n2.getNodeName())) { return false; } // compare attributes NamedNodeMap nnm1 = n1.getAttributes(); NamedNodeMap nnm2 = n2.getAttributes(); if (nnm1.getLength() != nnm2.getLength()) { return false; } for (int i = 0; i < nnm1.getLength(); i++) { Node attr1 = nnm1.item(i); if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName()))) { return false; } } // compare children Node c1 = n1.getFirstChild(); Node c2 = n2.getFirstChild(); for (;;) { while ((c1 != null) && c1.getNodeName().startsWith("#")) { c1 = c1.getNextSibling(); } while ((c2 != null) && c2.getNodeName().startsWith("#")) { c2 = c2.getNextSibling(); } if ((c1 == null) && (c2 == null)) { break; } if ((c1 == null) || (c2 == null)) { return false; } if (!equals(c1, c2)) { return false; } c1 = c1.getNextSibling(); c2 = c2.getNextSibling(); } return true; } /** * @param n Node to examine * @param attr Attribute to look for * @param def Default value to return if attribute is not present * @return if the Node contains the named Attribute, the value, if not, the * def parameter */ public static String getAttribute(Node n, String attr, String def) { NamedNodeMap attrs = n.getAttributes(); if (attrs == null) { return def; } Node ret = attrs.getNamedItem(attr); if (ret == null) { return def; } else { return ret.getNodeValue(); } } /** * @param n Node to examine * @param attr Attribute to look for * @return if the Node contains the named Attribute, the value, if not, * empty string */ public static String getAttribute(Node n, String attr) { return getAttribute(n, attr, ""); } }