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

private static String getStringValue(String attrName, Map<String, String> runtimeAttributes, Node docElement) {
    String stringValue = runtimeAttributes.get(attrName);
    if (stringValue == null) {
        Node staleNode = docElement.getAttributes().getNamedItem(attrName);
        if (staleNode != null) {
            stringValue = staleNode.getNodeValue();
        }/*from w  ww . j a va 2s .c o  m*/
    }
    return stringValue;
}

From source file:com.wso2telco.hub.workflow.extensions.util.WorkflowProperties.java

public static Map<String, String> loadWorkflowPropertiesFromXML() {
    if (propertiesMap == null) {
        try {/* w  w w. j  av  a  2  s .  c  om*/
            propertiesMap = new HashMap<String, String>();

            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            String carbonHome = System.getProperty("carbon.home");
            String workflowPropertiesFile = carbonHome + "/repository/conf/" + WORKFLOW_PROPERTIES_XML_FILE;

            Document document = builder.parse(new File(workflowPropertiesFile));
            Element rootElement = document.getDocumentElement();

            NodeList nodeList = rootElement.getElementsByTagName("Property");
            if (nodeList != null && nodeList.getLength() > 0) {
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Node node = nodeList.item(i);
                    String nodeName = node.getAttributes().getNamedItem("name").getNodeValue();
                    if (nodeName.equalsIgnoreCase(SERVICE_HOST)
                            || nodeName.equalsIgnoreCase(KEY_WORKFLOW_EMAIL_NOTIFICATION_HOST)
                            || nodeName.equalsIgnoreCase(KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_ADDRESS)
                            || nodeName.equalsIgnoreCase(KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_PASSWORD)
                            || nodeName.equalsIgnoreCase(PUBLISHER_ROLE_START_WITH)
                            || nodeName.equalsIgnoreCase(PUBLISHER_ROLE_END_WITH)
                            || nodeName.equalsIgnoreCase(MANDATE_SERVICE_HOST)) {
                        String value = ((Element) node).getTextContent();
                        propertiesMap.put(nodeName, value);
                    } else {
                        //Not a matching property
                    }
                }
            }
        } catch (Exception e) {
            String errorMessage = "Error in WorkflowProperties.loadWorkflowPropertiesFromXML";
            log.error(errorMessage, e);
        }
    } else {
        //Return already loaded propertiesMap
    }
    return propertiesMap;
}

From source file:Main.java

public static void walkNodes(Node nodeIn, StringBuffer sb, String sPad) {
    if (nodeIn == null)
        return;/*from  www  . j a  v  a  2 s .  c o  m*/
    NamedNodeMap map = nodeIn.getAttributes();
    if (map != null)
        for (int i = 0; i < map.getLength(); i++) {
            Node n = map.item(i);
            if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
                sb.append(sPad + "   Attribute:" + n.getNodeName() + " = " + n.getNodeValue() + '\n');
            }
        }
    NodeList nodes = nodeIn.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(sPad + "Element: " + n.getNodeName() + '\n');
        }
        if (n.getNodeType() == Node.TEXT_NODE) {
            sb.append(sPad + "   Value: = " + n.getNodeValue() + '\n');
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            walkNodes(n, sb, sPad + "   ");
        }
    }
}

From source file:Main.java

public static void addAttribute(Document xmlDoc, Node node, String attrName, String attrValue) {
    Attr newAtt = xmlDoc.createAttribute(attrName);
    newAtt.setNodeValue(attrValue);/*from  w  ww .  ja  va2s . c  o  m*/
    NamedNodeMap attrs = node.getAttributes();
    attrs.setNamedItem(newAtt);
}

From source file:Main.java

/**
 * Returns the attribute value for the passed name in the passed node.
 * @param node the node to get the attribute from
 * @param name the name of the attribute
 * @return The attribute value or null if it is not found.
 *///from  w w w  .  j  a va 2  s.  c o m
public static String getAttributeValueByName(Node node, String name) {
    return getAttributeValueByName(node.getAttributes(), name);
}

From source file:Main.java

public static String getPrefix(String uri, Node e) {
    while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) {
        NamedNodeMap attrs = e.getAttributes();
        for (int n = 0; n < attrs.getLength(); n++) {
            Attr a = (Attr) attrs.item(n);
            String name;//  w  w  w  . j a  va2s. com
            if ((name = a.getName()).startsWith("xmlns:") && a.getNodeValue().equals(uri)) {
                return name.substring(6);
            }
        }
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

/**
 * Returns the given attribute of a node, if it exists
 * Otherwise, returns null//w  w  w  .  j  a  v  a  2 s .c  om
 */
static Node get_named_attribute(Node node, String att_name) {
    if (node == null)
        return null;

    // Does the node have attributes?
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        // Does the node have this attribute?
        return attributes.getNamedItem(att_name);
    }

    // No attributes
    return null;
}

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  w  w. j a v a2  s  .  co  m
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

/**
 * Set node's attribute./*from   w  w  w.  j a v  a2  s  . c  o  m*/
 * 
 * @param node
 *          the node
 * @param attributeName
 *          the attribute name
 * @param value
 *          the value
 */
public static void setNodeAttributeValue(Node node, String attributeName, String value) {
    if (null == node) {
        // Do Nothing
        return;
    }
    NamedNodeMap attributes = node.getAttributes();
    if (null == attributes) {
        // Do Nothing
        return;
    }
    Node n = attributes.getNamedItem(attributeName);
    if (null == n) {
        // Do Nothing
        return;
    }
    n.setNodeValue(value);
}

From source file:Main.java

/**
 * Returns a map of the passed node's attributes
 * @param node The nopde to get an attribute map for
 * @return a [possibly empty] map of the node's attributes
 *///w  w w .  j ava2 s . c om
public static Map<String, String> getAttributeMap(final Node node) {
    if (node == null)
        throw new IllegalArgumentException("The passed node was null");
    final NamedNodeMap nnm = node.getAttributes();
    final int size = nnm.getLength();
    if (size == 0)
        return Collections.emptyMap();
    final Map<String, String> map = new LinkedHashMap<String, String>(size);
    for (int i = 0; i < size; i++) {
        final Attr attr = (Attr) nnm.item(i);
        map.put(attr.getName(), attr.getValue());
    }
    return map;
}