Example usage for org.w3c.dom Element getTextContent

List of usage examples for org.w3c.dom Element getTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Element getTextContent.

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:Main.java

public static Map<String, String> NodeListToMap(NodeList nList) {
    Map<String, String> simpleMap = new HashMap<String, String>();
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                simpleMap.put(eElement.getTagName(), eElement.getTextContent());
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return simpleMap;
}

From source file:org.shareok.data.documentProcessor.FileHandlerFactory.java

/**
 *
 * @param fileExtension//from  ww w .  j  a v a 2  s. co m
 * @return
 */
public static FileHandler getFileHandlerByFileExtension(String fileExtension) {

    FileHandler fh = null;
    String beanName = "";

    try {
        String fileTypePath = DocumentProcessorUtil.getFilePathFromResources("filetypes.xml");
        Document fileTypeDoc = loadXMLFromString(fileTypePath);
        fileTypeDoc.getDocumentElement().normalize();
        Element docEle = fileTypeDoc.getDocumentElement();
        NodeList nl = docEle.getChildNodes();

        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {
                if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Element el = (Element) nl.item(i);
                    String nodeVal = el.getTextContent();
                    if (nodeVal.equals(fileExtension)) {
                        beanName = el.getAttribute("bean");
                        break;
                    }
                }
            }
        }

        ApplicationContext context = new ClassPathXmlApplicationContext("documentProcessorContext.xml");
        fh = (FileHandler) context.getBean(beanName);

    } catch (Exception ex) {
        Logger.getLogger(DocumentProcessorUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    return fh;
}

From source file:Main.java

/** 
 * Gets the value of the child element by tag name under the given parent 
 * element. If there is more than one child element, return the value of the 
 * first one. //from w w w. java2  s . c  o m
 * 
 * @param parent the parent element 
 * @param tagName the tag name of the child element 
 * @return value of the first child element, NULL if tag not exists 
 */
public static String getElementValue(Element parent, String tagName) {
    String value = null;

    Element element = getElement(parent, tagName);
    if (element != null) {
        value = element.getTextContent();
    }

    return value;
}

From source file:Main.java

public static Map<String, String> SimpleDocumentToMap(Document doc) {
    Map<String, String> simpleMap = new HashMap<String, String>();
    //trim off outter layer
    Node node = doc.getFirstChild();
    NodeList nList = node.getChildNodes();
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                simpleMap.put(eElement.getTagName(), eElement.getTextContent());
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return simpleMap;
}

From source file:Main.java

public static ArrayList<String> getFontSizeList() {
    ArrayList<String> fontList = new ArrayList<String>();
    DocumentBuilderFactory dbfFont = null;
    DocumentBuilder dbFont = null;
    Document domFont = null;//from   w  w  w. j av  a2 s.  c o  m

    try {
        dbfFont = DocumentBuilderFactory.newInstance();
        String fontSizeFileName = "resources/FontSizes.xml";

        //Using factory get an instance of document builder
        dbFont = dbfFont.newDocumentBuilder();

        //parse using builder to get DOM representation of the XML file
        domFont = dbFont.parse(fontSizeFileName);

        //get the root elememt
        Element docEle = domFont.getDocumentElement();

        //get a nodelist of <sizes> elements
        NodeList sizeList = docEle.getElementsByTagName("size");
        if (sizeList != null && sizeList.getLength() > 0) {
            for (int i = 0; i < sizeList.getLength(); i++) {

                //get the employee element
                Element sizeElement = (Element) sizeList.item(i);
                fontList.add(sizeElement.getTextContent());
            }

        }
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (SAXException se) {
        se.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return fontList;

}

From source file:Main.java

/**
 * Loads an optional double element value from a XML element
 *//*from w ww  .j  a  v a  2s.  c  o m*/
public static double getDoubleNode(Element element, String name) {
    Element child = getSingleElement(element, name);

    if (child == null)
        return 0.0;

    String value = child.getTextContent();

    if (value == null)
        return 0.0;

    if (value.length() == 0)
        return 0.0;

    return Double.parseDouble(value);
}

From source file:Main.java

public static String getChildElemText(Element parent, String tagName) {
    String val = null;
    Element childElem = getFirstChildByTagName(parent, tagName);
    if (childElem != null) {
        val = childElem.getTextContent();
    }//w w  w  . j  av a2s .c om
    return val;
}

From source file:Main.java

/**
 * Returns the text content of the child element with the specified tag
 * name, and the default value if no such element exists or the element does
 * not have any text content./*w ww .ja  v a  2 s  .co  m*/
 * 
 * @param element
 * @param localName
 * @return
 */
public static String getContent(Element element, String localName, String defaultValue) {
    Element child = getElement(element, localName);
    if (child != null) {
        String value = child.getTextContent();
        if (value != null) {
            return value;
        }
    }
    return defaultValue;
}

From source file:Main.java

/**
 * Loads an optional string element value from a XML element
 *///from   ww  w .  j ava  2  s  .co  m
public static String getStringNode(Element element, String name, String _default) {
    Element child = getSingleElement(element, name);

    if (child == null)
        return _default;

    String value = child.getTextContent();

    if (value == null)
        return _default;

    return value;
}

From source file:Main.java

/**
 * Gets the value of the child element by tag name under the given parent
 * element. If there is more than one child element, return the value of the
 * first one.//  w  w w  .j a  v a2  s.  co m
 * 
 * @param parent the parent element
 * @param tagName the tag name of the child element
 * @return value of the first child element, NULL if tag not exists
 */
public static String getElementValue(Element parent, String tagName) {
    Element element = getElement(parent, tagName);
    if (element != null) {
        return element.getTextContent();
    } else {
        return null;
    }
}