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 ArrayList<Node> getChildNodesWithTagName(String tagName, Element parentElement) {
    NodeList childNodes = parentElement.getChildNodes();

    ArrayList<Node> childNodesWithTagName = new ArrayList<Node>();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals(tagName)) {
            childNodesWithTagName.add(childNode);
        }/*w w w .  j  a  va  2  s  .c o m*/
    }

    return childNodesWithTagName;
}

From source file:Main.java

public static String getNodeAttributeByName(String xmlFilePath, String nodeName, String attributeName) {
    String returnVal = "No value!";
    DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
    try {//from  www  .j  a v a 2s  .com

        DocumentBuilder domBuilder = domBuilderFactory.newDocumentBuilder();
        InputStream is = new FileInputStream(xmlFilePath);
        Document doc = domBuilder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    String test = node.getNodeName();
                    if (test.equals(nodeName)) {
                        returnVal = node.getAttributes().getNamedItem(attributeName).getNodeValue();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return returnVal;
}

From source file:Main.java

/**
 * Return a List of Element objects that have the given name and are immediate children of the
 * given element; if name is null, all child elements will be included.
 *//*from  w  w  w.ja  v a  2 s .  c o  m*/
public static List<Element> childElementList(Element element, String childElementName) {
    if (element == null)
        return null;

    List<Element> elements = new LinkedList<Element>();
    Node node = element.getFirstChild();

    if (node != null) {
        do {
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && (childElementName == null || childElementName.equals(node.getNodeName()))) {
                Element childElement = (Element) node;

                elements.add(childElement);
            }
        } while ((node = node.getNextSibling()) != null);
    }
    return elements;
}

From source file:Main.java

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

    List children = new LinkedList();
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {//ww  w . j a  v a2  s.  c o  m
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                children.add(childNode);
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return children;
}

From source file:Main.java

/**
 * Returns the index of a node .//w  w w  .  ja  v  a2s.c  o m
 * 
 * @param elt
 * @return
 */
public static final int getElementIdx(Node elt) {
    int count = 0;
    for (Node sib = elt.getPreviousSibling(); sib != null; sib = sib.getPreviousSibling()) {
        if (sib.getNodeType() == Node.ELEMENT_NODE
                && ((Element) sib).getTagName().equals(((Element) elt).getTagName()))
            count++;
    }

    return count;
}

From source file:Main.java

public static Element getElementByTagNameNS(Node element, String namespace, String name) {
    NodeList elements = element.getChildNodes();
    CharSequence colon = ":";
    if (elements != null) {
        for (int i = 0; i < elements.getLength(); i++) {
            if (elements.item(i).getNodeType() == Node.ELEMENT_NODE && (elements.item(i).getAttributes()
                    .getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil") == null
                    || !"true".equals(elements.item(i).getAttributes()
                            .getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "nil")))) {
                Element currentElement = (Element) elements.item(i);
                String nodeName = currentElement.getNodeName();
                String nodeNameOnly = nodeName;
                if (nodeName.contains(colon)) {
                    String[] nodeNameSplit = nodeName.split(":");
                    nodeNameOnly = nodeNameSplit[1];
                }/*from  www.j a v  a  2 s  .c  om*/

                if ((currentElement.getNamespaceURI() == null
                        || currentElement.getNamespaceURI().equals(namespace)) && nodeNameOnly.equals(name)) {
                    return currentElement;
                }
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Convert a node type to a string. For debug purpose only.
 * //  w w w. j av a 2 s  .com
 * @param nodeType
 *            the node type
 * @return the string
 * @throws Exception
 *             the exception
 */
public static String nodeTypeToString(short nodeType) throws Exception {
    if (nodeType == Node.ELEMENT_NODE)
        return "ELEMENT_NODE";
    if (nodeType == Node.ATTRIBUTE_NODE)
        return "ATTRIBUTE_NODE";
    if (nodeType == Node.TEXT_NODE)
        return "TEXT_NODE";
    if (nodeType == Node.CDATA_SECTION_NODE)
        return "CDATA_SECTION_NODE";
    if (nodeType == Node.ENTITY_REFERENCE_NODE)
        return "ENTITY_REFERENCE_NODE";
    if (nodeType == Node.ENTITY_NODE)
        return "ENTITY_NODE";
    if (nodeType == Node.PROCESSING_INSTRUCTION_NODE)
        return "PROCESSING_INSTRUCTION_NODE";
    if (nodeType == Node.COMMENT_NODE)
        return "COMMENT_NODE";
    if (nodeType == Node.DOCUMENT_NODE)
        return "DOCUMENT_NODE";
    if (nodeType == Node.DOCUMENT_TYPE_NODE)
        return "DOCUMENT_TYPE_NODE";
    if (nodeType == Node.DOCUMENT_FRAGMENT_NODE)
        return "DOCUMENT_FRAGMENT_NODE";
    if (nodeType == Node.NOTATION_NODE)
        return "NOTATION_NODE";
    if (nodeType == Node.DOCUMENT_POSITION_DISCONNECTED)
        return "DOCUMENT_POSITION_DISCONNECTED";
    if (nodeType == Node.DOCUMENT_POSITION_PRECEDING)
        return "DOCUMENT_POSITION_PRECEDING";
    if (nodeType == Node.DOCUMENT_POSITION_FOLLOWING)
        return "DOCUMENT_POSITION_FOLLOWING";
    if (nodeType == Node.DOCUMENT_POSITION_CONTAINS)
        return "DOCUMENT_POSITION_CONTAINS";
    if (nodeType == Node.DOCUMENT_POSITION_CONTAINED_BY)
        return "DOCUMENT_POSITION_CONTAINED_BY";
    if (nodeType == Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC)
        return "DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC";

    throw new Exception("Unknown value : " + nodeType);
}

From source file:Main.java

public static void updateUserMgtXML(File userMgtXML, String jndiConfigNameUserDB) throws Exception {
    Document doc = initializeXML(userMgtXML);
    NodeList configNodeList = doc.getElementsByTagName("Configuration");
    Node configNode = configNodeList.item(0); //get the 0 index, since only one available

    NodeList nodeList = configNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equalsIgnoreCase("Property")) {
                if (node.hasAttributes()) {
                    NamedNodeMap namedNodeMap = node.getAttributes();
                    Node attr = namedNodeMap.getNamedItem("name");
                    if (attr != null && attr.getNodeValue().equalsIgnoreCase("dataSource")) {
                        node.setTextContent(jndiConfigNameUserDB);
                    }//from  w  w w  . j  a v a  2  s .com
                }
            }
        }
    }
    finalizeXML(userMgtXML, doc, 4);
}

From source file:Main.java

private static String getValue(Node node, short nodeType) {
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        return ((Element) node).getTagName();

    case Node.TEXT_NODE:
        return ((Text) node).getData();

    case Node.PROCESSING_INSTRUCTION_NODE:
        return ((ProcessingInstruction) node).getData();

    default://from  w  w w . j  a  v  a  2s. c  om
        return "";
    }
}

From source file:Main.java

public static Set<Node> populateNodes(Node node, Set<Node> nodes) {
    if (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            nodes.add(node);//from ww w  .jav  a  2s  .  c  om
        }
        populateNodes(node.getNextSibling(), nodes);
    }
    return nodes;
}