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

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

Description

get Element Value Long

License

Open Source License

Declaration

public static long getElementValueLong(Element root, String name)
            throws Exception 

Method Source Code

//package com.java2s;
/*/*from w ww .java  2 s. co  m*/
 *  $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 long getElementValueLong(Element root, String name)
            throws Exception {
        String value = getElementValue(root, name).trim();
        if (value.length() == 0 || "null".equalsIgnoreCase(value))
            return 0;

        if ("unlimited".equalsIgnoreCase(value))
            return Long.MAX_VALUE;

        try {
            return Long.parseLong(value);
        } catch (Exception e) {
            if (DEBUG)
                System.out.println(value + ": cannot converto to long");

            return 0;
        }
    }

    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. getElement(String elementName, Node rootNode)
  2. getElementData(final Element root, final String elementName)
  3. getElementData(final Element root, final String elementName)
  4. getElementText(String elemName, Element root, boolean trim)
  5. getElementValue(Element root, String name)
  6. getRoot(Document doc)
  7. getRoot(Document doc)
  8. getRoot(Document document)
  9. getRoot(final Document doc)