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

/**
 * Returns the value of the first child node of the given node, and asserts
 * that the child node is a Text node.//from   www  .  j a va 2 s  . c o m
 * 
 * @param node a node containing a single Text child node
 * @return the value of the child Text node
 */
public static String getChildText(Node node) {
    Node child = node.getFirstChild();
    assert (child.getNodeType() == Node.TEXT_NODE);
    return child.getNodeValue();
}

From source file:Main.java

public static Element getChildElement(Element elt, String name) {
    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(node.getNodeName()))
                return (Element) node;
    }/*  w  w  w. j a  v a2  s.  c  om*/
    return null;
}

From source file:Main.java

static void printElement(Element element, String indent) {
    System.out.println("Element '" + element.getNodeName() + "'");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            printElement((Element) child, indent + "\t");
            break;
        case Node.ATTRIBUTE_NODE:
            Attr attr = (Attr) child;
            System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'");
            break;
        case Node.COMMENT_NODE:
            Comment comment = (Comment) child;
            System.out.println("\tComment: '" + comment.getData() + "'");
            break;
        case Node.CDATA_SECTION_NODE:
            CharacterData cdata = (CharacterData) child;
            System.out.println("\tCDatat: '" + cdata.getData() + "'");
            break;
        case Node.TEXT_NODE:
            Text text = (Text) child;
            System.out.println("\tText: '" + text.getData() + "'");
            break;
        default:/*from  w w w  .ja  v  a  2  s .  co  m*/
            System.out.println("\tUnknown node type: '" + child.getNodeType() + "'");
            break;
        }
    }
}

From source file:Main.java

public static String getTextContent(Node e) {
    if (e == null || e.getNodeType() != Node.ELEMENT_NODE) {
        return null;
    }/*w w  w  .ja v a 2 s . c o m*/

    NodeList nodes = e.getChildNodes();
    StringBuilder text = new StringBuilder();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = e.getFirstChild();

        if (node != null && node.getNodeType() == Node.TEXT_NODE) {
            String s = node.getNodeValue();
            if (s != null) {
                text.append(s);
            }
        }
    }

    if (text.length() > 0) {
        return text.toString();
    } else {
        return null;
    }
}

From source file:Main.java

public static String getTextTag(Node node) {
    Node child = node.getFirstChild();
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
    return null;/*from w w  w. ja  v  a2  s. c  o m*/
}

From source file:Main.java

public static Vector<String> getPropertiesFromXML(Node propNode) {
    Vector<String> properties;
    properties = new Vector<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.addElement(namespace + ":" + nodeName);
        }//  ww w .j  ava  2s .c  o  m
    }
    return properties;
}

From source file:Main.java

/**
 * Gets a TEXT node value from the specified Element node.
 *
 * @param element a ELEMENT Node has TEXT node 
 *///from w ww  .  j  ava 2s.co m
/* TODO: Child? */
public static String getTextData(Node element) {
    if (element.getNodeType() != Node.ELEMENT_NODE) {
        throw new IllegalArgumentException(element + " is not ELEMENT node");
    }
    Node description = element.getNextSibling();
    if (description.getNodeType() != Node.TEXT_NODE) {
        throw new IllegalArgumentException(description + " is not TEXT node");
    }
    return description.getNodeValue();
}

From source file:Main.java

public static Element getFirstChildElementByName(Element root, String tagName) {
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*  w  w  w.j av a  2s  .c  o  m*/
        if (tagName.equals(child.getNodeName())) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

public static ArrayList<Element> getElements(String elementName, Document doc) {
    ArrayList<Element> result = new ArrayList<Element>();
    NodeList list = doc.getElementsByTagName(elementName);

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

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            result.add((Element) nNode);
        }/* w  w w . j  a va 2 s  .co  m*/
    }
    return result;
}

From source file:Main.java

public static ArrayList<Element> getElements(String elementName, Element element) {
    ArrayList<Element> result = new ArrayList<Element>();
    NodeList list = element.getElementsByTagName(elementName);

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

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            result.add((Element) nNode);
        }/*from   ww  w  .ja va 2 s.  c  om*/
    }
    return result;
}