Java tutorial
//package com.java2s; /******************************************************************************* * Manchester Centre for Integrative Systems Biology * University of Manchester * Manchester M1 7ND * United Kingdom * * Copyright (C) 2007 University of Manchester * * This program is released under the Academic Free License ("AFL") v3.0. * (http://www.opensource.org/licenses/academic.php) *******************************************************************************/ import java.util.*; import org.w3c.dom.*; import org.w3c.dom.Element; public class Main { /** * */ public static final int ALL_VALUES = 2; /** * * @param element * @param tagname * @param valueRange * @return List */ public static List<String> getDOMElementTextByTagName(final Element element, String tagname, final int valueRange) { final List<String> list = new ArrayList<>(); if (element != null) { // Just to ensure we pick off by the name of the child node... final NodeList nodeList = element.getElementsByTagName(tagname.substring(tagname.lastIndexOf("/") + 1)); //$NON-NLS-1$ if (nodeList.getLength() == 0) { list.add(null); } for (int i = 0; i < ((valueRange == ALL_VALUES) ? nodeList.getLength() : 1); i++) { final Node node = nodeList.item(i); if (node != null) { final Node text = node.getFirstChild(); list.add(text.getNodeValue()); } } } else { list.add(null); } return list; } }