Example usage for org.w3c.dom Node ELEMENT_NODE

List of usage examples for org.w3c.dom Node ELEMENT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node ELEMENT_NODE.

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

private static void printNote(NodeList nodeList, int depth) {
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
            System.out.println(depth + "Node Value =" + tempNode.getTextContent());
            if (tempNode.hasAttributes()) {
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                }/*from  w w w .j a va 2s.c om*/
            }
            if (tempNode.hasChildNodes()) {
                printNote(tempNode.getChildNodes(), depth + 1);
            }
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
        }
    }
}

From source file:Main.java

public static Element previousSiblingElement(Node node) {
    for (Node tempNode = node.getPreviousSibling(); tempNode != null; tempNode = tempNode
            .getPreviousSibling()) {/*from w  ww.  j  a  va2 s.  c o  m*/
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) tempNode;
        }
    }
    return null;
}

From source file:Main.java

private static void traverseNodes(Element parent, String tagName, List<Element> lst) {
    if (parent.getTagName().indexOf(tagName) != -1) {
        lst.add(parent);/* w w w .ja  v a 2s  .  c  om*/
        return;
    } else {
        NodeList nodeList = parent.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                traverseNodes((Element) item, tagName, lst);
            }
        }
    }
}

From source file:Main.java

public static List<Element> getElementsByTagNames(Element element, String[] tagNames) {
    List<Element> children = new ArrayList<Element>();
    if (element != null && tagNames != null) {
        List tagList = Arrays.asList(tagNames);
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && tagList.contains(((Element) child).getTagName())) {
                children.add((Element) child);
            }/*from  w w w . j  av  a2  s  . c o  m*/
        }
    }
    return children;
}

From source file:Main.java

public static String getTextContent(final Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default:/*from   www. ja v  a  2 s . c  o  m*/
        return null;
    }
}

From source file:Main.java

private static int findNodeIndex(Node node) {
    String nm = node.getLocalName();
    String ns = node.getNamespaceURI();
    short nt = node.getNodeType();

    Node parentNode = node.getParentNode();
    if (parentNode.getNodeType() != Node.ELEMENT_NODE)
        return 1;

    Node child = parentNode.getFirstChild();

    int ix = 0;/*www  .java  2  s .  com*/
    while (child != null) {
        if (child == node)
            return ix + 1;

        if (child.getNodeType() == nt && nm.equals(child.getLocalName())
                && ((ns == null && child.getNamespaceURI() == null)
                        || (ns != null && ns.equals(child.getNamespaceURI()))))
            ix++;

        child = child.getNextSibling();
    }

    throw new RuntimeException("Child node not found in parent!?");
}

From source file:Main.java

public static List getChildren(Element parentEl, String name) {
    List children = new ArrayList();
    NodeList l = parentEl.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);//from   w  w w .  j a v a2  s  . c  o  m
        if (Node.ELEMENT_NODE == n.getNodeType() && name.equals(n.getNodeName())) {
            children.add((Element) n);
        }
    }
    return children;
}

From source file:Main.java

public static Map<String, String> XmlAsMap(Node node) {
    Map<String, String> map = new HashMap<String, String>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.hasAttributes()) {
            for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
                Node item = currentNode.getAttributes().item(i);
                if (item != null)
                    map.put(item.getNodeName(), prepare(item.getTextContent()));
            }//w w w .  j  ava  2 s.co m
        }
        if (currentNode.getFirstChild() != null) {
            if (currentNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
                map.putAll(XmlAsMap(currentNode));
            } else if (currentNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                map.put(currentNode.getLocalName(), prepare(currentNode.getTextContent()));
            }
        }
    }
    return map;
}

From source file:Main.java

/** Takes XML node and prints to String.
 *
 * @param node Element to print to String
 * @return XML node as String/*from www . j  a v a2s . c  o  m*/
 */
private static void printNode(StringBuffer sBuffer, Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
        sBuffer.append("<![CDATA[");
        sBuffer.append(node.getNodeValue());
        sBuffer.append("]]>");
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) node;

        sBuffer.append("<").append(el.getTagName());

        NamedNodeMap attribs = el.getAttributes();

        for (int i = 0; i < attribs.getLength(); i++) {
            Attr nextAtt = (Attr) attribs.item(i);
            sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\"");
        }

        NodeList nodes = node.getChildNodes();

        if (nodes.getLength() == 0) {
            sBuffer.append("/>");
        } else {
            sBuffer.append(">");

            for (int i = 0; i < nodes.getLength(); i++) {
                printNode(sBuffer, nodes.item(i));
            }

            sBuffer.append("</").append(el.getTagName()).append(">");
        }
    }
}

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  ww  . j  av a  2s.co m*/
        }
    }

    return childElements;
}