Java XML Element Get by Name getValue(Document doc, String Tag)

Here you can find the source of getValue(Document doc, String Tag)

Description

Get Value from XML

License

Open Source License

Parameter

Parameter Description
node a parameter
Tag a parameter

Declaration

public static String getValue(Document doc, String Tag) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Product: OSeB http://code.google.com/p/oseb                                *
 * Copyright (C) 2012 Mario Grigioni                                          *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 *****************************************************************************/

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

public class Main {
    /**//from  w w  w .jav  a  2s  . co  m
     *    Get Value from XML
     *
     * @param node
     * @param Tag
     * @return
     */
    public static String getValue(Node node, String Tag) {
        if (node == null)
            return "";

        NodeList nl = ((Element) node).getElementsByTagName(Tag);
        if (nl == null)
            return "";

        Element el = (Element) nl.item(0);
        if (el == null)
            return "";

        nl = el.getChildNodes();
        if (nl == null)
            return "";

        return nl.item(0).getNodeValue();
    }

    /**
     *    Get Value from XML
     *
     * @param node
     * @param Tag
     * @return
     */
    public static String getValue(Document doc, String Tag) {
        if (doc.getElementsByTagName(Tag) == null)
            return "";

        if (doc.getElementsByTagName(Tag).item(0) == null)
            return "";

        return doc.getElementsByTagName(Tag).item(0).getTextContent();
    }
}

Related

  1. getFirstElementByTagName(Element parent, String tagName)
  2. getGrandSonElementByTagName(Element element, String parentName, String eleName)
  3. getGrandSonElementsByTagName(Element ele, String parentName, String eleName)
  4. getGrandSonElementValueByTagName(Element element, String parentName, String eleName)
  5. getGrandSonListValueByTagName(Element element, String parentName, String eleName)
  6. getValue(Document doc, String tagName)
  7. getValue(Document pDocument, String psTagName)
  8. getXMLElement(Document document, String name)