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

/**
 * @param element The element whose ancestry we will check
 * @param tagName The tagName of the element we are searching for in the ancestry
 * @param limitTagName Stop searching if we hit this limit
 * @return The first matching element, if found
 *//*from  ww w .ja v  a  2 s. c om*/
public static Element getAncestorOrSelf(final Element element, final String tagName,
        final String limitTagName) {
    Element result = null;
    Element next = element;
    String currentTagName;
    do {
        currentTagName = next.getTagName();
        if (currentTagName.equals(tagName)) {
            result = next;
            break;
        } else {
            Node parent = next.getParentNode();
            if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
                next = (Element) parent;
            } else {
                break;
            }
        }
    } while (next != null && (limitTagName == null || !tagName.equals(limitTagName)));
    return result;
}

From source file:Main.java

public static Element[] getChildrenByName(Element element, String paramString) {
    NodeList localNodeList = element.getChildNodes();
    int i = localNodeList.getLength();
    LinkedList<Node> nodes = new LinkedList<Node>();
    for (int j = 0; j < i; ++j) {
        Node localNode = localNodeList.item(j);
        if ((localNode.getNodeType() != 1) || (!localNode.getNodeName().equals(paramString)))
            continue;
        nodes.add(localNode);/* w w w.j ava  2s .c o  m*/
    }
    return (Element[]) nodes.toArray(new Element[nodes.size()]);
}

From source file:Main.java

public static String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE)
                    return data.getNodeValue();
            }// w  ww .ja v a2  s  .  c o m
        }
    }
    return "";
}

From source file:Main.java

public static List<Map<String, String>> readXMLFile(String outFile) {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    List<Map<String, String>> returnlist = new ArrayList<Map<String, String>>();

    try {//from  w w w .  j av  a 2s  . co m
        DocumentBuilder dombuilder = domfac.newDocumentBuilder();
        InputStream is = new FileInputStream(outFile);
        Document doc = dombuilder.parse(is);
        NodeList nl = doc.getElementsByTagName("row");
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            NodeList fileds = node.getChildNodes();
            Map<String, String> map = new HashMap<String, String>();
            for (int j = 0; j < fileds.getLength(); j++) {
                Node filed = fileds.item(j);

                if (filed.getNodeType() == Node.ELEMENT_NODE) {
                    map.put(filed.getAttributes().getNamedItem("name").getNodeValue(),
                            filed.getFirstChild().getNodeValue());
                }
            }
            returnlist.add(map);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return returnlist;

}

From source file:DOMHelper.java

/**
 * Gets the first child element of a node.
 * @param node the node to get the child from
 * @return the first element child of {@code node} or {@code null} if none
 * @throws NullPointerException if {@code node} is {@code null}
 *//*www .j a va  2 s.  c  o  m*/
public static Element getFirstChildElement(Node node) {
    node = node.getFirstChild();
    while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
        node = node.getNextSibling();
    }
    return (Element) node;
}

From source file:Main.java

/**
 * Extracts the inner text value from a xml node accept : <node>value</node> and return value
 * string//from   www.j a v  a  2  s  .  c  om
 * 
 * @param node XML node to extract inner text from
 * @return innerText if its not empty otherwise null.
 */
private static String getNodeTextValue(Node node) {
    String innerText = null;
    if (node != null) {
        Node textNode = node.getFirstChild();
        if (textNode != null && textNode.getNodeType() == Node.TEXT_NODE) {
            innerText = textNode.getNodeValue();
            innerText = innerText.trim();
            if (innerText.length() == 0) {
                innerText = null;
            }
        }
    }
    return innerText;
}

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 va  2  s.  c  o 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

/**
 * Update a property of a given configuration file and return a string representation of the
 * xml document//w ww  .j a  va2  s. com
 * @param doc
 * @param property
 * @param newValue
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 * @throws ClassNotFoundException 
 */
public static String updateXmlDoc(Document doc, List<String> properties)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    for (String property : properties) {
        String key = property.split(":")[0];
        String value = property.split(":")[1];
        NodeList propertiesNode = doc.getElementsByTagName("esf:property");
        for (int i = 0; i < propertiesNode.getLength(); i++) {
            Node node = propertiesNode.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element el = (Element) node;
                if (key.equals(el.getAttribute("name"))) {
                    el.getElementsByTagName("esf:value").item(0).setTextContent(value);
                }
            }
        }
    }
    return parseXmlDocToString(doc);
}

From source file:Main.java

/**
 * Get the next sibling element of the specified element, null if it has no other siblings.
 * //from w w  w. j  a va  2 s . c om
 * @param element The element to search siblings for.
 * @return The next sibling element, null if it has none.
 */
public static Element getNextSibling(Element element) {
    if (element == null)
        return null;
    Node node = element.getNextSibling();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * only returns the first instance of this child node
 * @param node//from  ww  w. j  a  v  a  2s.c  o m
 * @param localName
 * @return
 */
public static Node getChildNode(Node node, String name) {
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName()))
            return (temp);
    }

    return (null);
}