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 String cdataValue(Element element) {
    if (element == null)
        return null;
    // get the first element with the given name
    Node node = element.getFirstChild();

    if (node != null) {
        do {/* w  w w  .  j a v  a 2s  .co  m*/
            if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
                return node.getNodeValue();
            }
        } while ((node = node.getNextSibling()) != null);
    }
    return null;
}

From source file:Main.java

/**
 * Get all child elements of the specified (root) element. If the root is null, an illegal argument exception is
 * thrown. If the root has no children, an empty array is returned.
 * /*  w w  w.jav a  2 s  .  c o m*/
 * @param root The element to search.
 * @return An array of all child elements of the root.
 */
public static ArrayList<Element> getElements(Element root) {
    if (root == null)
        throw new IllegalArgumentException("Null or invalid root element!");
    NodeList lst = root.getChildNodes();
    final int n = lst.getLength();
    ArrayList<Element> a = new ArrayList<Element>(n);
    for (int i = 0; i < n; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            a.add((Element) node);
    }
    return a;
}

From source file:Main.java

/**
 * Dumps a debug listing of the child nodes of the given node to
 * System.out./*w  w  w  .ja va  2  s . c om*/
 * 
 * @param node the node to dump the children of
 */
public static void dumpChildren(Node node) {
    System.out.println("Children of " + node.getNodeName() + ", NS: " + node.getNamespaceURI() + ", Type: "
            + node.getClass());
    Node child = node.getFirstChild();
    while (child != null) {
        short nodeType = child.getNodeType();
        String nodeName = child.getNodeName();
        String nodeValue = child.getNodeValue();
        String nsURI = child.getNamespaceURI();
        System.out.println("  Type: " + nodeType + ", Name: " + nodeName + ", Value: " + nodeValue + ", NS: "
                + nsURI + ", Type: " + node.getClass());
        child = child.getNextSibling();
    }
}

From source file:Main.java

/**
 * This method is used to search all the <code>Element</code> objects with
 * given key in the passed <code>Element</code> object.
 * //from w  w  w. j a v  a  2 s .  c  o m
 * @param tagName
 *            Name of the tag as String.
 * @param input
 *            Element object.
 * @return <code>Element[]</code> Returns the array of elements, or an empty
 *         array in case here is no match.
 */
public static List<Element> getElements(String tagName, Element input) {
    NodeList nodes = input.getElementsByTagName(tagName);

    int len = nodes.getLength();
    List<Element> elt = new ArrayList<Element>(len);
    Node node;
    for (int i = 0; i < len; i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elt.add((Element) node);
        }
    }

    return elt;
}

From source file:Main.java

/** Finds and returns the next sibling node with the given qualified name. */
public static Element getNextSiblingElementNS(Node node, String[][] elemNames) {

    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                String uri = sibling.getNamespaceURI();
                if (uri != null && uri.equals(elemNames[i][0])
                        && sibling.getLocalName().equals(elemNames[i][1])) {
                    return (Element) sibling;
                }//w  w w  .j  ava 2 s .com
            }
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Access all immediate child elements inside the given Element
 *
 * @param element the starting element, cannot be null.
 * @param namespaceURI name space of the child element to look for,
 * cannot be null. Use "*" to match all namespaces
 * @param elemName the name of the child element to look for,
 * cannot be null./*from   w  w  w  .j  a  va  2  s .c o m*/
 * @return array of all immediate child element inside element, or
 * an array of size zero if no child elements are found.
 */
public static Element[] getChildElements(Element element, String namespaceURI, String elemName) {
    NodeList list = element.getChildNodes();
    int len = list.getLength();
    ArrayList<Node> array = new ArrayList<Node>(len);

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (elemName.equals(n.getLocalName())
                    && ("*".equals(namespaceURI) || namespaceURI.equals(n.getNamespaceURI()))) {
                array.add(n);
            }
        }
    }
    Element[] elems = new Element[array.size()];

    return (Element[]) array.toArray(elems);
}

From source file:Main.java

public static String getAttrValue(NamedNodeMap attrs, String attrName) {
    if (attrs == null || attrName == null) {
        return null;
    }/*  www . j  a  v  a 2 s .co m*/
    String attrValue = null;

    Node attrNode = attrs.getNamedItem(attrName);
    if (attrNode == null || Node.ATTRIBUTE_NODE != attrNode.getNodeType()) {
        return null;
    }

    attrValue = attrNode.getNodeValue();
    return attrValue;
}

From source file:Main.java

/**
 * Checks if the given {@link NodeList} contains a child element.
 *
 * @param list The {@link NodeList} to check.
 * @return Whether the {@link NodeList} contains children.
 *//*from  w w  w .  ja  va 2  s  .  c om*/
public static boolean hasChildElement(NodeList list) {

    Node n;

    for (int i = 0; i < list.getLength(); i++) {
        n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }

    return false;

}

From source file:com.nexmo.client.verify.endpoints.SharedParsers.java

protected static VerifyResult parseVerifyResponseXmlNode(Element root) throws NexmoResponseParseException {
    String requestId = null;//from www. j a  v  a2  s .  c o  m
    int status = -1;
    String errorText = null;

    NodeList fields = root.getChildNodes();
    for (int i = 0; i < fields.getLength(); i++) {
        Node node = fields.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        String name = node.getNodeName();
        if ("request_id".equals(name)) {
            requestId = XmlUtil.stringValue(node);
        } else if ("status".equals(name)) {
            String str = XmlUtil.stringValue(node);
            try {
                if (str != null)
                    status = Integer.parseInt(str);
            } catch (NumberFormatException e) {
                log.error("xml parser .. invalid value in <status> node [ " + str + " ] ");
                status = BaseResult.STATUS_INTERNAL_ERROR;
            }
        } else if ("error_text".equals(name)) {
            errorText = XmlUtil.stringValue(node);
        }
    }

    if (status == -1)
        throw new NexmoResponseParseException("Xml Parser - did not find a <status> node");

    // Is this a temporary error ?
    boolean temporaryError = (status == BaseResult.STATUS_THROTTLED
            || status == BaseResult.STATUS_INTERNAL_ERROR);

    return new VerifyResult(status, requestId, errorText, temporaryError);
}

From source file:Main.java

public static String convertDOMToString(Node node) {
    StringBuffer sb = new StringBuffer();
    if (node.getNodeType() == Node.TEXT_NODE) {
        sb.append(node.getNodeValue());//  w  w  w  . j av  a2s.  c  om
    } else {
        String currentTag = node.getNodeName();
        sb.append('<');
        sb.append(currentTag);
        appendAttributes(node, sb);
        sb.append('>');
        if (node.getNodeValue() != null) {
            sb.append(node.getNodeValue());
        }

        appendChildren(node, sb);

        appendEndTag(sb, currentTag);
    }
    return sb.toString();
}