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

/**
 * Build {@link Map} of namespace URIs to prefixes.
 * /*from ww w.j  ava 2  s .c  o  m*/
 * @param root
 *          {@link Element} to get namespaces and prefixes from.
 * @return {@link Map} of namespace URIs to prefixes.
 * @since 8.1
 */
private static final Map<String, String> buildNamespacePrefixMap(final Element root) {
    final HashMap<String, String> namespacePrefixMap = new HashMap<>();

    //Look for all of the attributes of cache that start with
    //xmlns
    NamedNodeMap attributes = root.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node item = attributes.item(i);
        if (item.getNodeName().startsWith("xmlns")) {
            //Anything after the colon is the prefix
            //eg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            //has a prefix of xsi
            String[] splitName = item.getNodeName().split(":");
            String prefix;
            if (splitName.length > 1) {
                prefix = splitName[1];
            } else {
                prefix = "";
            }
            String uri = item.getTextContent();
            namespacePrefixMap.put(uri, prefix);
        }
    }

    return namespacePrefixMap;
}

From source file:Utils.java

public static String getPrefix(Element el, String ns) {
    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (ns.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            return node.getPrefix();
        }//from   w w  w .  ja v a  2s.  c  o  m
    }
    return null;
}

From source file:Main.java

public static void printNode(Node node, String indent) {
    String nodeName = node.getNodeName();
    System.out.print(indent);/*from   ww w. j  a va 2  s  .c  om*/

    if (nodeName.equals("#text")) {
        String nodeText = node.getNodeValue();

        if ((nodeText != null) && (nodeText.trim().length() > 0)) {
            System.out.print(nodeText.trim());
        }
    } else {
        System.out.print(nodeName);
    }

    System.out.print(" ");

    if (!nodeName.equals("#text")) {
        NamedNodeMap attrs = node.getAttributes();

        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                System.out.print(attr.getNodeName() + "=\"" + attr.getNodeValue() + "\" ");
            }
        }
    }

    NodeList children = node.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        System.out.println();
        printNode(children.item(i), indent + "\t");
    }
}

From source file:Main.java

/**
 * Return children of the {@code node} parameter with a matching {@code nodeName} &
 * {@code attributeName} that matches at least one of the passed-in {@code attributeValues}.
 * If {@code attributeValues} is empty, no nodes will match. To match names only,
 * pass null for both {@code attributeName} and {@code attributeValues}.
 *
 * @param node the root node to look beneath.
 * @param nodeName all child nodes will match this element.
 * @param attributeName all matching child nodes will have an attribute of this name.
 * @param attributeValues all matching child nodes' matching attribute will have a value that
 *                        matches one of these values.
 * @return child nodes that match all parameters
 */// ww w  .ja  v a 2 s .c  o m
static List<Node> getMatchingChildNodes(final Node node, final String nodeName, final String attributeName,
        final List<String> attributeValues) {
    if (node == null || nodeName == null) {
        return null;
    }

    final List<Node> nodes = new ArrayList<Node>();
    final NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeName().equals(nodeName)
                && nodeMatchesAttributeFilter(childNode, attributeName, attributeValues)) {
            nodes.add(childNode);
        }
    }
    return nodes;
}

From source file:Main.java

/**
 * Get a list of the attribute names of an element.
 * @param e Element to use//from w  w  w  . j a va  2 s. c  om
 * @return a list with all the names of the attributes of the node
 */
public static List<String> getAttributeNames(final Element e) {

    if (e == null) {
        return null;
    }

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

    final NamedNodeMap map = e.getAttributes();

    for (int i = 0; i < map.getLength(); i++) {

        final Node attribute = map.item(i);

        result.add(attribute.getNodeName());
    }

    return result;
}

From source file:Main.java

/**
 * used to parse out elements of a list such as this:
 * <node>/*from  ww w.java2 s . c  om*/
 *    <element>value1</element>
 *    <element>value2</element>
 *    <element>value3</element>
 * </node>
 *
 * for this example, called with Node pointing to <node> element and "element" 
 * in listElementName.
 * @param listNode pointer to list's "root" node
 * @param listElementName name of list element
 * @return Vector of list elements 
 */
public static Vector getListElements(Node listNode, String listElementName) {
    String tmp = "";
    Vector ret = null;

    NodeList children = listNode.getChildNodes();
    if (children != null) {
        ret = new Vector();

        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);

            if (child.getNodeName().equalsIgnoreCase(listElementName)) {
                /* value is in element node's only (text) child */
                String value = child.getChildNodes().item(0).getNodeValue();
                if ((value != null) && !value.equals("")) {
                    ret.add(value);
                }
            }
        }
    }
    return ret;
}

From source file:Main.java

/**
 * This method will compare the attributes of a given src element with the attributes of a given dest element.
 * Both must contain the same attributes, but you can specify a String array of attribute names whose values
 * are ignored. Both elements must have the ignore attributes, their contents can be different and they will
 * still be considered equal./*ww  w.  jav  a  2s  . co m*/
 * @param src - the src element whose attributes are to be compared
 * @param dest - the dest element whose attributes are to be compared
 * @param ignore - the string array of attributes whose values are to be ignored during the compare.
 * @return true if the attributes of both nodes meet the criteria of being equal, false otherwise.
 */
private static boolean compareAttributes(Element src, Element dest, String[] ignore) {
    NamedNodeMap srcAttrs = src.getAttributes();

    if (srcAttrs.getLength() != dest.getAttributes().getLength())
        return false;

    for (int ctr = 0; ctr < srcAttrs.getLength(); ctr++) {
        Node srcAttr = srcAttrs.item(ctr);

        String name = srcAttr.getNodeName();
        if (Arrays.binarySearch(ignore, name) < 0) {
            Node destAttr = dest.getAttributeNode(name);
            if (destAttr == null || !srcAttr.isEqualNode(destAttr)) {
                return false;
            }
        }
    }

    return true;
}

From source file:Main.java

public static String lookupNamespaceURI(Node root, String specifiedPrefix) {
    if (root == null) {
        return null;
    }// w w w . ja v a2  s  . c  o m
    if (root.hasAttributes()) {
        NamedNodeMap nnm = root.getAttributes();
        for (int i = 0; i < nnm.getLength(); i++) {
            Node n = nnm.item(i);
            if (("xmlns".equals(n.getPrefix()) && specifiedPrefix.equals(n.getNodeName()))
                    || ("xmlns:" + specifiedPrefix).equals(n.getNodeName())) {
                return n.getNodeValue();
            }
        }
    }
    return lookupNamespaceURI(root.getParentNode(), specifiedPrefix);
}

From source file:Main.java

/**
 * Get siblings of the same type as element from parent.
 * //  ww w . j  a v a  2s.c  o  m
 * @param parent
 *            parent node.
 * @param element
 *            element.
 * @return List of sibling (from element) under parent
 */
public static List<Node> getSiblings(Node parent, Node element) {
    List<Node> result = new ArrayList<Node>();
    NodeList list = parent.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node el = list.item(i);

        if (el.getNodeName().equals(element.getNodeName())) {
            result.add(el);
        }
    }

    return result;
}

From source file:Main.java

public static List<Node> getChildrenByTagName(Node node, String element) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node childNode = childNodes.item(j);
        if ("*".equals(element) || element == null || childNode.getNodeName().equals(element)) {
            result.add(childNode);/*from  ww  w  .j  a v  a2 s  .  c  o  m*/
        }
    }
    return result;
}