Java XML Element Get from Parent getElementValueByTagName(Document doc, String parentName, String eleName)

Here you can find the source of getElementValueByTagName(Document doc, String parentName, String eleName)

Description

get Element Value By Tag Name

License

Open Source License

Declaration

public static String getElementValueByTagName(Document doc, String parentName, String eleName) 

Method Source Code


//package com.java2s;

import org.w3c.dom.*;

public class Main {

    public static String getElementValueByTagName(Document doc, String parentName, String eleName) {

        NodeList nl = doc.getElementsByTagName(parentName);
        if (null == nl) {
            return null;
        }//  www  . j av a 2  s.  c o  m
        Node item = nl.item(0);
        return getChildElementValueByTagName((Element) item, eleName);
    }

    public static String getChildElementValueByTagName(Element ele, String childEleName) {

        Element child = getChildElementByTagName(ele, childEleName);
        return (child != null ? getTextValue(child) : null);
    }

    public static Element getChildElementByTagName(Element ele, String childEleName) {

        NodeList nl = ele.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element && childEleName.equals(node.getNodeName())
                    || childEleName.equals(node.getLocalName())) {
                return (Element) node;
            }
        }
        return null;
    }

    public static String getTextValue(Element valueEle) {

        StringBuffer value = new StringBuffer();
        NodeList nl = valueEle.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node item = nl.item(i);
            if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
                value.append(item.getNodeValue());
            }
        }
        return value.toString().trim();
    }
}

Related

  1. getElementIntValue(Document document, Element parent, String string)
  2. getElements(Document document, Element parent)
  3. getElements(Document document, Element parent)
  4. getElementStringValue(Document document, Element parent, String element)
  5. getFirstElement(Node parent)
  6. getFirstElement(Node parent)
  7. getFirstNode(final Node parent, final String... path)
  8. getNamedElemValue(Element parent, String elementName)