Example usage for org.w3c.dom Node getNodeType

List of usage examples for org.w3c.dom Node getNodeType

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeType.

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

public static Element[] getChildrenByName(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<Node> list = new LinkedList<Node>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(n);//from   w  ww .  j a  v  a2 s. co  m
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

public static void setCDATAContent(Node node, String value) {
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node child = nodeList.item(i);
            if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
                CDATASection cdata = (CDATASection) child;
                cdata.setData(value);/*from w  w  w  .j  a  va 2s  .c o  m*/
            }
        }
    }
}

From source file:Main.java

public static String getTagValue(Node node, String name) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {

        Element el = (Element) node;
        NodeList nodeList = el.getElementsByTagName(name);
        if (nodeList.getLength() > 0) {
            el = (Element) nodeList.item(0);
            nodeList = el.getChildNodes();

            if (nodeList.getLength() > 0) {
                return ((Node) nodeList.item(0)).getNodeValue();
            }/*from   w  w  w.  j av  a  2s.  c  o m*/
        }

    }
    return "";
}

From source file:Main.java

/**
 * Extracts the first CDATA child from the given {@code org.w3c.dom.Element}.
 *
 * @param elem the parent Element//w ww . j a v a 2  s . c o m
 * @return the String value of the CDATA section, or {@code null} if none
 *         found
 */
public static String getCdata(Element elem) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
            CharacterData cdataNode = (CharacterData) node;
            return cdataNode.getData();
        }
    }
    return null;
}

From source file:Main.java

public static void removeElementXML(Node node, short nodeType, String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {/*  www.  j av  a 2  s  . co  m*/
        // Visit the children
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeElementXML(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static final String getText(Node node) {
    if (node.hasChildNodes()) {
        NodeList childNodes = node.getChildNodes();
        if (childNodes.getLength() > 0) {
            Node child = childNodes.item(0);
            if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
                return child.getNodeValue();
            }/*w w  w  .j a  v a 2 s  .c  o  m*/
        }
    }
    return null;
}

From source file:Main.java

public static List getChildrenElement(Node n, String childTagName) {
    List lst = new ArrayList();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);

        if ((cn.getNodeType() == Node.ELEMENT_NODE) && (cn.getNodeName().equals(childTagName))) {
            lst.add(cn);/* www . jav  a 2 s. c om*/
        }
    }
    return lst;
}

From source file:Main.java

public static Element getChildElement(String tagName, Element element) {
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            return (Element) n;
        }//from w w  w .j a v a2  s  .c om
    }
    return null;
}

From source file:Main.java

public static Document getOwnerDocument(Node node) {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        return (Document) node;
    } else {/*from  ww w . j  a  v  a 2s  .  co  m*/
        return node.getOwnerDocument();
    }
}

From source file:Main.java

/**
 * Get Element by according tag/*w  ww.  j  a  va 2 s. c o m*/
 * Only one such element should exist
 *
 * @param document document to search
 * @param tag tag to look for in document
 * @return corresponding element
 */
public static Element getSingleAppearingElementByTag(Document document, String tag) {
    NodeList elementList = document.getElementsByTagName(tag);
    if (elementList.getLength() != 1)
        return null;

    Node nNode = elementList.item(0);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) nNode;
    }
    return null;
}