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

/**
 * Remove <tt>xml:base</tt> attributes from the given node and all of its
 * descendants. According to section 1.3.4 of the DOM Level 3 Core
 * specification, when expanding an external entity reference, DOM parser
 * will add <tt>xml:base</tt> attributes to all elements appearing in the
 * external entity. These attributes don't appear in the reference output
 * files provided in the test suite (see
 * {@link XMLConformanceTest#getOutput()}). This method can be used to
 * remove <tt>xml:base</tt> attributes in DOM documents.
 * //from  ww w .  j a  v a 2  s  .  c  om
 * @param node
 *            the node to process
 */
public static void removeXmlBaseAttributes(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        element.removeAttribute("xml:base");
    }
    Node child = node.getFirstChild();
    while (child != null) {
        removeXmlBaseAttributes(child);
        child = child.getNextSibling();
    }
}

From source file:Main.java

public static List getChildrenElement(Node n) {
    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) {
            lst.add(cn);/*from w  ww . j a v  a  2s  .  c o m*/
        }
    }

    return lst;
}

From source file:Main.java

public static Element getChildElementByTagNameNS(Element parent, String tagName, String nsName) {
    NodeList nl = parent.getChildNodes();
    int size = nl.getLength();
    for (int i = 0; i < size; i++) {
        Node node = nl.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (tagName.equals(node.getLocalName())) {
                String ns = node.getNamespaceURI();
                if (ns != null && ns.equals(nsName)) {
                    return (Element) node;
                }/*w w w  .j  a v  a  2 s.  c o m*/
            }
        }
    }

    return null;
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);//from   www .  ja  v a 2  s  .c o  m
        }
    }
    return al.toArray(new Element[0]);
}

From source file:Main.java

public static Element[] getChildrenByName(Element e, String name) {
    NodeList nl = e.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(name)) {
            list.add(n);//from   ww w.j  av  a 2  s .  com
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

public static final List<Element> getChildElements(Element element) {
    List<Element> childElements = new ArrayList<Element>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) item);
        }//from w  ww . ja  va  2 s .  c  om
    }
    return childElements;
}

From source file:Main.java

public static Element getNextSiblingElement(Node node) {
    Node n = node.getNextSibling();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();/*from w ww  .j av  a2s .com*/
    }
    return (Element) n;
}

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:Main.java

/**
 * Gets <code>CDATASection</code> element for node.
 *
 * @param node node with text./*from   ww  w . j  a v  a 2 s  .  c  o m*/
 * @return text, that contains the specified node.
 */
public static String getNodeCDATASection(final Node node) {
    //        node.normalize();
    final NodeList list = node.getChildNodes();
    for (int i = 0, len = list.getLength(); i < len; i++) {
        final Node child = list.item(i);
        if (child.getNodeType() == Node.CDATA_SECTION_NODE)
            return child.getNodeValue();
    }
    return null;
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search/*from  ww  w.ja  va  2 s  .  co  m*/
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementIgnoreCase(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

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

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equalsIgnoreCase(element)) {
                return n;
            }
        }
    }

    return null;
}