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:lineage2.gameserver.utils.XMLUtil.java

/**
 * Method getAttributeLongValue.//from ww w  .  j  av  a  2s.c  om
 * @param n Node
 * @param item String
 * @param dflt long
 * @return long
 */
public static long getAttributeLongValue(Node n, String item, long dflt) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null) {
        return dflt;
    }
    final String val = d.getNodeValue();
    if (val == null) {
        return dflt;
    }
    return Long.parseLong(val);
}

From source file:Main.java

/**
 * get the value of an Attribute in the Xml Document.
 * finds the first occurance of the parent element and then searches its attributes
 * for the occurance of the attribute and retrieves its value.
 * @param root the root Element.//  w ww . ja  v  a 2s  .  c o m
 * @param elemName the name of the element to search for.
 * @param att the name of the attribute  to search for.
 * @return String the attribute value or null if not found.
 */
public static String getElementAttribute(Element root, String elemName, String att) {
    NodeList nl = root.getElementsByTagName(elemName);
    if (null == nl) {
        return (null);
    }
    Node n = nl.item(0);
    if (null == n) {
        return (null);
    }
    NamedNodeMap attributes = n.getAttributes();
    if (null == attributes) {
        return (null);
    }
    n = attributes.getNamedItem(att);
    if (null == n) {
        return (null);
    }
    return (n.getNodeValue().trim());

}

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()));
            }/*w ww.  ja v a2  s.co  m*/
        }
        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:lineage2.gameserver.utils.XMLUtil.java

/**
 * Method getAttributeBooleanValue.//from w  ww  .j a va 2 s.c om
 * @param n Node
 * @param item String
 * @param dflt boolean
 * @return boolean
 */
public static boolean getAttributeBooleanValue(Node n, String item, boolean dflt) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null) {
        return dflt;
    }
    final String val = d.getNodeValue();
    if (val == null) {
        return dflt;
    }
    return Boolean.parseBoolean(val);
}

From source file:Main.java

public static void addAttribute(Document document, Node parent, String attrName, String value) {
    Attr attr = document.createAttribute(attrName);
    attr.setValue(value);//from ww w . j  a v a  2 s  . c o  m
    NamedNodeMap map = parent.getAttributes();
    map.setNamedItem(attr);
}

From source file:Main.java

/**
 * This method yields the Attribute at the node extracted by the expression.
 * @param parentNode The node to start at.
 * @param expression The XPath expression to evaluate.
 * @param attribute The Name of the attribute you would like to extract from the
 * XPath'd node.//from  w  w w  .  j av a  2 s .  c o m
 * @return The Value of the attribute, or the empty String if no matching node
 * or matching attribute could be found.
 * @throws XPathExpressionException
 *
 * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a>
 */
public static String getAttribute(Node parentNode, String expression, String attribute)
        throws XPathExpressionException {
    String attr = "";

    Node n = getNode(parentNode, expression);

    if (n != null) {
        n = n.getAttributes().getNamedItem(attribute);
    }

    if (n != null) {
        attr = n.getNodeValue();
    }

    return attr;
}

From source file:Main.java

public static String getProcessIdFromBpmn(final String bpmn) {
    String processId = null;//  ww  w.  ja  v  a 2  s . c  o  m
    try {
        final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        final Document doc = domFactory.newDocumentBuilder()
                .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8")));
        final XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {
            @Override
            public Iterator<?> getPrefixes(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getPrefix(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getNamespaceURI(final String prefix) {
                // Only require the URI for the bpmn2 NS.
                return BPMN2_NAMESPACE_URI;
            }
        });
        final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR);

        final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue();
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return processId;
}

From source file:Main.java

public static String getAttributeValue(Node elementNode, String attributeName) {
    if (elementNode == null) {
        return null;
    }//from  ww  w.j av a  2 s  .c  om
    NamedNodeMap attributes = elementNode.getAttributes();
    if (attributes == null) {
        return null;
    }
    Node attribute = attributes.getNamedItem(attributeName);
    if (attribute == null) {
        return null;
    }
    return attribute.getNodeValue();
}

From source file:Main.java

public static void addAttribute(Document doc, Node parentNode, String name, String content) {
    Attr attNode = doc.createAttribute(name);
    attNode.setNodeValue(content);//from www .  j ava 2 s .  co m
    parentNode.getAttributes().setNamedItem(attNode);
}

From source file:Main.java

private static void appendAttributes(Node node, StringBuffer sb) {
    if (node instanceof Element) {
        NamedNodeMap nodeMap = node.getAttributes();
        for (int i = 0; i < nodeMap.getLength(); i++) {
            sb.append(' ');
            sb.append(nodeMap.item(i).getNodeName());
            sb.append('=');
            sb.append('"');
            sb.append(nodeMap.item(i).getNodeValue());
            sb.append('"');
        }//w w  w. j  a va2 s.c o  m
    }
}