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

/**
 * Filter node list for Element nodes of specified name.
 *//*www. j a  v  a 2s  . c o m*/
public static List<Node> filterNodeListElements(NodeList nodeList, String nodeName) {
    List<Node> nodes = new ArrayList<Node>();
    for (int k = 0; k < nodeList.getLength(); k++) {
        Node node = nodeList.item(k);
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (nodeName != null && (node.getNodeName().equals(nodeName) == false)) {
            continue;
        }
        nodes.add(node);
    }
    return nodes;
}

From source file:Main.java

public static List<Element> getNamedChildElements(Element parent, String name) {
    List<Element> elements = new ArrayList<Element>();
    if (parent != null) {
        Node child = parent.getFirstChild();
        while (child != null) {
            if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(name))) {
                elements.add((Element) child);
            }//from w ww  .j a  va  2s  .  com
            child = child.getNextSibling();
        }
    }
    return elements;
}

From source file:Main.java

/**
 * @return a list of all child node text contents with this name
 *//*  w w w  .  j  av  a 2  s  .c om*/
public static ArrayList<String> getChildNodesTextContents(Node node, String name) {
    ArrayList<String> results = new ArrayList<String>();

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName())) {
            results.add(temp.getTextContent());
        }
    }

    return (results);
}

From source file:Main.java

public static Element getFirstChildElementNS(Element elm, String tns, String localName) {
    if (tns == null && localName == null)
        return getFirstChildElement(elm);

    if (tns == null)
        return getFirstChildElement(elm, localName);

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (localName == null && tns.equals(node.getNamespaceURI()))
            return (Element) node;

        if (localName != null && tns.equals(node.getNamespaceURI()) && localName.equals(node.getLocalName()))
            return (Element) node;
    }/* www . j a v  a  2s .  co  m*/

    return null;
}

From source file:Main.java

public static Element getFirstChildElement(Node node) {
    Node child = node.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
        child = child.getNextSibling();/*from w  w w  . j  a  va2 s. c om*/
    }
    return (Element) child;
}

From source file:Main.java

public static void updateUserMgtXML(File userMgtXML, String jndiConfigNameUserDB) throws Exception {
    Document doc = initializeXML(userMgtXML);
    NodeList configNodeList = doc.getElementsByTagName("Configuration");
    Node configNode = configNodeList.item(0); //get the 0 index, since only one available

    NodeList nodeList = configNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equalsIgnoreCase("Property")) {
                if (node.hasAttributes()) {
                    NamedNodeMap namedNodeMap = node.getAttributes();
                    Node attr = namedNodeMap.getNamedItem("name");
                    if (attr != null && attr.getNodeValue().equalsIgnoreCase("dataSource")) {
                        node.setTextContent(jndiConfigNameUserDB);
                    }//w  ww  .j  a v a2 s.  c o  m
                }
            }
        }
    }
    finalizeXML(userMgtXML, doc, 4);
}

From source file:Main.java

/**
 * Remove any whitespace text nodes from the DOM. Calling this before saving
 * a formatted document will fix the formatting indentation of elements
 * loaded from a different document.// w  ww  . ja  va2s. c  o  m
 * 
 * @param node
 *            Node to remove whitespace nodes from
 * @param deep
 *            Should this method recurse into the node's children?
 */
public static void removeWhitespace(Node node, boolean deep) {
    NodeList children = node.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE && length > 1) {
            Node previous = child.getPreviousSibling();
            Node next = child.getNextSibling();
            if ((previous == null || previous.getNodeType() == Node.ELEMENT_NODE
                    || previous.getNodeType() == Node.COMMENT_NODE)
                    && (next == null || next.getNodeType() == Node.ELEMENT_NODE
                            || next.getNodeType() == Node.COMMENT_NODE)) {
                String content = child.getTextContent();
                if (content.matches("\\s*")) //$NON-NLS-1$
                {
                    node.removeChild(child);
                    i--;
                    length--;
                }
            }
        } else if (deep && child.getNodeType() == Node.ELEMENT_NODE) {
            removeWhitespace(child, deep);
        }
    }
}

From source file:Main.java

/** Look for Element node of given name.
 *
 *  <p>Checks the node itself and its siblings for an {@link Element}.
 *  Does not descent down the 'child' links.
 *
 *  @param node Node where to start.//w  ww  .  jav  a2  s .  c  o m
 *  @param name Name of the node to look for.
 *  @return Returns node, the next matching sibling, or <code>null</code>.
 */
private static final Element findElementByName(Node node, final String name) {
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name))
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static String getTextContent(Node node, boolean trim) {
    if (node == null) {
        return "";
    }//from  ww  w. j  ava 2s  .  co m

    String textContent = "";
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            textContent += childNode.getNodeValue().trim();
        }
    }

    textContent = textContent.replace("\r", "");
    if (textContent.startsWith("\n")) {
        textContent = textContent.substring(1);
    }

    if (trim) {
        textContent = textContent.trim();
    }

    return textContent;
}

From source file:Main.java

public static Node getPreviousSiblingElement(Node node) {
    if (node == null)
        return null;
    Node prevSibling = node.getPreviousSibling();
    while ((prevSibling != null) && (prevSibling.getNodeType() != Node.ELEMENT_NODE))
        prevSibling = prevSibling.getPreviousSibling();
    if ((prevSibling != null) && (prevSibling.getNodeType() == Node.ELEMENT_NODE))
        return prevSibling;
    return null;//from  w  ww .j a  v  a2s . co m
}