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

protected static Element getChildElement(Element element, String tagName) {
    NodeList childNodes = element.getChildNodes();
    int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }//w w  w.  j  a va  2 s  . c  o  m
        Element childElement = (Element) childNode;

        if (tagName != null) {
            String childTagName = childElement.getTagName();
            if (!childTagName.equals(tagName)) {
                continue;
            }
        }
        return childElement;
    }
    return null;
}

From source file:Main.java

/**
 * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method
 *//*w  ww  .  j  av  a 2  s .  c  om*/
public static String getTextContent(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:
        return null;
    }
}

From source file:Main.java

/**
 * Obtains the list of child element nodes.
 *
 * @param element the root element./*from ww  w  . j a  v a 2 s.c o  m*/
 * @return the list of child elements.
 */
private static List<Element> getElementList(final Element element) {
    final List<Element> elementList = new ArrayList<Element>();

    final NodeList childNodes = element.getChildNodes();
    final int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        final Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        elementList.add((Element) childNode);
    }

    return elementList;
}

From source file:Main.java

/**
 * getNodeText/* w  ww.  j  a  va  2  s. com*/
 * @param n node
 * @return text node content
 */
public static String getNodeText(Node n) {
    NodeList nl = n.getChildNodes();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);
        if (cn.getNodeType() == Node.TEXT_NODE) {
            Text txt = (Text) cn;
            sb.append(txt.getData().trim());
        }
    }

    return sb.toString();
}

From source file:Main.java

static public Element selectSingleElement(Element element, String xpathExpression)
        throws XPathExpressionException {
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                return (Element) node;
            }/*from   ww w  .j av  a 2s.c  om*/
        }
        //  NodeList nodes = element.getElementsByTagName(xpathExpression);
        //  if (nodes.getLength() > 0) {
        //      return (Element) nodes.item(0);
        //  } else {
        return null;
        //  }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE);
        return node;
    }
}

From source file:Main.java

/**
 * @param n// ww  w.  j  a va2s. c  o  m
 *            the node
 * @return the first element in the ancestor tree of {@code n}. If
 *         {@code n} is an {@link Element}, {@code n} is returned. If
 *         {@code n} is <code>null</code>, this method returns
 *         <code>null</code>.
 */
public static Element getAncestorElement(Node n) {
    if (n == null) {
        return null;
    }
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) n;
    }
    Node parent = n.getParentNode();
    if (parent == null) {
        Document doc = (Document) (n instanceof Document ? n : n.getOwnerDocument());
        return doc == null ? null : doc.getDocumentElement();
    }
    return getAncestorElement(parent);
}

From source file:Main.java

public static Element getFirstChildElementNS(Element elm, String tns, String localName) {
    if (tns == null && localName == null)
        return getFirstChildElement(elm);

    if (tns == null || tns.length() == 0)
        return getFirstChildElement(elm, localName);

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (localName == null && tns.equals(node.getNamespaceURI()))
            return (Element) node;

        if (localName != null && tns.equals(node.getNamespaceURI()) && localName.equals(node.getLocalName()))
            return (Element) node;
    }/*from  w w  w  . j a  v  a2s  . co  m*/

    return null;
}

From source file:Main.java

/**
 * Returns an iterator over the children of the given element with
 * the given tag name.//from w  w  w .ja v a  2 s.com
 *
 * @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<Element> 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<Element> goodChildren = new ArrayList<Element>();
    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((Element) currentChild);
        }
    }
    return goodChildren.iterator();
}

From source file:Main.java

public static Comment addComment(Node node, String comment) {
    Document doc = null;/*from   ww  w .j a v a  2  s.  com*/
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        doc = (Document) node;
    } else {
        doc = node.getOwnerDocument();
    }
    Comment e = doc.createComment(comment);
    node.appendChild(e);
    return e;
}

From source file:Main.java

public static String getText(Element paramElement) {
    NodeList localNodeList = paramElement.getChildNodes();
    int i = localNodeList.getLength();
    for (int j = 0; j < i; ++j) {
        Node localNode = localNodeList.item(j);
        if (localNode.getNodeType() == 3)
            return localNode.getNodeValue();
    }/*ww  w.  j ava 2 s  .co m*/
    return "";
}