Example usage for org.w3c.dom Node getAttributes

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

Introduction

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

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:Main.java

/**
 * Clones the given DOM node into the given DOM document.
 * /*from   ww w. jav  a  2s .c o  m*/
 * @param node
 *            The DOM node to clone.
 * @param doc
 *            The target DOM document.
 * @return The cloned node in the target DOM document.
 */
public static Node cloneNode(Node node, Document doc) {
    Node clone = null;
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = doc.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            Attr attrClone = doc.createAttribute(attrNode.getNodeName());
            attrClone.setNodeValue(attrNode.getNodeValue());
            ((Element) clone).setAttributeNode(attrClone);
        }

        // Iterate through each child nodes.
        NodeList childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node childNode = childNodes.item(i);
                if (childNode == null) {
                    continue;
                }
                Node childClone = cloneNode(childNode, doc);
                if (childClone == null) {
                    continue;
                }
                clone.appendChild(childClone);
            }
        }
        break;

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        clone = doc.createTextNode(node.getNodeName());
        clone.setNodeValue(node.getNodeValue());
        break;
    }
    return clone;
}

From source file:MainClass.java

public static void print(Node node, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        ps.print("<" + node.getNodeName());

        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ps.print(" " + map.item(i).getNodeName() + "=\"" + map.item(i).getNodeValue() + "\"");
        }/*from   www  .  ja v  a 2  s.  c  o m*/
        ps.println(">");
        return;
    case Node.ATTRIBUTE_NODE:
        ps.println(node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
        return;
    case Node.TEXT_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.CDATA_SECTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        ps.println(node.getNodeName() + "=" + node.getNodeValue());
        return;
    }
}

From source file:DomUtil.java

public static void setAttribute(Node node, String attName, String val) {
    NamedNodeMap attributes = node.getAttributes();
    Node attNode = node.getOwnerDocument().createAttribute(attName);
    attNode.setNodeValue(val);
    attributes.setNamedItem(attNode);//from  ww  w.  j a  v a 2s .  co  m
}

From source file:Main.java

/**
 *  Gets a named attribute of a Node<p>
 *  @param node The node to search/* w  w w .j  av a2s  .co  m*/
 *  @param attr The name of the attribute to get
 */
public synchronized static String getAttribute(Node node, String attr) {
    if (node == null)
        throw new IllegalArgumentException("Node argument cannot be null");
    if (attr == null)
        throw new IllegalArgumentException("Node attribute argument cannot be null");

    NamedNodeMap map = node.getAttributes();
    if (map != null) {
        Node an = map.getNamedItem(attr);
        if (an != null)
            return an.getNodeValue();
    }

    return null;
}

From source file:Main.java

/**
 * Returns {@code true} iff the node has the attribute {@code attributeName} with a value that
 * matches one of {@code attributeValues}.
 *//* w  ww .  j  a  va2s .  c om*/
static boolean nodeMatchesAttributeFilter(final Node node, final String attributeName,
        final List<String> attributeValues) {
    if (attributeName == null || attributeValues == null) {
        return true;
    }

    final NamedNodeMap attrMap = node.getAttributes();
    if (attrMap != null) {
        Node attrNode = attrMap.getNamedItem(attributeName);
        if (attrNode != null && attributeValues.contains(attrNode.getNodeValue())) {
            return true;
        }
    }

    return false;
}

From source file:org.owasp.benchmark.tools.BenchmarkCrawler.java

public static String getAttributeValue(String name, Node node) {
    if (node == null) {
        return null;
    }/*from   w  ww  . j  av  a  2s. c om*/
    NamedNodeMap nnm = node.getAttributes();
    if (nnm != null) {
        Node attrnode = nnm.getNamedItem(name);
        if (attrnode != null) {
            String value = attrnode.getNodeValue();
            if (value.equals("[random]")) {
                value = getToken();
            }
            //System.out.println("El value es: " + value);
            return value;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Searches for a node within a DOM document with a given node path expression.
 * Elements are separated by '.' characters.
 * Example: Foo.Bar.Poo/*from  ww  w. j av  a 2 s  . c om*/
 * @param doc DOM Document to search for a node.
 * @param pathExpression dot separated path expression
 * @return Node element found in the DOM document.
 */
public static Node findNodeByName(Document doc, String pathExpression) {
    final StringTokenizer tok = new StringTokenizer(pathExpression, ".");
    final int numToks = tok.countTokens();
    NodeList elements;
    if (numToks == 1) {
        elements = doc.getElementsByTagNameNS("*", pathExpression);
        return elements.item(0);
    }

    String element = pathExpression.substring(pathExpression.lastIndexOf('.') + 1);
    elements = doc.getElementsByTagNameNS("*", element);

    String attributeName = null;
    if (elements.getLength() == 0) {
        //No element found, but maybe we are searching for an attribute
        attributeName = element;

        //cut off attributeName and set element to next token and continue
        Node found = findNodeByName(doc,
                pathExpression.substring(0, pathExpression.length() - attributeName.length() - 1));

        if (found != null) {
            return found.getAttributes().getNamedItem(attributeName);
        } else {
            return null;
        }
    }

    StringBuffer pathName;
    Node parent;
    for (int j = 0; j < elements.getLength(); j++) {
        int cnt = numToks - 1;
        pathName = new StringBuffer(element);
        parent = elements.item(j).getParentNode();
        do {
            if (parent != null) {
                pathName.insert(0, '.');
                pathName.insert(0, parent.getLocalName());//getNodeName());

                parent = parent.getParentNode();
            }
        } while (parent != null && --cnt > 0);
        if (pathName.toString().equals(pathExpression)) {
            return elements.item(j);
        }
    }

    return null;
}

From source file:com.jaeksoft.searchlib.util.XPathParser.java

private final static String getAttributeString(Node node, String attributeName, boolean unescapeXml) {
    NamedNodeMap attr = node.getAttributes();
    if (attr == null)
        return null;
    Node n = attr.getNamedItem(attributeName);
    if (n == null)
        return null;
    String t = n.getTextContent();
    if (t == null)
        return null;
    return unescapeXml ? StringEscapeUtils.unescapeXml(t) : t;
}

From source file:Main.java

public static void printTree(Node doc) {
    if (doc == null) {
        System.out.println("Nothing to print!!");
        return;/*w  w  w  .  j  a v a2 s.c o  m*/
    }
    try {
        System.out.println(doc.getNodeName() + "  " + doc.getNodeValue());
        NamedNodeMap cl = doc.getAttributes();
        for (int i = 0; i < cl.getLength(); i++) {
            Node node = cl.item(i);
            System.out.println("\t" + node.getNodeName() + " ->" + node.getNodeValue());
        }
        NodeList nl = doc.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            printTree(node);
        }
    } catch (Throwable e) {
        System.out.println("Cannot print!! " + e.getMessage());
    }
}

From source file:Main.java

public static StringBuilder append(Node n, StringBuilder buf, int level) {
    if (n instanceof CharacterData)
        return _level(buf, level).append(n.getNodeValue()).append("\n");

    _level(buf, level).append("<").append(n.getNodeName());
    NamedNodeMap attr = n.getAttributes();
    if (attr != null) {
        for (int i = 0; i < attr.getLength(); i++) {
            Node a = attr.item(i);
            buf.append(" ").append(a.getNodeName()).append("=\"").append(a.getNodeValue()).append("\" ");
        }//from  www  .  j  a v  a2 s. co  m
    }

    NodeList children = n.getChildNodes();
    if (children == null || children.getLength() == 0)
        return buf.append("/>\n");
    buf.append(">\n");

    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        append(c, buf, level + 1);
    }

    return _level(buf, level).append("</").append(n.getNodeName()).append(">\n");
}