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

/** Decide if the node is text, and so must be handled specially */
public static boolean isTextNode(final Node n) {
    if (n == null) {
        return false;
    }/*from  w  ww  .  java2  s  .c  o m*/
    short nodeType = n.getNodeType();
    return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
}

From source file:Main.java

/**
 * Returns the next Element node.//from  w  w w.  j a  v  a 2 s . c  o m
 */
private static Element getNextElementNode(Node node) {
    while (node != null) {
        if (Node.ELEMENT_NODE == node.getNodeType())
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static CDATASection addCDataText(Node node, String data) {
    Document doc = null;/* w ww . j a v  a 2  s . c  o  m*/
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        doc = (Document) node;
    } else {
        doc = node.getOwnerDocument();
    }
    CDATASection e = doc.createCDATASection(data);
    node.appendChild(e);
    return e;
}

From source file:Main.java

private static void extractText(Node n, StringBuffer buf) {
    if (n.getNodeType() == Node.TEXT_NODE) {
        buf.append(n.getNodeValue());/*w  w  w. ja va2s  .  c  o m*/
    }

    for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
        extractText(n, buf);
    }
}

From source file:Main.java

public static String path(Node n) {
    if (n == null)
        return "";
    switch (n.getNodeType()) {
    case Node.ATTRIBUTE_NODE: {
        return path(n.getParentNode()) + "/@" + n.getNodeName();
    }/*from www  .  ja  v a 2 s.c om*/
    case Node.TEXT_NODE: {
        return path(n.getParentNode()) + "/text()";
    }
    case Node.CDATA_SECTION_NODE: {
        return path(n.getParentNode()) + "/cdata()";
    }
    case Node.ELEMENT_NODE: {
        return path(n.getParentNode()) + "/" + n.getNodeName();
    }
    case Node.DOCUMENT_NODE: {
        return "";
    }
    default: {
        return "";
    }
    }
}

From source file:Main.java

/**
 * Search all child nodes of the given for the first element that has the
 * specified tag name./*from   w  w  w .  ja  v a2s  . c om*/
 *
 * @param aStartNode
 *        The parent element to be searched. May not be <code>null</code>.
 * @param sName
 *        The tag name to search.
 * @return <code>null</code> if the parent element has no such child element.
 */
@Nullable
public static Element getFirstChildElementOfName(@Nonnull final Node aStartNode, @Nullable final String sName) {
    final NodeList aNodeList = aStartNode.getChildNodes();
    final int nLen = aNodeList.getLength();
    for (int i = 0; i < nLen; ++i) {
        final Node aNode = aNodeList.item(i);
        if (aNode.getNodeType() == Node.ELEMENT_NODE) {
            final Element aElement = (Element) aNode;
            if (aElement.getTagName().equals(sName))
                return aElement;
        }
    }
    return null;
}

From source file:Main.java

/**
 * This method returns a list of the direct element node children of this element node with the specified tag.
 * @param node - parent node//ww w  . jav  a2  s . c om
 * @param tag - tag of direct children to be returned
 * @return a list containing the direct element children with the given tag
 * @author Tristan Bepler
 */
public static List<Element> getDirectChildElementsByTag(Element node, String tag) {
    List<Element> children = new ArrayList<Element>();
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE
                && (child.getNodeName().equals(tag) || tag.equals(DOM_WILDCARD))) {
            children.add((Element) child);
        }
        child = child.getNextSibling();
    }
    return children;
}

From source file:Main.java

public static Element getNextSiblingElement(Node node) {
    if (node == null)
        return null;
    Node nextSibling = node.getNextSibling();
    while ((nextSibling != null) && (nextSibling.getNodeType() != Node.ELEMENT_NODE))
        nextSibling = nextSibling.getNextSibling();
    if ((nextSibling != null) && (nextSibling.getNodeType() == Node.ELEMENT_NODE))
        return (Element) nextSibling;
    return null;//from  w  w  w .  j  a v a  2s  .co  m
}

From source file:Main.java

public static String readProjectName(String file) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    InputStream is = new FileInputStream(file);
    Document doc = dombuilder.parse(is);
    Element root = doc.getDocumentElement();
    NodeList prjInfo = root.getChildNodes();
    if (prjInfo != null) {
        for (int i = 0; i < prjInfo.getLength(); i++) {
            Node project = prjInfo.item(i);
            if (project.getNodeType() == Node.ELEMENT_NODE) {
                String strProject = project.getNodeName();
                if (strProject.equals("project")) {
                    for (Node node = project.getFirstChild(); node != null; node = node.getNextSibling()) {
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                            String strNodeName = node.getNodeName();
                            if (strNodeName.equals("name")) {
                                return node.getTextContent();
                            }/*from  www. j a  v  a  2  s  . c  o m*/
                        }
                    }
                }
            }

        }
    }
    return "";
}

From source file:Main.java

/**
 * returns list of child Element Nodes for a given, parsed XML node
 * /*  www. j  a  v a  2 s . com*/
 * @param xmlNode parsed XML Node for which child Nodes are to be returned
 * @return List of child Element Nodes of xmlNode
 **/
public static final List<Element> getChildElementNodes(final Node xmlNode) {
    final NodeList children = xmlNode.getChildNodes();
    if (children == null) {
        return null;
    }
    final int childrenCnt = children.getLength();
    final List<Element> list = new ArrayList<Element>(childrenCnt);

    for (int i = 0; i < childrenCnt; i++) { // Then loop through them looking for search criteria node
        final Node childNode = children.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            list.add((Element) childNode);
        }
    }

    return list;
}