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

/**
 * Returns a Map of all attributes of the given element with the given namespace.
 * Why can we not create our own NamedNodeMaps? This would save trouble.
 * @param element the elment from which to retrieve the attributes.
 * @param namespaceURI the namespace of the attributes to retrieve.
 * @return a Map containing the attributes names and their values.
 *///  www. j  a  v  a2  s . c o  m
public static Map getAttributesWithNS(Element element, String namespaceURI) {
    Map result = new HashMap();

    NamedNodeMap attributes = element.getAttributes();

    if (attributes == null)
        return result;

    for (int i = 0; i != attributes.getLength(); i++) {
        Node attribute = attributes.item(i);

        if (namespaceURI == null && attribute.getNamespaceURI() == null) {
            result.put(attribute.getNodeName(), attribute.getNodeValue());
        } else if (attribute.getNamespaceURI() != null && attribute.getNamespaceURI().equals(namespaceURI)) {
            result.put(attribute.getNodeName(), attribute.getNodeValue());
        }
    }

    return result;
}

From source file:Main.java

private static void findAllProcessingIstructions(Node node, String name, List<ProcessingInstruction> result) {
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return;/*from  ww w  . jav  a 2s.c  o m*/
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
            if (name == null || name.length() == 0 || n.getNodeName().equals(name)) {
                result.add((ProcessingInstruction) n);
            }
        }
        findAllProcessingIstructions(n, name, result);
    }
}

From source file:Main.java

public static Element[] getChildElementNodesByName(Node node, String name) {
    if (node == null) {
        return null;
    }//from www  .j a  v  a2 s.com

    ArrayList<Element> elements = new ArrayList<>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null && (node.getNodeType() == Node.ELEMENT_NODE)
                    && name.equalsIgnoreCase(item.getNodeName())) {
                elements.add((Element) item);
            }
        }
    }
    return elements.toArray(new Element[elements.size()]);
}

From source file:Main.java

public static Element getFirstChildElementByName(Element root, String tagName) {
    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  w  w  . ja v  a 2 s  .co m*/
        if (tagName.equals(child.getNodeName())) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

/**
 * check if given name is equal to name of the element (with and without namespace)
 * @param node//w w w. ja  v a  2  s . c  o m
 * @param k
 * @param caseSensitive
 * @return
 */
public static boolean nameEqual(Node node, String name, boolean caseSensitive) {
    if (name == null)
        return false;
    if (caseSensitive) {
        return name.equals(node.getNodeName()) || name.equals(node.getLocalName());
    }
    return name.equalsIgnoreCase(node.getNodeName()) || name.equalsIgnoreCase(node.getLocalName());
}

From source file:Main.java

/**
 * This method parse an Attributes tag, DTD for Attribute is as follows.
 * //from   w  ww  . j a  v  a2 s.  c om
 * <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;
        }

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

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;
                }/*from   w  w  w.ja v a  2  s . c om*/
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the occurrences of a given node (specified by its name)
 * in a given list of nodes. Counts all subsequent occurs after the
 * first one.//  w ww.ja  va 2 s . c om
 * 
 * @param nodeName The name of the node whose occurrences are requested
 * @param nodes      The list of nodes that will be searched for occurrences of
 *                nodeName.
 * @return         The number of the first subsequent occurences of nodeName. 
 *                Greater than or equal to 0.
 */
public static int getOccurs(String nodeName, NodeList nodes) {
    int occurs = 0;

    int childNr = 0;
    boolean foundFirstOccurence = false;
    while (childNr < nodes.getLength()) {
        Node node = nodes.item(childNr);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().compareTo(nodeName) == 0) {
                occurs++;
                foundFirstOccurence = true;
            } else {
                if (foundFirstOccurence) {
                    return occurs;
                }
            }
        }
        childNr++;
    }

    return occurs;
}

From source file:net.mindengine.oculus.frontend.domain.document.testcase.Testcase.java

public static Testcase parse(String xmlData) throws Exception {
    Testcase testcase = new Testcase();

    try {/*from   w w  w  . j a v  a  2s.c  om*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder builder = dbf.newDocumentBuilder();

        Reader reader = new CharArrayReader(xmlData.toCharArray());
        org.w3c.dom.Document doc = builder.parse(new org.xml.sax.InputSource(reader));

        Node root = doc.getDocumentElement();

        NodeList nodeList = root.getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if ("steps".equals(node.getNodeName())) {
                NodeList stepsList = node.getChildNodes();
                for (int j = 0; j < stepsList.getLength(); j++) {
                    Node n = stepsList.item(j);

                    TestcaseStep step = new TestcaseStep();
                    step.setAction(XmlUtils.getChildNodeText(n, "action"));
                    step.setExpected(XmlUtils.getChildNodeText(n, "expected"));
                    step.setComment(XmlUtils.getChildNodeText(n, "comment"));
                    testcase.steps.add(step);
                }
            }
        }
    } catch (Exception e) {
    }

    return testcase;
}

From source file:Main.java

public static QName getQName(Node n) {
    if (n.getLocalName() != null) {
        return new QName(n.getNamespaceURI(), n.getLocalName());
    } else {//from  w  w  w. j a v  a2s.  c o  m
        return new QName(n.getNamespaceURI(), n.getNodeName());
    }
}