Java XML Node Value getValue(Node node, String Tag)

Here you can find the source of getValue(Node node, 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(Node node, 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 {
    /**/* ww  w .j a v a  2 s  .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. getValue(Node node)
  2. getValue(Node node)
  3. getValue(Node node, short nodeType)
  4. getValue(Node node, short nodeType)
  5. getValue(Node node, short nodeType)
  6. getValue(Node pNode)
  7. getValueByElement(Node node)
  8. getValueByTagName(Node n, String tag)
  9. getValueOfValueNode(Node n)