Example usage for org.w3c.dom Node hasAttributes

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

Introduction

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

Prototype

public boolean hasAttributes();

Source Link

Document

Returns whether this node (if it is an element) has any attributes.

Usage

From source file:MainClass.java

static void listNodes(Node node) {
    String nodeName = node.getNodeName();

    if (node instanceof Element) {
        if (node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i); // Get an attribute
                System.out.println(" " + attribute.getName() + "=" + attribute.getValue());
            }/*  w ww . ja  va2  s.co  m*/
        }
        System.out.println(indent + "<" + nodeName + ">");
    } else if (node instanceof Text) {
        System.out.println(((Text) node).getData());
    } else if (node instanceof DocumentType) {
        System.out.println(getDoctypeString((DocumentType) node));
    }

    indent.append(' ');
    NodeList list = node.getChildNodes();
    if (list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
            listNodes(list.item(i));
        }
    }
    System.out.println("</" + nodeName + ">");

}

From source file:Main.java

public static String retrieveNodeAsString(Node node) {
    String nodeStr = new String("<");
    nodeStr += node.getNodeName() + internal;
    if (node.hasAttributes()) {
        NamedNodeMap attrs = node.getAttributes();
        // add the attrubite name-value pairs
        for (int i = 0; i < attrs.getLength(); i++) {
            Node a = attrs.item(i);
            nodeStr += a.getNodeName() + "=" + a.getNodeValue() + internal;
        }/*  w  ww.  ja v  a 2 s. c o  m*/
    }

    if (node.hasChildNodes()) {
        nodeStr += ">\n";
        NodeList ns = node.getChildNodes();
        for (int i = 0; i < ns.getLength(); i++) {
            nodeStr += logXMLSubNode(ns.item(i), 1);
        }
        nodeStr += "<" + node.getNodeName() + "/>\n";
    } else {
        nodeStr += "/>\n";
    }
    return nodeStr;
}

From source file:Main.java

/**
 * Constructs a XPath query to the supplied node.
 * //from w w w  .j  av a2s  .  c  o m
 * @param n
 * @return
 */
public static String getXPath(Node n) {
    if (null == n) {
        throw new IllegalArgumentException("Invalid node");
    }

    ArrayList<Node> hierarchy = new ArrayList<Node>();
    StringBuffer buffer = new StringBuffer();
    Node parent = null;

    // Push parent element's on stack
    hierarchy.add(n);
    parent = n.getParentNode();
    while (parent != null && parent.getNodeType() != Node.DOCUMENT_NODE) {
        hierarchy.add(0, parent);
        parent = parent.getParentNode();
    }

    Iterator<Node> i = hierarchy.iterator();
    while (i.hasNext()) {
        Node node = i.next();
        buffer.append("/");
        buffer.append(node.getNodeName());
        if (node.hasAttributes()) {
            Node uuid = node.getAttributes().getNamedItem("uuid");
            if (uuid != null) {
                buffer.append("[@uuid='");
                buffer.append(uuid.getNodeValue());
                buffer.append("']");
            }
        }
    }

    // return buffer
    return buffer.toString();
}

From source file:com.mingo.parser.xml.dom.DomUtil.java

/**
 * Transform node attributes to map.// ww w  .j av a2 s .  co m
 *
 * @param node {@link Node}
 * @return map : key - attribute name; value - attribute value
 */
public static Map<String, String> getAttributes(Node node) {
    Map<String, String> attributes = ImmutableMap.of();
    if (node.hasAttributes()) {
        ImmutableMap.Builder builder = ImmutableMap.builder();
        // get attributes names and values
        NamedNodeMap nodeMap = node.getAttributes();
        for (int i = 0; i < nodeMap.getLength(); i++) {
            Node currentNode = nodeMap.item(i);
            builder.put(currentNode.getNodeName(), currentNode.getNodeValue());
        }
        attributes = builder.build();
    }
    return attributes;
}

From source file:Main.java

public static void updateUserMgtXML(File userMgtXML, String jndiConfigNameUserDB) throws Exception {
    Document doc = initializeXML(userMgtXML);
    NodeList configNodeList = doc.getElementsByTagName("Configuration");
    Node configNode = configNodeList.item(0); //get the 0 index, since only one available

    NodeList nodeList = configNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equalsIgnoreCase("Property")) {
                if (node.hasAttributes()) {
                    NamedNodeMap namedNodeMap = node.getAttributes();
                    Node attr = namedNodeMap.getNamedItem("name");
                    if (attr != null && attr.getNodeValue().equalsIgnoreCase("dataSource")) {
                        node.setTextContent(jndiConfigNameUserDB);
                    }/* w ww.ja  v a  2 s .  c  o m*/
                }
            }
        }
    }
    finalizeXML(userMgtXML, doc, 4);
}

From source file:Main.java

private static Map<?, ?> getNodeBean(Node parent) {
    Map<Object, Object> rtn = new HashMap<Object, Object>();

    if (parent != null) {
        Map<Object, Object> attrMap = new HashMap<Object, Object>();
        if (parent.hasAttributes()) {
            NamedNodeMap attrs = parent.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                attr.getNodeName();//w  w  w. j  a v  a  2s.  c om
                attr.getNodeValue();
                attrMap.put(attr.getNodeName(), attr.getNodeValue());
            }
        }

        rtn.put("tagName", parent.getNodeName());
        rtn.put("attr", attrMap);

        NodeList nodeList = parent.getChildNodes();
        if (nodeList != null) {
            List<Object> children = new ArrayList<Object>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    children.add(getNodeBean(child));
                }
            }

            rtn.put("children", children);
        }
    }

    return rtn;
}

From source file:Main.java

private static void printNote(NodeList nodeList, int depth) {
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
            System.out.println(depth + "Node Value =" + tempNode.getTextContent());
            if (tempNode.hasAttributes()) {
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                }//  w  w w . j  a  va  2 s.  com
            }
            if (tempNode.hasChildNodes()) {
                printNote(tempNode.getChildNodes(), depth + 1);
            }
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
        }
    }
}

From source file:com.mingo.parser.xml.dom.DomUtil.java

/**
 * Gets attribute by name./*ww w  .j  a v a2s.  co m*/
 *
 * @param node          {@link Node}
 * @param attributeName attribute name
 * @return attribute value with type 'string'
 */
public static String getAttributeString(Node node, String attributeName) {
    Validate.notNull(node, "getAttributeString::node cannot be null");
    String value = StringUtils.EMPTY;
    if (node.hasAttributes()) {
        value = getAttributes(node).get(attributeName);
    }
    return value;
}

From source file:Main.java

/**
 * Output a DOM node including children using a log4j logger.
 * If logger is null it will just output to system out.
 * It will indent by the number of tabs passed in.
 * This method recursively calls itself to output
 * children nodes./*from w w  w  .j av a  2s.com*/
 * 
 * @param logger
 * @param n
 * @param tabs
 */
public static void outputNode(Logger logger, Node n, int tabs) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < tabs; i++) {
        sb.append("\t");
    }
    sb.append("<" + n.getNodeName());

    if (n.hasAttributes()) {
        NamedNodeMap nnMap = n.getAttributes();
        for (int i = 0; i < nnMap.getLength(); i++) {
            Node att = nnMap.item(i);
            sb.append(" " + att.getNodeName() + "=\"" + att.getNodeValue() + "\"");
        }
    }
    sb.append(">");
    sb = printBuffer(logger, sb, true);

    for (int i = 0; i < tabs + 1; i++) {
        sb.append("\t");
    }
    sb.append(n.getNodeValue());
    sb = printBuffer(logger, sb, true);

    if (n.hasChildNodes()) {
        NodeList nodes = n.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            outputNode(nodes.item(i), tabs + 1);
        }
    }
    for (int i = 0; i < tabs; i++) {
        sb.append("\t");
    }
    sb.append("</" + n.getNodeName() + ">");
    sb = printBuffer(logger, sb, true);
}

From source file:TryDOM.java

static void listNodes(Node node, String indent) {
        String nodeName = node.getNodeName();
        System.out.println(indent + nodeName + " Node, type is " + node.getClass().getName() + ":");
        System.out.println(node);

        if (node instanceof Element && node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i);
                System.out.println(indent + attribute.getName() + "=" + attribute.getValue());
            }/*from   ww  w .j a  va  2s  .co m*/
        }
        NodeList list = node.getChildNodes();
        if (list.getLength() > 0) {
            System.out.println(indent + "Child Nodes of " + nodeName + " are:");
            for (int i = 0; i < list.getLength(); i++)
                listNodes(list.item(i), indent + " ");
        }
    }