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

/**
 * Method to convert a Node XML to a Element XML.
 * @param node node XML to input./*  ww  w.j a v a 2s .  c  om*/
 * @return elment XML.
 */
public static Element convertNodeToElement(Node node) {
    NodeList list = node.getChildNodes();
    Element m = null;
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        //Node n = elem.getFirstChild();
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            m = (Element) n;
        }
    }
    return m;
}

From source file:Main.java

public static String getValueXPath(String srcXmlString, String xPath) {
    String value = null;//  www.  j  a  va2s  . c om
    try {
        Object result = execXpathGetNode(srcXmlString, xPath);
        Node node = (Node) result;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            value = node.getTextContent();
        } else {
            value = node.getNodeValue();
        }
        logger.debug(xPath + " = " + value);
    } catch (Exception ex) {
        logger.error(ex.getMessage() + " Could not extract any value using xpath: " + xPath);
    }
    return value;
}

From source file:Main.java

protected static List<Element> createElementList(Element element) {
    List<Element> elementList = new ArrayList<>();

    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  .  jav a 2  s  .  co  m*/
        elementList.add((Element) childNode);
    }

    return elementList;
}

From source file:Main.java

/**
 * Get the first child Element of the supplied node.
 *
 * @param node The DOM Node.//ww w. j  av  a 2 s  .c  o m
 * @return The first child element
 */
public static Element getFirstChildElement(Node node) {
    NodeList children = node.getChildNodes();
    int childCount = children.getLength();

    for (int i = 0; i < childCount; i++) {
        Node child = children.item(i);
        if (child != null && child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }
    }
    return null;
}

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 w  ww.  j a  v  a  2 s.  co m
        return null;
    }
}

From source file:Main.java

/**
 * /*from w ww  .j a  va 2  s  .c om*/
 * @param context
 * @param element
 */
public static void recursiveIdBrowse(DOMValidateContext context, Element element) {
    for (int i = 0; i < element.getChildNodes().getLength(); i++) {
        Node node = element.getChildNodes().item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element childEl = (Element) node;
            String ID_ATTRIBUTE_NAME = "Id";
            if (childEl.hasAttribute(ID_ATTRIBUTE_NAME)) {
                context.setIdAttributeNS(childEl, null, ID_ATTRIBUTE_NAME);
            }
            recursiveIdBrowse(context, childEl);
        }
    }
}

From source file:Main.java

public static List<Node> getTextAndElementChildren(Node node) {
    List<Node> result = new LinkedList<Node>();
    NodeList children = node.getChildNodes();
    if (children == null) {
        return result;
    }/*  ww w .  ja v a  2  s .  co  m*/
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (Node.ELEMENT_NODE == child.getNodeType() || Node.TEXT_NODE == child.getNodeType()) {
            result.add(child);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Returns an array containing the element objects in the provided node list.
 * //w ww  .ja v  a2 s .com
 * @param nodeList The DOM node objects to extract elements from.
 * @return The array of DOM elements found in the node list.
 */
@SuppressWarnings("unchecked")
public static Element[] getElementsOfNodeList(NodeList nodeList) {
    Element[] ret = null;
    @SuppressWarnings("rawtypes")
    Vector v = new Vector();

    for (int n = 0; n < nodeList.getLength(); n++) {
        Node item = nodeList.item(n);

        if (item.getNodeType() == Node.ELEMENT_NODE) {
            v.addElement(item);
        }
    }

    ret = new Element[v.size()];

    for (int n = 0; n < ret.length; n++) {
        ret[n] = (Element) v.elementAt(n);
    }

    return ret;
}

From source file:Main.java

private static void walkNodes(Node nodeIn, HashSet<String> hElements) {
    if (nodeIn == null)
        return;/*from  www  . j  a v a 2  s  .  co  m*/
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            String sNodeName = n.getNodeName();
            if (!hElements.contains(sNodeName))
                hElements.add(sNodeName);
            walkNodes(n, hElements);
        }
    }
}

From source file:Main.java

public static String getText(final Node node) {
    final StringBuilder result = new StringBuilder();
    if (!node.hasChildNodes())
        return "";

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node subnode = list.item(i);
        if (subnode.getNodeType() == Node.TEXT_NODE) {
            result.append(subnode.getNodeValue());
        } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) {
            result.append(subnode.getNodeValue());
        } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
            // Recurse into the subtree for text
            // (and ignore comments)
            result.append(getText(subnode));
        }/*from   www.  j  a  v a2  s. c  o  m*/
    }

    return result.toString();
}