Java tutorial
//package com.java2s; /** * The contents of this file are subject to the Regenstrief Public License * Version 1.0 (the "License"); you may not use this file except in compliance with the License. * Please contact Regenstrief Institute if you would like to obtain a copy of the license. * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) Regenstrief Institute. All Rights Reserved. */ import org.w3c.dom.*; public class Main { /** * Retrieves the desired attribute from the given Node * * @param xml the Node * @param attr the attribute name String * @return the attribute value String **/ public final static String getAttribute(final Node xml, final String attr) { if (xml == null) { return null; } final NamedNodeMap attrs = xml.getAttributes(); return attrs == null ? null : xmlFindTextNode(attrs.getNamedItem(attr)); } /** * Retrieves the desired attribute from the given Node * * @param xml the Node * @param namespaceURI the attribute namespace URI String * @param localName the attribute local name String * @return the attribute value String **/ public final static String getAttribute(final Node xml, final String namespaceURI, final String localName) { return xml == null ? null : xmlFindTextNode(xml.getAttributes().getNamedItemNS(namespaceURI, localName)); } /** * returns contents of first TextNode defined within an XML Node * * @param xmlNode parsed XML Node in which to look for a TextNode * @return text character string value of text node **/ public static String xmlFindTextNode(final Node xmlNode) { if (xmlNode == null) { return null; } final NodeList children = xmlNode.getChildNodes(); final int childrenCnt = size(children); for (int j = 0; j < childrenCnt; j++) { final Node childNode = children.item(j); if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) { return childNode.getNodeValue(); } } return null; } public static Node getNamedItem(final NamedNodeMap nodeMap, final String name) { return nodeMap == null ? null : nodeMap.getNamedItem(name); } public static Node getNamedItemNS(final NamedNodeMap nodeMap, final String namespaceURI, final String localName) { return nodeMap == null ? null : nodeMap.getNamedItemNS(namespaceURI, localName); } /** * Retrieves the number of Nodes in the NodeList * * @param nodeList the NodeList * @return the number of Nodes **/ public final static int size(final NodeList nodeList) { return nodeList == null ? 0 : nodeList.getLength(); } /** * Retrieves the number of Nodes in the NamedNodeMap * * @param nodeMap the NamedNodeMap * @return the number of Nodes **/ public final static int size(final NamedNodeMap nodeMap) { return nodeMap == null ? 0 : nodeMap.getLength(); } /** * Retrieves the number of Nodes in the tree rooted at the given Node * * @param node the root Node * @return the number of Nodes **/ public final static int size(final Node node) { if (node == null) { return 0; } final NodeList children = node.getChildNodes(); int size = 1; for (int i = 0, n = size(children); i < n; i++) { size += size(children.item(i)); } return size; } }