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

/**
 * Returns the occurrences of a given node (specified by its name)
 * in a given list of nodes. Counts all subsequent occurs after the
 * first one./*  w ww  .  j  av  a  2s .c  o m*/
 * 
 * @param nodeName The name of the node whose occurrences are requested
 * @param nodes      The list of nodes that will be searched for occurrences of
 *                nodeName.
 * @return         The number of the first subsequent occurences of nodeName. 
 *                Greater than or equal to 0.
 */
public static int getOccurs(String nodeName, NodeList nodes) {
    int occurs = 0;

    int childNr = 0;
    boolean foundFirstOccurence = false;
    while (childNr < nodes.getLength()) {
        Node node = nodes.item(childNr);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().compareTo(nodeName) == 0) {
                occurs++;
                foundFirstOccurence = true;
            } else {
                if (foundFirstOccurence) {
                    return occurs;
                }
            }
        }
        childNr++;
    }

    return occurs;
}

From source file:Main.java

private static void autoQualify(Document document, Element element, Map<String, String> namespaces,
        String parentPrefix) {//from w  ww.  ja  v  a  2s  .  c  om
    String localName = element.getLocalName();
    element = renameElement(document, element, localName, namespaces.get(parentPrefix));
    String prefix = parentPrefix;
    if (namespaces.containsKey(localName.toLowerCase())) {
        prefix = localName;
    } else if (genericBranchCoverageTypes.contains(localName.toLowerCase())) {
        prefix = GENERIC_BRANCH_COVERAGE;
    }
    NodeList nodes = element.getChildNodes();
    for (int i = 0, iEnd = nodes.getLength(); i < iEnd; ++i) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            autoQualify(document, (Element) node, namespaces, prefix);
        }
    }
}

From source file:Main.java

/**
 * Check if the element contains just a text/cdata, in which case return
 * that value. Else return null;/*w  ww . j a  va  2  s  . com*/
 *
 * @param element
 * @return null if this is not a textElement as we see it. Value of single
 *         text/CData child otherwise
 */
private static String getElementValue(Element element) {
    if (element.getAttributes().getLength() > 0) {
        return null;
    }

    NodeList children = element.getChildNodes();
    String value = null;
    int nbrChildren = children.getLength();
    for (int i = 0; i < nbrChildren; i++) {
        Node child = children.item(i);
        short childType = child.getNodeType();
        if (childType == Node.ELEMENT_NODE) {
            return null;
        }
        if (childType == Node.CDATA_SECTION_NODE || childType == Node.TEXT_NODE) {
            if (value != null) {
                return null;
            }
            value = child.getNodeValue();
        }
    }
    return value;
}

From source file:Main.java

/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(Node parent, String elemNames[]) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                if (child.getNodeName().equals(elemNames[i])) {
                    return (Element) child;
                }/* w ww  .j a  v a2  s.  com*/
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static void setByPath(Document doc, String path, Node in) {
    Node node = getNodeByPath(doc, path);
    if (in.getNodeType() == Node.DOCUMENT_NODE) {
        in = in.getFirstChild();/*from  w ww.  j  av  a  2  s .  c o m*/
    }
    Node newNode = doc.importNode(in, true);
    node.getParentNode().replaceChild(newNode, node);
}

From source file:Main.java

/**
 * Finds and returns the first child node with the given name and
 * attribute name, value pair./*from   w  w  w.  java  2s  .c  o m*/
 */
public static Element getFirstChildElement(Node parent, String elemName, String attrName, String attrValue) {

    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) child;
            if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) {
                return element;
            }
        }
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * For the given {@code sourceNode}, read each "top level" element into the resulting {@code Map}. Each element name
 * is a key to the map, each element value is the value paired to the key. Example - anchor is the node, label and
 * href are keys://from w  w w .  j  ava  2s  .c  o  m
 *
 * <pre>
 * {@code
 * <anchor>
 *  <label>Slashdot</label>
 *    <href>http://slashdot.org/</href>
 *  </anchor>
 * }
 * </pre>
 */
public static Map<String, String> readNodeElementsToMap(final Node sourceNode) {
    Map<String, String> result = new HashMap<String, String>();

    if (sourceNode == null) {
        return result;
    }

    NodeList childNodes = sourceNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node element = childNodes.item(i);

        if (element.getNodeType() == Node.ELEMENT_NODE) {
            String elementName = element.getNodeName();
            String elementValue = "";
            Node firstChild = element.getFirstChild();

            if (firstChild != null) {
                elementValue = firstChild.getNodeValue();
            }

            if (elementValue != null) {
                result.put(elementName, elementValue);
            }
        }
    }

    return result;
}

From source file:Main.java

public static void setText(Element element, String Text) {
    Node node;
    node = element.getFirstChild();//from w w w  .j av  a 2s  .c om
    while (null != node) {
        if (Node.TEXT_NODE == node.getNodeType()) {
            Text text = (Text) node;
            text.setData(Text);
            return;
        }
    }
    Text text = element.getOwnerDocument().createTextNode(Text);
    element.appendChild(text);
}

From source file:Main.java

/** Finds and returns the first child node with the given name. */
public static Element getFirstChildElement(Node parent, String elemNames[]) {

    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                if (child.getNodeName().equals(elemNames[i])) {
                    return (Element) child;
                }/*from  ww  w . j a  v  a  2 s.c om*/
            }
        }
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Finds and returns the last child node with the given name and
 * attribute name, value pair./*ww  w  . j  av  a2  s  .c o m*/
 */
public static Element getLastChildElement(Node parent, String elemName, String attrName, String attrValue) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) child;
            if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) {
                return element;
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}