Java examples for XML:DOM Node Value
get XML Node Value with default value
//package com.java2s; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static String getNodeValue(final Node parentNode, final String strNodeName, final String strDefaultValue) { return getNodeValue(parentNode.getChildNodes(), strNodeName, strDefaultValue);//from w w w . j a v a2 s . co m } public static int getNodeValue(final Node parentNode, final String strNodeName, final int iDefaultValue) { final String strValue = getNodeValue(parentNode.getChildNodes(), strNodeName, null); if (strValue != null) { return Integer.parseInt(strValue); } return iDefaultValue; } public static long getNodeValue(final Node parentNode, final String strNodeName, final long lDefaultValue) { final String strValue = getNodeValue(parentNode.getChildNodes(), strNodeName, null); if (strValue != null) { return Long.parseLong(strValue); } return lDefaultValue; } public static double getNodeValue(final Node parentNode, final String strNodeName, final double dDefaultValue) { final String strValue = getNodeValue(parentNode.getChildNodes(), strNodeName, null); if (strValue != null) { return Double.parseDouble(strValue); } return dDefaultValue; } public static String getNodeValue(final NodeList nodes, final String strNodeName, final String strDefaultValue) { for (int i = 0; i < nodes.getLength(); ++i) { final Node node = nodes.item(i); if (node instanceof Element) { final Element elem = (Element) node; if (elem.getNodeName().equals(strNodeName)) { return elem.getLastChild().getTextContent().trim(); } } } // end for return strDefaultValue; } }