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:Main.java

public static void removeNodeAttribute(Node node, String name) {

    if (node.hasAttributes()) {

        NamedNodeMap attrs = node.getAttributes();
        if (attrs.getNamedItem(name) != null) {
            attrs.removeNamedItem(name);
        }/*from  w  ww . ja va 2s  . com*/
    }

}

From source file:Main.java

/**
 * Gets the value from a node's attribute.
 * /*from   w  w  w  .ja va  2 s  . c o  m*/
 * @param node
 *            the node.
 * @param attributeName
 *            the name of the attribute.
 * @return the value of the attribute with the specified name or
 *         <code>null</code> if there is no attribute with that name.
 */
public static Node getAttributeValue(Node node, String attributeName) {
    return (node.hasAttributes()) ? node.getAttributes().getNamedItem(attributeName) : null;

}

From source file:Main.java

public static String getNodeAttribute(Node node, String name) {

    if (node.hasAttributes()) {

        NamedNodeMap attrs = node.getAttributes();

        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attribute = (Attr) attrs.item(i);

            if (attribute.getName().equals(name)) {
                return attribute.getValue();
            }/*w  w  w.  ja  v a 2s.com*/

        }
    }

    return null;

}

From source file:Main.java

public static Map<String, String> XmlAsMap(Node node) {
    Map<String, String> map = new HashMap<String, String>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.hasAttributes()) {
            for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
                Node item = currentNode.getAttributes().item(i);
                if (item != null)
                    map.put(item.getNodeName(), prepare(item.getTextContent()));
            }/*  ww w  .j  a va2  s .c om*/
        }
        if (currentNode.getFirstChild() != null) {
            if (currentNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
                map.putAll(XmlAsMap(currentNode));
            } else if (currentNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                map.put(currentNode.getLocalName(), prepare(currentNode.getTextContent()));
            }
        }
    }
    return map;
}

From source file:Main.java

/**
 *  Gets the attribute of the node.//from  w  w  w  .j a  va 2s .co  m
 *
 *@param  node              the node from which to get the attribute node
 *@param  name              the attribute name for which to get the node
 *@return                   The attribute node
 *@exception  DOMException  if an error occurs while getting the attribute
 *      node
 */
public final static Node getAttribute(Node node, String name) throws DOMException {
    if (node != null && node.hasAttributes()) {
        NamedNodeMap attrs = node.getAttributes();
        return attrs.getNamedItem(name);
    }
    return null;
}

From source file:Main.java

/**
 * Extract included filenames in the XML document, assuming that filenames are
 * provided with the attribute "href"./*from w  w w .j  av a  2  s . c  o  m*/
 * 
 * @param xmlDocument the XML document
 * @return the filenames to include
 */
private static Vector<String> extractIncludedFiles(Document xmlDocument) {

    Vector<String> includedFiles = new Vector<String>();

    NodeList top = xmlDocument.getChildNodes();
    for (int i = 0; i < top.getLength(); i++) {
        Node topNode = top.item(i);
        NodeList firstElements = topNode.getChildNodes();
        for (int j = 0; j < firstElements.getLength(); j++) {
            Node midNode = firstElements.item(j);
            for (int k = 0; k < midNode.getChildNodes().getLength(); k++) {
                Node node = midNode.getChildNodes().item(k);
                if (node.hasAttributes() && node.getAttributes().getNamedItem("href") != null) {
                    String fileName = node.getAttributes().getNamedItem("href").getNodeValue();
                    includedFiles.add(fileName);
                }
            }

        }
    }
    return includedFiles;
}

From source file:Main.java

/**
 * A method to get attribute value from provided xml node and attribute name
 * @param Node parent, a node where the attribute residing
 * @param String attr, attribute name to get
 * @return String , a value from request attribute 
 *//*from w ww  .jav a2 s. c om*/
static public String getAttribute(Node parent, String Attr) {
    String ret = "";

    if (!parent.hasAttributes())
        return ("");
    NamedNodeMap nmap = parent.getAttributes();

    for (int i = 0; i < nmap.getLength(); ++i) {
        Node n = nmap.item(i);
        if (n.getNodeName().trim().equals(Attr)) {
            ret = n.getNodeValue().trim();
        }
        //System.out.println("Attribute: "+n.getNodeType()+" - "+n.getNodeName()+" "+n.getNodeValue());

    }
    return (ret);
}

From source file:Main.java

private static String unevaluatedXML(String result, Node node) {
    String nodeName = node.getNodeName();
    String attributes = "";
    if (node.hasAttributes()) {
        NamedNodeMap XMLAttributes = node.getAttributes();
        for (int i = 0; i < XMLAttributes.getLength(); i++)

        {/*from w  ww.j  a  v  a  2 s. c  o  m*/
            attributes += " " + XMLAttributes.item(i).getNodeName() + "=\""
                    + XMLAttributes.item(i).getNodeValue() + "\"";
        }
    }
    if (result.equals(""))
        return " <" + nodeName + attributes + "/> ";
    else
        return " <" + nodeName + attributes + ">" + result + "</" + nodeName + "> "; // add spaces
}

From source file:Main.java

public static String lookupNamespaceURI(Node root, String specifiedPrefix) {
    if (root == null) {
        return null;
    }//from  w  ww .  ja  v  a  2s.  c  om
    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

public static List<String> getValuesFromDocumentByTagAndAttribute(Node parentNode, String tagName,
        String attributeName, String attributeValue) {
    ArrayList<String> values = new ArrayList<String>();

    for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) {
        Node childNode = parentNode.getChildNodes().item(i);

        if (childNode != null) {
            if (childNode.hasAttributes()) {
                for (int j = 0; j < childNode.getAttributes().getLength(); j++) {
                    Node attribute = childNode.getAttributes().item(j);
                    if (attribute.getNodeName().equals(attributeName)
                            && attribute.getNodeValue().equals(attributeValue)) {
                        values.add(childNode.getTextContent().trim());
                    }/* w  w w . ja va 2s.  c  o  m*/
                }
            }

            if (childNode.hasChildNodes()) {
                values.addAll(getValuesFromDocumentByTagAndAttribute(childNode, tagName, attributeName,
                        attributeValue));
            }
        }
    }

    return values;
}