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

/**
 * Gets first child element with specified name.
 *
 * @param parentNode parent node./*from w w  w. ja  v a  2s .  co m*/
 * @param childName  child name.
 * @return first childr element with specified name.
 */
public static Element getChildElement(final Node parentNode, final String childName) {
    //        parentNode.normalize();
    final NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(childName))
            return (Element) node;
    }
    return null;
}

From source file:Main.java

public static Iterator getElementsByTagNames(Element element, String[] tags) {
    List<Element> children = new ArrayList<Element>();
    if (element != null && tags != null) {
        List tagList = Arrays.asList(tags);
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && tagList.contains(((Element) child).getTagName())) {
                children.add((Element) child);
            }/*from   w w w.j a  v  a 2 s  . c o  m*/
        }
    }
    return children.iterator();
}

From source file:Main.java

/**
 * Returns the first direct child element for the given parent node, which
 * matches the given tagName (probably faster than retrieving all children first).
 * @param parent the parent node under which to search for the element
 * @param tagName the tag name of the element to find
 * @return Element - the first found Element or null if no such Element is present
 *//*from  w w w .j  a  v  a  2  s. co  m*/
public static Element findFirstChildElement(Node parent, String tagName) {
    Node n = parent.getFirstChild();
    do {
        if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().equals(tagName))) {
            return (Element) n;
        }
        // Android Workaround
        try {
            n = n.getNextSibling();
        } catch (IndexOutOfBoundsException e) {
            n = null;
        }
    } while (n != null);
    return null;
}

From source file:Main.java

/**
 * Prints a textual representation of the given node to the specified PrintStream.
 *
 * @param  n    Node that is to be printed.
 * @param  out  The PrintStream to which the node is to be printed.
 * @pre    n != null && out != null
 *///from w ww.  j  a v  a  2  s .c  o  m
public static void printXMLNode(Node n, PrintStream out) {
    switch (n.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.println("DOC_ROOT");
        break;

    case Node.ELEMENT_NODE:
        out.println("<" + ((Element) n).getTagName() + ">");
        break;

    case Node.ATTRIBUTE_NODE:
        out.println("@" + ((Attr) n).getName());
        break;

    case Node.TEXT_NODE:
        out.println("\"" + ((Text) n).getWholeText().trim() + "\"");
        break;

    case Node.COMMENT_NODE:
        out.println("COMMENT: \"" + n.getTextContent().trim() + "\"");
        break;

    default:
        out.println("Unknown node type: " + n.getNodeType());
    }
}

From source file:Main.java

/**
 * Get Element by according tag// www  .  j av a  2s  . co  m
 * Only one such element should exist
 *
 * @param document document to search
 * @param tag tag to look for in document
 * @return corresponding element
 */
public static Element getSingleAppearingElementByTag(Document document, String tag) {
    NodeList elementList = document.getElementsByTagName(tag);
    if (elementList.getLength() != 1)
        return null;

    Node nNode = elementList.item(0);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) nNode;
    }
    return null;
}

From source file:Main.java

public static String getTagName(Node labelNode) {
    if (labelNode.getNodeType() == Node.ELEMENT_NODE) {
        return labelNode.getNodeName();
    }/*from   w  ww  .j a  v  a2s. c  o m*/
    return null;
}

From source file:Main.java

public static Map<String, String> SimpleDocumentToMap(Document doc) {
    Map<String, String> simpleMap = new HashMap<String, String>();
    //trim off outter layer
    Node node = doc.getFirstChild();
    NodeList nList = node.getChildNodes();
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                simpleMap.put(eElement.getTagName(), eElement.getTextContent());
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return simpleMap;
}

From source file:Main.java

public static Element getChildElement(Element parent, String childName) {
    NodeList children = parent.getChildNodes();
    int size = children.getLength();

    for (int i = 0; i < size; i++) {
        Node node = children.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            if (childName.equals(element.getNodeName())) {
                return element;
            }//from w  w w.j  a  v a2s  .c  om
        }
    }

    return null;
}

From source file:Main.java

public static Element[] getChildElementNodesByName(Node node, String name) {
    if (node == null) {
        return null;
    }/*  w ww  .j  a  v  a2 s.co m*/

    ArrayList<Element> elements = new ArrayList<>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null && (node.getNodeType() == Node.ELEMENT_NODE)
                    && name.equalsIgnoreCase(item.getNodeName())) {
                elements.add((Element) item);
            }
        }
    }
    return elements.toArray(new Element[elements.size()]);
}

From source file:Main.java

public static Map<String, String> getChildElementNodesMap(Node node) {
    if (node == null) {
        return null;
    }//from   ww w  . j  a v a  2 s  .co m

    Map<String, String> map = new ConcurrentHashMap<>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null && item.getNodeType() == Node.ELEMENT_NODE) {
                map.put(item.getNodeName(), item.getTextContent().trim());
            }
        }
    }
    return map;
}