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 Node getTag(Element root, String tagName) {
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        System.out.println("nodeName : " + node.getNodeName() + ":::::" + node.getNodeType());
        if (node.getNodeName().equals(tagName)) {
            return node;
        }//from   w w w . j a v  a  2s . c o m
    }
    return null;
}

From source file:Main.java

public static boolean isSpacePreserved(Node node) {
    if (node == null) {
        return false;
    }//www  .  j a v  a  2s  .  c o  m
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        final String spaceAttr = ((Element) node).getAttributeNS(XMLConstants.XML_NS_URI, "space");
        if (spaceAttr != null) {
            return ("preserve".equals(spaceAttr));
        }
    }
    return isSpacePreserved(node.getParentNode());
}

From source file:Main.java

private static Map<?, ?> getNodeBean(Node parent) {
    Map<Object, Object> rtn = new HashMap<Object, Object>();

    if (parent != null) {
        Map<Object, Object> attrMap = new HashMap<Object, Object>();
        if (parent.hasAttributes()) {
            NamedNodeMap attrs = parent.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                attr.getNodeName();/*from   w  w  w  . j  a v  a  2  s. c om*/
                attr.getNodeValue();
                attrMap.put(attr.getNodeName(), attr.getNodeValue());
            }
        }

        rtn.put("tagName", parent.getNodeName());
        rtn.put("attr", attrMap);

        NodeList nodeList = parent.getChildNodes();
        if (nodeList != null) {
            List<Object> children = new ArrayList<Object>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    children.add(getNodeBean(child));
                }
            }

            rtn.put("children", children);
        }
    }

    return rtn;
}

From source file:Main.java

public static Element getFirstChildElement(Element parentElem) {
    if (parentElem == null) {
        return null;
    }//  w ww. j  av  a2  s  .c  om
    Node child = parentElem.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 *   Find the first child node matching the given tag name. Only searches 
 *   1st-level children; not a recursive search. Null if not found.
 */// ww  w.j av  a 2 s.c om
public static Node findChildNodeMatching(Node root, String tagName, short nodeType) {
    Node childNode = root.getFirstChild();
    while (childNode != null) {
        if (childNode.getNodeType() == nodeType) {
            if (tagName.equals(childNode.getNodeName())) {
                return childNode;
            }
        }

        childNode = childNode.getNextSibling();
    }

    return null;
}

From source file:Main.java

protected static void getTextFromNode(Node node, StringBuffer buffer, boolean addSpace) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        buffer.append(node.getNodeValue());
        if (addSpace)
            buffer.append(" ");
    }//  w w w.  j  a v  a 2s  .com
    Node child = node.getFirstChild();
    while (child != null) {
        getTextFromNode(child, buffer, addSpace);
        child = child.getNextSibling();
    }
}

From source file:Main.java

static public String getNodeAttr(String tagName, String attrName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (data.getNodeName().equalsIgnoreCase(attrName)) {
                        return data.getNodeValue();
                    }//www.j a  v  a2 s . co m
                }
            }
        }
    }

    return "";
}

From source file:Main.java

/**
 * Returns an iterator over the children of the given element with
 * the given tag name.// w  ww.  j a v a  2  s  .  c  o m
 *
 * @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 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 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(currentChild);
        }
    }
    return goodChildren.iterator();
}

From source file:Main.java

public static Element getFirstChildElementByLocalName(Element parentElem, String localName) {
    Node child = parentElem.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && getLocalName(child).equals(localName)) {
            return (Element) child;
        }//from   w  w w. j a va2s  .  co m
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

/** Finds and returns the first child element node. */
public static Element getFirstChildElement(Node parent) {

    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }//from  ww w  .ja  v  a2 s  .co  m
        child = child.getNextSibling();
    }

    // not found
    return null;

}