Java tutorial
//package com.java2s; /* * XMLUtility.java 2/6/13 1:04 PM * * Copyright (C) 2012-2013 Nick Ma * * This program 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; either version 3 * of the License, or any later version. * This program 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. */ import java.util.Enumeration; import java.util.Vector; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Searches parent node for matching child nodes and collects and returns * their matching attribute String values. * * @param node The parent node * @param elemName The matching child node element name * @param attr The matching child node attribute name * @return List of attribute values */ public static Enumeration getChildrenAttributeValues(Node node, String elemName, String attr) { Vector vect = new Vector(); NodeList nl = node.getChildNodes(); int n = nl.getLength(); for (int i = 0; i < n; i++) { Node nd = nl.item(i); if (nd.getNodeName().equals(elemName)) { vect.add(getAttribute(nd, attr)); } } return vect.elements(); } /** * Returns the value of the given attribute. * * @param node The current node * @param attrName The attribute name * @return The attribute's value */ public static String getAttribute(Node node, String attrName) { // handle null node if (node == null) { return null; } Node attr = node.getAttributes().getNamedItem(attrName); if (attr != null) { return attr.getNodeValue(); } return null; } /** * Searches parent node for matching child nodes, then collects and returns * the first match node's value. Useful when caller knows that there is only * one child node. * @param node The parent node * @return The child node's value */ public static String getNodeValue(Node node) { // sometimes jdom use more than one children nodes to represent text node NodeList children = node.getChildNodes(); String value = ""; for (int i = 0; i < children.getLength(); i++) { value += children.item(i).getNodeValue(); } return value; } /** * Searches parent node for matching child nodes, then collects and returns * the first match node's value. Useful when caller knows that there is only * one child node. * * @param node The parent node * @param nodeName The child node element name * @return The child node's value */ public static String getNodeValue(Node node, String nodeName) { Node n = getNode(node, nodeName); if (n == null) { return null; } Node ch = n.getFirstChild(); if (ch != null) { return ch.getNodeValue(); } return null; } /** * Searches parent node and returns first found matching node. * * @param node The parent node * @param nodeName The child node's element name * @return The first matching child Node */ public static Node getNode(Node node, String nodeName) { NodeList ch = node.getChildNodes(); int l = ch.getLength(); for (int i = 0; i < l; i++) { Node n = ch.item(i); if (n.getNodeName().equals(nodeName)) { return n; } } return null; } }