Java XML Element Root getElementValue(Element root, String name)

Here you can find the source of getElementValue(Element root, String name)

Description

get Element Value

License

Open Source License

Declaration

public static String getElementValue(Element root, String name)
            throws Exception 

Method Source Code

//package com.java2s;
/*/*from   www.  ja v  a 2  s  .c  om*/
 *  $File$ $Revision$ $Date$
 *
 *  ADOBE SYSTEMS INCORPORATED
 *    Copyright 2007 Adobe Systems Incorporated
 *    All Rights Reserved.
 *
 *  NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the 
 *  terms of the Adobe license agreement accompanying it.  If you have received this file from a 
 *  source other than Adobe, then your use, modification, or distribution of it requires the prior 
 *  written permission of Adobe.
 */

import javax.xml.parsers.*;
import org.w3c.dom.DOMImplementation;

import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;

public class Main {
    public static boolean DEBUG = false;

    public static String getElementValue(Element root, String name)
            throws Exception {
        try {
            return root.getElementsByTagName(name).item(0).getTextContent();
        } catch (Exception e) {
            if (DEBUG) {
                System.out.println("element: " + name);
                System.out.println(printXML(root));
            }

            throw e;
        }
    }

    public static String printXML(Element root) throws Exception {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();
        DOMImplementationLS ls = (DOMImplementationLS) impl.getFeature(
                "LS", "3.0");
        return ls.createLSSerializer().writeToString(root);
    }
}

Related

  1. createRootElement(Document doc, String rootTagName)
  2. getElement(String elementName, Node rootNode)
  3. getElementData(final Element root, final String elementName)
  4. getElementData(final Element root, final String elementName)
  5. getElementText(String elemName, Element root, boolean trim)
  6. getElementValueLong(Element root, String name)
  7. getRoot(Document doc)
  8. getRoot(Document doc)
  9. getRoot(Document document)