Example usage for org.w3c.dom Node ELEMENT_NODE

List of usage examples for org.w3c.dom Node ELEMENT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node ELEMENT_NODE.

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

public static ArrayList<Element> getElementsByTagNameNS(Node element, String namespace, String name) {
    ArrayList<Element> childElements = new ArrayList<Element>();

    NodeList elements = element.getChildNodes();
    if (elements != null) {
        for (int i = 0; i < elements.getLength(); i++) {
            if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) elements.item(i);
                if ((currentElement.getNamespaceURI() == null
                        || currentElement.getNamespaceURI().equals(namespace))
                        && currentElement.getNodeName().equals(name)) {

                    childElements.add(currentElement);
                }/*from w w w. j  a  v a2  s . c  o  m*/
            }
        }
    }

    return childElements;
}

From source file:Main.java

/** Finds and returns the first child element node. */
public static Element getFirstChildElement(Node parent) {

    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }/*from  w  w w  .j  av a 2 s.c  om*/
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method
 *//*from  w  w w.j  a  v a  2  s. c  o  m*/
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

public static void stripWhitespace(Element element) {
    final NodeList childNodeList = element.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node node = childNodeList.item(i);
        switch (node.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            final String text = ((CharacterData) node).getData();
            if (WhitespacePattern.matcher(text).matches()) {
                element.removeChild(node);
                --i;/*from   ww w. j a  va  2s .co  m*/
            }
            break;
        case Node.ELEMENT_NODE:
            stripWhitespace((Element) node);
            break;
        }
    }
}

From source file:Main.java

/**
 * Gets an element from the XML//from   w ww. jav a  2s .  c o  m
 * 
 * @param elementTag
 * @param fromElement
 * @return
 */
public static Element getElement(String elementTag, Element fromElement) {
    Element element = null;
    NodeList nodeList = fromElement.getElementsByTagName(elementTag);
    if (nodeList.getLength() > 0) {
        Node node = nodeList.item(0);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            element = (Element) node;
        }
    }
    return element;
}

From source file:Main.java

public static Element getFirstChildElement(Element parentElem) {
    if (parentElem == null) {
        return null;
    }//  w w w. ja v  a2  s .c  o m
    Node child = parentElem.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

/** Finds and returns the next sibling element node. */
public static Element getNextSiblingElement(Node node) {

    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) sibling;
        }/*from www .  ja v  a  2 s.c  om*/
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static List<Element> getElementsByTagNames(Node node, String... tagName) {
    List<Element> nds = new ArrayList<Element>();
    if (tagName.length == 0)
        tagName = new String[] { "" };
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element doc = (Element) node;
        for (String elementName : tagName) {
            nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
        }/*  w w  w  .  j  a v a 2s .  co  m*/
    } else if (node instanceof Document) {
        Document doc = ((Document) node);
        for (String elementName : tagName) {
            nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
        }
    } else if (node instanceof DocumentFragment) {
        Document doc = ((DocumentFragment) node).getOwnerDocument();
        for (String elementName : tagName) {
            nds.addAll(toElementList(doc.getElementsByTagName(elementName)));
        }
    } else {
        throw new IllegalArgumentException("a node who doesn't support getElementsByTagName operation.");
    }
    return nds;
}

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)
        return getFirstChildElement(elm, localName);

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);//  w ww.j  a va2 s. c o m
        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;
    }

    return null;
}

From source file:Main.java

/**
 * Checks if Element Node is same as a Element name String
 *//*from   ww  w .j a va2  s  . c om*/

public static boolean isStrElementNode(String elementName, Node elementNode, boolean ignoreCase)

{

    if ((elementNode == null) || (elementName == null) ||

            (elementName.trim().equals("")) || (elementNode.getNodeType() != Node.ELEMENT_NODE))

        return false;

    StringTokenizer tokenizer = new StringTokenizer(":");

    int numTokens = tokenizer.countTokens();

    if (numTokens == 1)

    {

        String name = (String) tokenizer.nextElement();

        Element element = (Element) elementNode;

        if (element.getNamespaceURI() != null)

            return false;

        if (ignoreCase)

            return element.getNodeName().trim().equalsIgnoreCase(elementName);

        return element.getNodeName().trim().equals(elementName);

    } else if (numTokens == 2)

    {

        String namespace = (String) tokenizer.nextElement();

        String localName = (String) tokenizer.nextElement();

        Element element = (Element) elementNode;

        if (element.getNamespaceURI() == null)

            return false;

        if (ignoreCase)

            return ((element.getLocalName().trim().equalsIgnoreCase(localName)) &&

                    (element.getNamespaceURI().equalsIgnoreCase(namespace.trim())));

        return ((element.getLocalName().trim().equals(localName)) &&

                (element.getNamespaceURI().equals(namespace.trim())));

    } else

        return false;

}