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

/**
 * Traverses through a DOM tree starting at the given element and removes all those text-nodes from it that only
 * contain white spaces./*from   ww w .j  ava2  s  .c om*/
 * 
 * @param parent
 *          the parent Element
 */
public static void removeWhitespaceTextNodes(Element parent) {
    final NodeList nl = parent.getChildNodes();

    for (int i = 0; i < nl.getLength(); i++) {
        final Node child = nl.item(i);

        if (child.getNodeType() == Node.TEXT_NODE) {
            if (child.getNodeValue().trim().length() == 0) {
                parent.removeChild(child);
                i--; // since the child is removed counting up must be made undone
            }
        } else if (child.getNodeType() == Node.ELEMENT_NODE && child.getChildNodes().getLength() > 0) {
            removeWhitespaceTextNodes((Element) child);
        }
    }
}

From source file:Main.java

/**
 * Returns an iterator over the children of the given element with
 * the given tag name./* www. ja v  a2 s .  com*/
 * @param element    The parent element
 * @param tagName    The name of the desired child
 * @return           An interator of children or null if element is null.
 */
public static Iterator<Element> getChildrenByTagName(Element element, String tagName) {
    if (element == null)
        return null;
    // getElementsByTagName gives the corresponding elements in the whole 
    // descendance. We want only children
    NodeList children = element.getChildNodes();
    ArrayList<Element> goodChildren = new ArrayList<>();
    for (int i = 0; i < children.getLength(); i++) {
        Node currentChild = children.item(i);
        if (currentChild.getNodeType() == Node.ELEMENT_NODE
                && ((Element) currentChild).getTagName().equals(tagName)) {
            goodChildren.add((Element) currentChild);
        }
    }
    return goodChildren.iterator();
}

From source file:Main.java

public static void createAttribute(Document doc, String nodeName, Map<String, String> map) {
    Node node = doc.getElementsByTagName(nodeName).item(0);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            element.setAttribute(entry.getKey(), entry.getValue());
        }/*from  www  . j a v a 2s .  c  om*/
        //TransformerFactory factory = TransformerFactory.newInstance();
        //Transformer former = factory.newTransformer();
        //former.transform(new DOMSource(doc), new StreamResult(new File("src/shuiguo.xml")));
    }
}

From source file:Utils.java

/**
 * <p>Returns a list of child elements with the given
 * name. Returns an empty list if there are no such child
 * elements.</p>//from  ww w.java  2 s  .  co  m
 *
 * @param parent parent element
 * @param name name of the child element
 * @return child elements
 */
public static List getChildElementsByName(Element parent, String name) {
    List elements = new ArrayList();

    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getTagName().equals(name)) {
                elements.add(element);
            }
        }
    }

    return elements;
}

From source file:Main.java

private static List<Node> filter(final NodeList nl, final short[] typesToFilter) {
    List<Node> nodes = new ArrayList<Node>();

    outer: for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        short type = n.getNodeType();
        for (int j = 0; j < typesToFilter.length; j++) {
            if (typesToFilter[j] == type) {
                continue outer;
            }/*from   w  w w  .  j  a va  2 s. c  o  m*/
        }
        nodes.add(n);
    }
    return nodes;
}

From source file:Main.java

/**
 * @return a List of child Elements/* w  w  w. j ava  2  s.  c  o  m*/
 */
public static List<Element> getChildren(Element element) throws Exception {
    NodeList nodes = element.getChildNodes();
    ArrayList<Element> ret = new ArrayList<Element>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            ret.add((Element) childNode);
        }
    }
    return ret;
}

From source file:Utils.java

/**
 * Get the first child of the specified type.
 * //from w  ww.  j a v  a2  s .  com
 * @param parent
 * @param type
 * @return
 */
public static Node getChild(Node parent, int type) {
    Node n = parent.getFirstChild();
    while (n != null && type != n.getNodeType()) {
        n = n.getNextSibling();
    }
    if (n == null) {
        return null;
    }
    return n;
}

From source file:Main.java

public static String getTextValue(Element e) {
    NodeList childs = e.getChildNodes();
    for (int k = 0; k < childs.getLength(); k++) {
        Node n = childs.item(k);
        if (n.getNodeType() == 3) {
            return n.getNodeValue();
        }//from ww w .  j  ava 2 s .  c  o  m
    }
    return null;
}

From source file:Main.java

public static ArrayList<Element> getChildElements(Element parent, String childName) {
    ArrayList<Element> childElements = new ArrayList<Element>();
    NodeList childNodes = parent.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (childName == null || childNode.getNodeName().equals(childName)) {
                childElements.add((Element) childNode);
            }//from  w  w  w.  ja  v  a  2 s. c  o m
        }
    }

    return childElements;
}

From source file:Main.java

public static List<Element> getChildren(Element parent) {

    NodeList children = parent.getChildNodes();

    final int length = children.getLength();

    ArrayList<Element> elements = new ArrayList<Element>();

    for (int index = 0; index < length; index++) {

        Node node = children.item(index);

        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }//from   w w w .  j a v  a  2 s.  c o  m

        elements.add((Element) node);
    }

    return elements;
}