Here you can find the source of getTextValue(Element ele, String tagName)
Parameter | Description |
---|---|
ele | Dom element |
tagName | name of xml tag |
public static String getTextValue(Element ele, String tagName)
//package com.java2s; /******************************************************************************* * Copyright (c) 2009, 2011 The University of Memphis. All rights reserved. * This program and the accompanying materials are made available * under the terms of the LIDA Software Framework Non-Commercial License v1.0 * which accompanies this distribution, and is available at * http://ccrg.cs.memphis.edu/assets/papers/2010/LIDA-framework-non-commercial-v1.0.pdf *******************************************************************************/ import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.Text; public class Main { /**/*from ww w . ja va2 s. com*/ * Returns text value of first element in specified element with specified * tag. Returns null if specified tag is empty or only has subchildren. * * @param ele * Dom element * @param tagName * name of xml tag * @return text value of element with specified xml tag */ public static String getTextValue(Element ele, String tagName) { String textVal = null; List<Element> nl = getChildren(ele, tagName); if (nl != null && nl.size() != 0) { Element el = nl.get(0); textVal = getValue(el); if (textVal != null) { textVal = textVal.trim(); if (textVal.equalsIgnoreCase("")) { return null; } } } return textVal; } /** * Returns all children with specified name. * * @param parent * {@link Element} * @param name * name of sought children * @return list of all children found. */ public static List<Element> getChildren(Element parent, String name) { List<Element> nl = new ArrayList<Element>(); for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof Element && name.equals(child.getNodeName())) { nl.add((Element) child); } } return nl; } /** * Returns String value of first child found that is a {@link Text}. or null * if the Text is empty * * @param parent * Element * @return child's string value. */ public static String getValue(Element parent) { for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof Text) { String value = child.getNodeValue().trim(); if (value.equalsIgnoreCase("")) { return null; } else { return value; } } } return null; } }