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 Map<String, String> SimpleDocumentToMap(Document doc) {
    Map<String, String> simpleMap = new HashMap<String, String>();
    //trim off outter layer
    Node node = doc.getFirstChild();
    NodeList nList = node.getChildNodes();
    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

public static List<Element> getElements(final Element element, final String tagName) {
    final ArrayList<Element> elements = new ArrayList<>();
    final NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            final String nodeName = node.getNodeName();
            if ((nodeName != null) && nodeName.equals(tagName)) {
                elements.add((Element) node);
            }//from   ww w .  j  a va 2 s .c om
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Get the first child element of the specified element, null if it has no child elements.
 * /* ww  w  . j ava  2s. co  m*/
 * @param root The element to search.
 * @return The first child element, null if it has none.
 */
public static Element getFirstChild(Element root) {
    if (root == null)
        return null;
    NodeList lst = root.getChildNodes();
    final int n = lst.getLength();
    for (int i = 0; i < n; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildElements(Element parentElement) {
    NodeList nodeList = parentElement.getChildNodes();
    List<Element> childElements = new ArrayList<Element>(nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(0);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) childNode);
        }/*from   w w  w .  j a  v  a2  s. c  o  m*/
    }
    return childElements;
}

From source file:Main.java

public static Element getFirst(NodeList nodes) {
    if (nodes == null || nodes.getLength() == 0) {
        return null;
    }/*from www  .ja  va2 s .  co m*/

    Node node = nodes.item(0);
    if (node.getNodeType() != Node.ELEMENT_NODE) {
        return null;
    }

    return (Element) node;
}

From source file:Main.java

public static void removeAllSubNodesExceptAttributes(Node n) {
    NodeList childNodes = n.getChildNodes();
    List<Node> childNodesToRemove = new ArrayList<Node>();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node c = childNodes.item(i);

        if (c.getNodeType() != Node.ATTRIBUTE_NODE) {
            childNodesToRemove.add(c);//from w w w  .j  a v  a  2s . co  m
        }
    }

    for (Node c : childNodesToRemove) {
        n.removeChild(c);
    }
}

From source file:Main.java

static public String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE) {
            return data.getNodeValue();
        }/*from w  w w  . j  ava  2 s . c  o m*/
    }
    return "";
}

From source file:Main.java

public static void getInnerText(StringBuilder sb, Element elt, String separator) {
    NodeList tns = elt.getChildNodes();
    for (int j = 0; j < tns.getLength(); j++) {
        Node tn = tns.item(j);
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        } else if (tn.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(separator);/*  w w w  . j  a  va 2s. com*/
            getInnerText(sb, (Element) tn, separator);
            sb.append(separator);
        }
    }
}

From source file:Main.java

public static ArrayList<Element> getElementsByTagName(Node node, String tagName) {
    ArrayList<Element> elements = new ArrayList<Element>();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node n = node.getChildNodes().item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            elements.add((Element) n);
        }//from  w w  w.  j a  v a 2 s  .c o  m
    }
    return elements;
}

From source file:Main.java

private static void findAllProcessingIstructions(Node node, String name, List<ProcessingInstruction> result) {
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return;/*ww  w  . j a  v  a 2  s  .  com*/
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
            if (name == null || name.length() == 0 || n.getNodeName().equals(name)) {
                result.add((ProcessingInstruction) n);
            }
        }
        findAllProcessingIstructions(n, name, result);
    }
}