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

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

Description

get Value

License

Open Source License

Declaration

public static String getValue(Document doc, String tagName) 

Method Source Code

//package com.java2s;
/**/*from   www  . java2  s  .  co m*/
*Copyright (c) 2000-2002 OCLC Online Computer Library Center,
*Inc. and other contributors. All rights reserved.  The contents of this file, as updated
*from time to time by the OCLC Office of Research, are subject to OCLC Research
*Public License Version 2.0 (the "License"); you may not use this file except in
*compliance with the License. You may obtain a current copy of the License at
*http://purl.oclc.org/oclc/research/ORPL/.  Software distributed under the License is
*distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
*or implied. See the License for the specific language governing rights and limitations
*under the License.  This software consists of voluntary contributions made by many
*individuals on behalf of OCLC Research. For more information on OCLC Research,
*please see http://www.oclc.org/oclc/research/.
*
*The Original Code is XMLUtil.java______________________________.
*The Initial Developer of the Original Code is Jeff Young.
*Portions created by ______________________ are
*Copyright (C) _____ _______________________. All Rights Reserved.
*Contributor(s):______________________________________.
*/

import org.w3c.dom.*;

public class Main {
    private static final boolean debug = false;

    public static String getValue(Document doc, String tagName) {
        return getValue(doc.getDocumentElement(), tagName);
    }

    public static String getValue(Element el, String tagName) {
        return getValue(el, tagName, 0);
    }

    public static String getValue(Element el, String tagName, int index) {
        return getValue(getElement(el, tagName, index));
    }

    public static String getValue(Element el) {
        if (el != null) {
            NodeList nodes = el.getChildNodes();
            StringBuffer sb = new StringBuffer();
            //       String s;
            int length = nodes.getLength();
            for (int i = 0; i < length; ++i) {
                Node node = nodes.item(i);
                String s = null;
                s = node.getNodeValue();
                //       System.out.println("XMLUtil.getValue: s=" + s);
                if (s != null)
                    sb.append(s.trim());
            }
            if (sb.length() > 0) {
                if (debug) {
                    System.out.println("XMLUtil.getValue: sb=" + sb.toString());
                }
                return sb.toString();
            }
        }
        return null;
    }

    public static Element getElement(Document doc, String tagName) {
        return getElement(doc.getDocumentElement(), tagName);
    }

    public static Element getElement(Document doc, String tagName, int index) {
        return getElement(doc.getDocumentElement(), tagName, index);
    }

    public static Element getElement(Element el, String tagName) {
        return getElement(el, tagName, 0);
    }

    public static Element getElement(Element el, String tagName, int index) {
        NodeList list = el.getElementsByTagName(tagName);
        return (Element) list.item(index);
    }
}

Related

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