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 Node GetSimpleTagNode(Document doc, String tag) {
    NodeList nList2 = doc.getElementsByTagName(tag);
    for (int temp = 0; temp < nList2.getLength(); temp++) {
        Node nNode2 = nList2.item(temp);
        if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
            return nNode2;
        } // end if nnode.getnodetype()
    } //end for int temp
    return null;/* www .j  a  v a 2 s .  c  o  m*/
}

From source file:Main.java

private static void printNode(NodeList nodeList, int level) {
    level++;//from   w w w.  j  a  v  a  2  s . c  om
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(node.getNodeName() + "[" + level + "]");
                printNode(node.getChildNodes(), level);
                // how depth is it?
                if (level > depthOfXML)
                    depthOfXML = level;
            }
        }
    }
}

From source file:Main.java

public static Vector<String> getPropertiesFromXML(Node propNode) {
    Vector<String> properties;
    properties = new Vector<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.addElement(namespace + ":" + nodeName);
        }//w  w w .  ja  va2 s  . co  m
    }
    return properties;
}

From source file:Main.java

public static String getFirstMatchedValueByChildTagName(Node parent, String name) {
    //List<Element> nodeList = new ArrayList<Element>();
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        //System.out.println(child.getNodeName());
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().indexOf(name) != -1) {
                return child.getTextContent();
            } else {
                String value = getFirstMatchedValueByChildTagName((Element) child, name);
                if (value != null) {
                    return value;
                }/*from  w w w. j av a  2s .com*/
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the first Element node in a NodeList
 * @param list the list to search//  w w w.jav a2 s . c  o m
 * @return the element or null if none is found
 */
public static Element getFirstElement(NodeList list) {
    if (list.getLength() == 0) {
        return null;
    }

    Node node = list.item(0);
    while (node.getNodeType() != Node.ELEMENT_NODE) {
        if (node.getNextSibling() == null) {
            return null;
        }
        node = node.getNextSibling();
    }
    return (Element) node;
}

From source file:Main.java

public static void getAllAttrs(Element parent, String name, List<String> lst) {
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            getAllAttrValueByName(child, name);
            List<String> tmp = getAllAttrValueByName(child, name);
            if (tmp != null) {
                lst.addAll(tmp);//from  w  w  w.j a  v  a2  s  . c  o m
            } else {
                // recursive childnodes
                getAllAttrs((Element) child, name, lst);
            }
        }
    }
}

From source file:Main.java

public static Vector<Element> getChildElemsByName(final String name, final Node parent) {
    Vector<Element> v = new Vector<Element>();
    Element elem = null;/*from w w w. j  a va 2  s  . com*/
    for (Node childNode = parent.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (childNode.getNodeName() == name) {
                elem = (Element) childNode;
                v.add(elem);
            }
        }
    }
    return v;
}

From source file:Main.java

public static List childNodeList(Node node, String childNodeName) {
    if (node == null)
        return null;

    List children = new LinkedList();
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {/*from   w  w w.  j  a v a 2s  .c  o  m*/
            if (childNode.getNodeType() == Node.ELEMENT_NODE
                    && (childNodeName == null || childNodeName.equals(childNode.getNodeName()))) {
                children.add(childNode);
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return children;
}

From source file:Main.java

private static String getTextValue(String def, Element doc, String tag) {
    String value = def;/*from w ww .  j av  a2s . c o  m*/
    NodeList noteList = doc.getElementsByTagName(tag);
    if (noteList.getLength() > 0) {
        for (int i = 0; i < noteList.getLength(); i++) {
            Node note = noteList.item(i);

            Element noteElement = (Element) note;
            if (noteElement.getNodeType() == Node.ELEMENT_NODE) {
                if (noteElement.getElementsByTagName("pitch").getLength() > 0) {
                    Element pitchElement = (Element) noteElement.getElementsByTagName("pitch").item(0);
                    System.out.println("Staff idX : "
                            + pitchElement.getElementsByTagName("step").item(0).getTextContent());
                } else {
                    System.out.println("yoktir");
                }
            } else {
                System.out.println("not element");
            }
        }

        //          value = nl.item(0).getFirstChild().getNodeValue();
    }
    return value;
}

From source file:Main.java

/**
 * Method to convert a Node XML to a Element XML.
 * @param node node XML to input./*from w  w  w  .  j  av a 2  s .  c om*/
 * @return elment XML.
 */
public static Element convertNodeToElement(Node node) {
    NodeList list = node.getChildNodes();
    Element m = null;
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        //Node n = elem.getFirstChild();
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            m = (Element) n;
        }
    }
    return m;
}