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

/**
 * Looks through all child elements of the specified root (recursively)
 * and returns the first element that corresponds to all parameters.
 *
 * @param root the Element where the search should begin
 * @param tagName the name of the node we're looking for
 * @param keyAttributeName the name of an attribute that the node has to
 * have/*from w  w w . jav  a  2s .  c  o m*/
 * @param keyAttributeValue the value that attribute must have
 * @return the Element in the tree under root that matches the specified
 * parameters.
 * @throws NullPointerException if any of the arguments is null.
 */
public static Element locateElement(Element root, String tagName, String keyAttributeName,
        String keyAttributeValue) {
    NodeList nodes = root.getChildNodes();
    int len = nodes.getLength();

    for (int i = 0; i < len; i++) {
        Node node = nodes.item(i);

        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        Element element = (Element) node;

        // is this the node we're looking for?
        if (node.getNodeName().equals(tagName)) {
            String attr = element.getAttribute(keyAttributeName);

            if ((attr != null) && attr.equals(keyAttributeValue))
                return element;
        }

        //look inside.
        Element child = locateElement(element, tagName, keyAttributeName, keyAttributeValue);

        if (child != null)
            return child;
    }
    return null;
}

From source file:Main.java

/**
 * This method will return the content of this particular <code>element</code>.
 * For example,/*  w  ww  .  j a v  a  2  s  . com*/
 * <p/>
 * <pre>
 *    <result>something_1</result>
 * </pre>
 * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
 * argument (<code>element</code> to this method, it returns the content of it,
 * namely, <code>something_1</code> in the example above.
 *
 * @return
 */
public static String getContent(Element element) {
    StringBuilder paramValue = new StringBuilder();
    NodeList childNodes = element.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node currentNode = childNodes.item(j);
        if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) {
            String val = currentNode.getNodeValue();
            if (val != null) {
                paramValue.append(val.trim());
            }
        }
    }
    return paramValue.toString().trim();
}

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;
        }/*  w w  w .  j  ava 2  s  .c o  m*/
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Returns a single element having a given tag
 *///from   w w  w.j ava  2 s .  co  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

private static List<Element> getJsModulesForSpecificPlatform(Document doc, String platformName) {
    List<Element> suitableJsModules = new ArrayList<Element>();
    Element documentElement = doc.getDocumentElement();
    NodeList childNodes = documentElement.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            if (isJsModuleElement(element)) { // Common js-module for all types of projects (ios, android, wp8 etc.)
                suitableJsModules.add(element);
            } else if (isPlatformElement(element) && (element.getAttribute(ATTRIBUTE_NAME) != null)
                    && element.getAttribute(ATTRIBUTE_NAME).equals(platformName)) { // platform-specific js-module
                List<Element> androidJsModules = getChildElementsByName(element, TAG_JS_MODULE);
                suitableJsModules.addAll(androidJsModules);
            }//from w  w w.  j  a  v  a2s . co m
        }
    }

    return suitableJsModules;
}

From source file:Main.java

public static List<Map<String, String>> ReadPlaylistItemsFromFile(String path, String filename) {

    List<Map<String, String>> results = new ArrayList<Map<String, String>>();

    try {/* w ww .j  a  va2  s . c o  m*/
        File fXmlFile = new File(path, filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList songList = doc.getElementsByTagName("song");
        Log.d("ReadItemsFromFile", "List Length: " + songList.getLength());

        for (int i = 0; i < songList.getLength(); i++) {

            Node nNode = songList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                HashMap<String, String> mapElement = new HashMap<String, String>();

                mapElement.put("name", eElement.getElementsByTagName("name").item(0).getTextContent());
                mapElement.put("artist", eElement.getElementsByTagName("artist").item(0).getTextContent());
                mapElement.put("startTime",
                        eElement.getElementsByTagName("startTime").item(0).getTextContent());
                mapElement.put("url", eElement.getElementsByTagName("url").item(0).getTextContent());

                results.add(mapElement);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return results;

}

From source file:Main.java

public static Node findWithAttrubute(Node root, String name, String attributeName) {
    final NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        final Node subnode = list.item(i);
        if (subnode.getNodeType() == Node.ELEMENT_NODE) {
            if (subnode.getNodeName().equals(name)) {
                if (subnode.getAttributes().getNamedItem(attributeName) != null) {
                    return subnode;
                }/*w w w . ja  v  a2 s .co m*/
            }
        }
    }
    return null;
}

From source file:Main.java

public static void stepThrough(Node start) {
    System.out.println(start.getNodeName() + " = " + start.getNodeValue());

    if (start.getNodeType() == Element.ELEMENT_NODE) {
        NamedNodeMap startAttr = start.getAttributes();
        for (int i = 0; i < startAttr.getLength(); i++) {
            Node attr = startAttr.item(i);
            System.out.println(" Attribute: " + attr.getNodeName() + " = " + attr.getNodeValue());
        }//from  w ww. j ava 2s.c o  m
    }

    for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) {
        stepThrough(child);
    }
}

From source file:Main.java

/**
 * Gets the index of the specified element amongst elements with the same
 * name/*from   w w w  .  ja v a2 s .  co  m*/
 * 
 * @param element
 *           the element to get for
 * @return the index of the element, will be >= 1
 */

public static int getElementIndex(Node element) {
    int result = 1;

    Node elm = element.getPreviousSibling();
    while (elm != null) {
        if (elm.getNodeType() == Node.ELEMENT_NODE && elm.getNodeName().equals(element.getNodeName()))
            result++;
        elm = elm.getPreviousSibling();
    }

    return result;
}

From source file:Main.java

private static Node getPreviousTypedNode(Node node, short nodeType) {
    node = node.getPreviousSibling();/*  w w  w.  j  a  v  a2  s  .c  o  m*/

    while (node != null && node.getNodeType() != nodeType) {
        node = node.getPreviousSibling();
    }

    return node;
}