Java tutorial
//package com.java2s; /* Copyright (c) 2016 by crossmobile.org * * CrossMobile is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 2. * * CrossMobile is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with CrossMobile; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import java.util.ArrayList; import java.util.List; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static Node getNodeWithPath(Node parent, String... name) { Node current = parent; for (String n : name) if (current != null) current = getNodeWithName(parent, n); else break; return current; } public static Node getNodeWithPath(Node parent, String path, String delimiter) { return getNodeWithPath(parent, path.split(delimiter)); } public static Node getNodeWithName(Node parent, String name) { List<Node> nodes = getNodesWithName(parent, name, false); if (nodes.size() < 1) return null; return nodes.get(0); } public static List<Node> getNodesWithName(Node parent, String name, boolean all_of_them) { ArrayList<Node> valid = new ArrayList<>(); NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) if (children.item(i).getNodeType() == Node.ELEMENT_NODE && (name == null || children.item(i).getNodeName().equalsIgnoreCase(name))) { valid.add(children.item(i)); if (!all_of_them) break; } return valid; } }