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

private static Element findElementinChilds(Node node, String uri, String nodeName) {

    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;

            if (nodeName.equals(childElement.getLocalName()) && uri.equals(childElement.getNamespaceURI())) {
                return (Element) childElement;
            }//www.  j ava  2s .c om
        }

        Element located = findElementinChilds(child, uri, nodeName);
        if (located != null) {
            return located;
        }
    }

    return null;
}

From source file:Main.java

public static Node findSibling(Node node, String tagName) {
    Node n = node;/* ww w.j  a v a 2  s . c o m*/
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE && !n.getNodeName().equals(tagName))
        n = n.getNextSibling();
    return n;
}

From source file:Main.java

public static String readProjectName(String file) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    InputStream is = new FileInputStream(file);
    Document doc = dombuilder.parse(is);
    Element root = doc.getDocumentElement();
    NodeList prjInfo = root.getChildNodes();
    if (prjInfo != null) {
        for (int i = 0; i < prjInfo.getLength(); i++) {
            Node project = prjInfo.item(i);
            if (project.getNodeType() == Node.ELEMENT_NODE) {
                String strProject = project.getNodeName();
                if (strProject.equals("project")) {
                    for (Node node = project.getFirstChild(); node != null; node = node.getNextSibling()) {
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                            String strNodeName = node.getNodeName();
                            if (strNodeName.equals("name")) {
                                return node.getTextContent();
                            }/*  w  w w  . ja  va  2 s . c  o m*/
                        }
                    }
                }
            }

        }
    }
    return "";
}

From source file:Main.java

public static ArrayList<String> getNodeListAttValAsStringCol(final String xPath, final Node node,
        final String attrName) throws Exception {
    ArrayList<String> retV = new ArrayList<String>();

    NodeList nl = getNodesListXpathNode(xPath, node);
    int l = nl.getLength();
    Element e = null;//from  www  . j  a v  a2s.c om
    String val = "";

    for (int i = 0; i < l; i++) {
        e = (Element) nl.item(i);
        if (e.getNodeType() == Node.ELEMENT_NODE) {
            val = e.getAttribute(attrName);
            if (val != null && val.length() > 0) {
                retV.add(val);
            }
        }
    }
    return retV;
}

From source file:Main.java

/**
 * Sarches for ressources that have to be downloaded and creates a list with all download links.
 *
 * @param node to check for download links
 * @return list with all found download links
 *///from   www.  ja v  a  2 s.co m
private static ArrayList<String> findUrls(Node node) {

    int type = node.getNodeType();
    ArrayList<String> result = new ArrayList<>();
    String temp = null;
    NamedNodeMap atts = node.getAttributes();
    Log.i(TAG, "parsing for ressources.  node: " + node.getNodeName() + " value: " + node.getNodeValue()
            + " atts length: " + atts.getLength() + "type: " + type);
    switch (type) {
    //Element
    case Node.ELEMENT_NODE:
        //TODO: This method is stupid. It just looks for
        // attributes named ressourcepath. What if we have to download several ressources?

        for (int j = 0; j < atts.getLength(); j++) {
            Log.i(TAG, "atts: " + atts.item(j).getNodeName() + " value: " + atts.item(j).getNodeValue());
            temp = atts.item(j).getNodeValue();

            if (temp != null) {
                result.add(temp);
                Log.i(TAG, "added path: " + temp);
            }

            Log.i(TAG, "parent node:" + node.getParentNode().getNodeName());
            Element parent = (Element) node.getParentNode();
            parent.setAttribute("TESTITEST", "dllink");

        }
        // get the pages, means the children
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            ArrayList<String> childres = findUrls(child);
            if (childres.size() > 0) {
                result.addAll(childres);
            }
        }
        break;
    }
    return result;
}

From source file:Main.java

public static Element getChildElement(Element parent, String childName) {
    NodeList list = parent.getElementsByTagName(childName);

    Node node;// w  w  w .  ja v a  2s. c  o  m
    for (int i = 0; i < list.getLength(); i++) {
        node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

public static boolean setNodeValue(Node domNode, String string) {
    if (domNode == null)
        return false;

    short nodeType = domNode.getNodeType();

    switch (nodeType) {
    case Node.ELEMENT_NODE: {
        setElementText((Element) domNode, string);
        break;/*from  w  ww  .ja  va2  s.  c  o m*/
    }
    case Node.ATTRIBUTE_NODE:
    case Node.TEXT_NODE: {
        domNode.setNodeValue(string);
        break;
    }
    case Node.PROCESSING_INSTRUCTION_NODE: {
        ((ProcessingInstruction) domNode).setData(string);
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        ((CDATASection) domNode).setData(string);
        break;
    }
    default: {
        return false;
    }
    }

    return true;
}

From source file:Main.java

/**
 * Returns a single element having a given tag
 *///www  .  ja v a  2  s .c o m
public static Element getSingleElement(Element element, String tagName) {
    Node node = element.getFirstChild();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0)
            return (Element) node;

        node = node.getNextSibling();
    }

    return null;
}

From source file:Main.java

/**
 * Returns the occurrences of a given node (specified by its name)
 * in a given list of nodes. Counts all subsequent occurs after the
 * first one./*from  w  w w. jav  a  2s  . c o  m*/
 * 
 * @param nodeName The name of the node whose occurrences are requested
 * @param nodes      The list of nodes that will be searched for occurrences of
 *                nodeName.
 * @return         The number of the first subsequent occurences of nodeName. 
 *                Greater than or equal to 0.
 */
public static int getOccurs(String nodeName, NodeList nodes) {
    int occurs = 0;

    int childNr = 0;
    boolean foundFirstOccurence = false;
    while (childNr < nodes.getLength()) {
        Node node = nodes.item(childNr);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().compareTo(nodeName) == 0) {
                occurs++;
                foundFirstOccurence = true;
            } else {
                if (foundFirstOccurence) {
                    return occurs;
                }
            }
        }
        childNr++;
    }

    return occurs;
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        NodeList childNodes = node.getChildNodes();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < childNodes.getLength(); i++) {
            if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) {
                Node myNode = childNodes.item(i);
                buffer.append(myNode.getNodeValue());
            }/*w w  w  . ja v a  2s  . com*/
        }
        return buffer.toString();
    }
    return "";
}