Example usage for org.w3c.dom Node getNodeName

List of usage examples for org.w3c.dom Node getNodeName

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeName.

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static Element getParentXElement(Element startElement, String xmlTag) {
    boolean found = false;
    if (startElement == null)
        return null;
    Node parentNode = startElement.getParentNode();
    while (!found) {
        if (parentNode == null)
            found = true;/*from  www. ja va  2 s .  co  m*/
        else if (parentNode.getNodeName().equals(xmlTag))
            found = true;
        if (!found)
            parentNode = parentNode.getParentNode();
    }
    return (Element) parentNode;
}

From source file:Main.java

/**
 * Scans a node and all of its children for nodes of a particular type.
 *
 * @param parent          The parent node to search from.
 * @param recursiveSearch If the child nodes should be recursively searched.
 * @param nodeNames       A single node name or list of node names to search for
 * @return a List of all the nodes found matching the nodeName under the parent
 *//*ww w .  j av  a2 s  .  c o m*/
protected static List<Node> getChildNodes(final Node parent, boolean recursiveSearch,
        final String... nodeNames) {
    final List<Node> nodes = new ArrayList<Node>();
    final NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        final Node child = children.item(i);

        for (final String nodeName : nodeNames) {
            if (child.getNodeName().equals(nodeName)) {
                nodes.add(child);
            }
            if (recursiveSearch) {
                nodes.addAll(getChildNodes(child, true, nodeName));
            }
        }
    }
    return nodes;
}

From source file:Main.java

public static Node getNode(Node parentNode, String nodeName, Map<String, String> attributesMap) {
    Node returnNode = null;/* www .j  av  a2 s  .  c o m*/
    try {
        DocumentTraversal dt = (DocumentTraversal) parentNode.getOwnerDocument();
        NodeIterator i = dt.createNodeIterator(parentNode, NodeFilter.SHOW_ELEMENT, null, false);
        Node node = i.nextNode();
        while (node != null) {
            if (node.getNodeName().equals(nodeName)) {
                if (attributesExist(node, attributesMap)) {
                    returnNode = node;
                    break;
                }
            }
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
    return returnNode;
}

From source file:Main.java

public static String getNodeAttributeByName(String xmlFilePath, String nodeName, String attributeName) {
    String returnVal = "No value!";
    DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
    try {//ww  w .  j a v  a2  s.  c o m

        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

public static String[] getAllTokens(Document doc, boolean hasCorpusElement) {
    ArrayList<String> toReturnAL = new ArrayList<String>();

    if (hasCorpusElement) {

        NodeList corpusDocs = doc.getChildNodes().item(0).getChildNodes();

        for (int d = 0; d < corpusDocs.getLength(); d++) {
            if (!corpusDocs.item(d).getNodeName().equals("doc"))
                continue;

            //System.out.println(doc.getChildNodes().getLength());

            NodeList sentences = corpusDocs.item(d).getChildNodes();

            for (int i = 0; i < sentences.getLength(); i++) {
                if (!sentences.item(i).getNodeName().equals("s"))
                    continue;
                NodeList tokens = sentences.item(i).getChildNodes();
                for (int j = 0; j < tokens.getLength(); j++) {
                    Node tokenNode = tokens.item(j);
                    if (tokenNode.getNodeName().equals("toponym")) {
                        toReturnAL.add(tokenNode.getAttributes().getNamedItem("term").getNodeValue());
                    } else if (tokenNode.getNodeName().equals("w")) {
                        toReturnAL.add(tokenNode.getAttributes().getNamedItem("tok").getNodeValue());
                    }/*from   w  ww  . j a  v a  2  s  . com*/

                }
            }
        }
    } else {
        NodeList sentences = doc.getChildNodes().item(1).getChildNodes();

        for (int i = 0; i < sentences.getLength(); i++) {
            if (!sentences.item(i).getNodeName().equals("s"))
                continue;
            NodeList tokens = sentences.item(i).getChildNodes();
            for (int j = 0; j < tokens.getLength(); j++) {
                Node tokenNode = tokens.item(j);
                if (tokenNode.getNodeName().equals("toponym")) {
                    toReturnAL.add(tokenNode.getAttributes().getNamedItem("term").getNodeValue());
                } else if (tokenNode.getNodeName().equals("w")) {
                    toReturnAL.add(tokenNode.getAttributes().getNamedItem("tok").getNodeValue());
                }

            }
        }
    }

    return toReturnAL.toArray(new String[0]);
}

From source file:Main.java

public static List<Element> getChildNodes(Node parent, String childName) {

    final List<Element> result = new ArrayList<Element>();

    final NodeList nodes = parent.getChildNodes();

    final int len = nodes.getLength();

    for (int i = 0; i < len; i++) {
        final Node n = nodes.item(i);
        if (!isElementNode(n)) {
            continue;
        }//from w w  w.j  a  v  a2 s  .com
        if (n.getNodeName().equals(childName)) {
            result.add((Element) n);
        }
    }
    return result;
}

From source file:Main.java

/***************************************************************************
 * Returns the value of the first child under the give element with the
 * given name.//from  w w  w  .j a v  a2s . com
 * 
 * @param e
 * @param name
 * @return
 * @throws Exception
 **************************************************************************/
public static String getChildValueByName(Element e, String name) throws Exception {
    String s = "Not found";

    /*
     * The getElementsByTagName() function returns ANY children under the
     * given element with the given tag name. This function is intended to
     * return the value of only an immediate child with a given name.
     */
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            if (node.getFirstChild() != null)
                s = node.getFirstChild().getNodeValue();
            else
                s = "";
            break;
        }
    }

    return s;
}

From source file:Main.java

public static ArrayList<Node> getNodeList(String xmlStr, String nodeName, Map<String, String> attributesMap) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    ArrayList<Node> returnNodeList = new ArrayList<Node>();
    try {/*  ww  w . ja va 2s  . c o m*/
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream inStream = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8));
        // or InputSource inputSource = new InputSource( new StringReader(
        // xmlStr ) );
        Document doc = db.parse(inStream);
        DocumentTraversal dt = (DocumentTraversal) doc;
        NodeIterator i = dt.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, false);
        Node node = i.nextNode();
        while (node != null) {
            if (node.getNodeName().equals(nodeName)) {
                if (attributesExist(node, attributesMap)) {
                    returnNodeList.add(node);
                }
            }
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
    return returnNodeList;
}

From source file:com.cuubez.visualizer.processor.ConfigurationProcessor.java

private static String getRootNodeName(final Document document) {

    Node node = document.getDocumentElement();
    return node.getNodeName();

}

From source file:Main.java

public static Node getNode(String xmlStr, String nodeName, Map<String, String> attributesMap) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Node returnNode = null;/*ww  w . j  ava2 s .  c om*/
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream inStream = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8));
        // or InputSource inputSource = new InputSource( new StringReader(
        // xmlStr ) );
        Document doc = db.parse(inStream);
        DocumentTraversal dt = (DocumentTraversal) doc;
        NodeIterator i = dt.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, false);
        Node node = i.nextNode();
        while (node != null) {
            if (node.getNodeName().equals(nodeName)) {
                if (attributesExist(node, attributesMap)) {
                    returnNode = node;
                    break;
                }
            }
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
    return returnNode;
}