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

/**
 * This method parse an Attributes tag, DTD for Attribute is as follows.
 * /*  w  w w  .  j ava 2s  . c o m*/
 * <pre>
 *  &lt; !-- This DTD defines the DPro Attribute tag.
 *    Unique Declaration name for DOCTYPE tag:
 *    &quot;Directory Pro 5.0 Attributes DTD&quot;
 *  --&gt;
 *  &lt; !ELEMENT Attributes (Attribute)+&gt;
 *  &lt; !ELEMENT Attribute EMPTY&gt;
 *  &lt; !ATTLIST Attribute
 *       name    NMTOKEN         #REQUIRED
 *  &gt;
 * </pre>
 * 
 * @param n
 *            Node
 * @return Set Set of the attribute names
 */
public static Set parseAttributesTag(Node n) {
    // get Attribute node list
    NodeList attributes = n.getChildNodes();
    final int numAttributes = attributes.getLength();

    if (numAttributes == 0) {
        return null;
    }

    Set set = new HashSet();
    for (int l = 0; l < numAttributes; l++) {
        // get next attribute
        Node attr = attributes.item(l);
        if ((attr.getNodeType() != Node.ELEMENT_NODE) && !attr.getNodeName().equals("Attribute")) {
            // need error handling
            continue;
        }

        String attrName = ((Element) attr).getAttribute("name");
        set.add(attrName);
    }
    return set;
}

From source file:Main.java

public static String readProjectName(String file) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    InputStream is = new FileInputStream(file);
    Document doc = dombuilder.parse(is);
    Element root = doc.getDocumentElement();
    NodeList prjInfo = root.getChildNodes();
    if (prjInfo != null) {
        for (int i = 0; i < prjInfo.getLength(); i++) {
            Node project = prjInfo.item(i);
            if (project.getNodeType() == Node.ELEMENT_NODE) {
                String strProject = project.getNodeName();
                if (strProject.equals("project")) {
                    for (Node node = project.getFirstChild(); node != null; node = node.getNextSibling()) {
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                            String strNodeName = node.getNodeName();
                            if (strNodeName.equals("name")) {
                                return node.getTextContent();
                            }/*ww  w.  j a v  a2 s  .  c o m*/
                        }
                    }
                }
            }

        }
    }
    return "";
}

From source file:Main.java

/**
 *   Find the first child node matching the given tag name. Only searches 
 *   1st-level children; not a recursive search. Null if not found.
 *///w  w  w .ja  va  2  s  .com
public static Node findChildNodeMatching(Node root, String tagName, short nodeType) {
    Node childNode = root.getFirstChild();
    while (childNode != null) {
        if (childNode.getNodeType() == nodeType) {
            if (tagName.equals(childNode.getNodeName())) {
                return childNode;
            }
        }

        childNode = childNode.getNextSibling();
    }

    return null;
}

From source file:Main.java

/**
 * Search a first node by name in the document
 * /*w ww . j av  a  2s.co  m*/
 * @param doc      source document
 * @param nodeName   the node name for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodesByName(Document doc, String nodeName) throws Exception {

    Node node = null;

    NodeList nl = doc.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        node = nl.item(i);
        if (node.getNodeName().equals(nodeName)) {
            break;
        }
    }

    return node;

}

From source file:Main.java

public static List<Element> getChildElementsByName(Element root, String tagName) {
    List<Element> tags = new LinkedList<Element>();
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*from  w  ww.j ava2  s .c o  m*/
        if (tagName.equals(child.getNodeName())) {
            tags.add((Element) child);
        }
    }
    return tags;
}

From source file:com.dinstone.jrpc.spring.EndpointBeanDefinitionParser.java

private static boolean nodeMatch(Node node, String desiredName) {
    return (desiredName.equals(node.getNodeName()) || desiredName.equals(node.getLocalName()));
}

From source file:Main.java

/**
 * Print all the attributes of the given node
 *
 * @param parent//from  w  ww  . j  a  v  a2s.com
 */
public static void printNode(Node parent) {
    if (parent == null) {
        System.out.println("null node!");
        return;
    }
    System.out.println(parent.getNodeName() + " node:");
    NamedNodeMap attrs = parent.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attribute = (Attr) attrs.item(i);
        System.out.println("  " + attribute.getName() + " = " + attribute.getValue());
    }
}

From source file:Main.java

/**
 * Gets the name of the node.// w ww .j  a  va2 s .  co  m
 * @param node the node
 * @return the name, first trying the local name (getLocalName()), but if null or zero-length, the node name (getNodeName())
 */
public static String nameOf(Node node) {
    if (node != null) {
        String name = node.getLocalName();
        if (name == null || name.length() == 0) {
            name = node.getNodeName();
        }
        return name;
    }
    return null;
}

From source file:Main.java

/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(Node parent, String elemName) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals(elemName)) {
                return (Element) child;
            }//from   w ww .  j  av  a  2  s.c o m
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static Node getFirstElementByTagName(Node parentNode, String tagName) {
    Node element = null;//  w w w . ja  v  a  2s .c om

    if (!isElement(parentNode)) {
        return null;
    } else {
        if (parentNode.getNodeName().equals(tagName)) {
            return parentNode;
        } else {
            NodeList childList = parentNode.getChildNodes();
            for (int nodeIx = 0; nodeIx < childList.getLength(); nodeIx++) {
                Node node = childList.item(nodeIx);
                element = getFirstElementByTagName(node, tagName);
                if (element != null) {
                    return element;
                }
            }
        }
    }

    return null;
}