Java XML Element Get by Name getElementByName(Document document, String name)

Here you can find the source of getElementByName(Document document, String name)

Description

get Element By Name

License

Open Source License

Parameter

Parameter Description
document a parameter
name a parameter

Declaration

public static String getElementByName(Document document, String name) 

Method Source Code

//package com.java2s;
/* $This file is distributed under the terms of the license in /doc/license.txt$ */

import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**//from  ww  w  . ja va  2 s. c  o  m
     * @param document
     * @param name
     * @return
     */
    public static String getElementByName(Document document, String name) {
        NodeList nodes = document.getElementsByTagName(name);
        String s = null;
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            s = node.getTextContent().trim();
        }
        return s;
    }
}

Related

  1. getElement(Element parent, String tagName)
  2. getElement(Element parentElement, String nodeName)
  3. getElement(Node parent, int index)
  4. getElement(String elementName, Element parentElement)
  5. getElement(String name, int index, Element parent)
  6. getElementByName(Element eParent, String elementName)
  7. getElementByName(Element parent, String nodeName)
  8. getElementByTag(HTMLDocument htmlDoc, int offset, Tag tag)
  9. getElementByTagAndName(Document document, String tagName, String elementName)