List of usage examples for org.w3c.dom Node getAttributes
public NamedNodeMap getAttributes();
NamedNodeMap
containing the attributes of this node (if it is an Element
) or null
otherwise. From source file:Main.java
public static void setNodeAttribute(Document doc, Node n, String attrName, String value) { Node namedAttr = n.getAttributes().getNamedItem(attrName); if (namedAttr == null) { namedAttr = doc.createAttribute(attrName); n.getAttributes().setNamedItem(namedAttr); }//from ww w .java 2 s . c o m namedAttr.setNodeValue(value); }
From source file:Main.java
/** * Retrieves the value for a xml Node attribute or fails if not found. * /* w ww . j a v a2 s .c om*/ * @param <T> the exception type to throw * @param node The Node to fetch the value from. * @param name The attribute name. * @param e an Exception instance * @return the attribute value * @throws T */ public static <T extends Exception> String getNodeAttributeOrFail(Node node, String name, T e) throws T { NamedNodeMap attributes = node.getAttributes(); Node valueNode = attributes.getNamedItem(name); if (valueNode == null) throw e; return valueNode.getNodeValue(); }
From source file:Main.java
public static Node findNodeByAttribute(Document document, String tagName, String attributeName, String attributeValue) {/*from ww w . ja va 2 s . co m*/ Node foundNode = null; NodeList nodes = document.getElementsByTagName(tagName); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); for (int j = 0; j < node.getAttributes().getLength(); j++) { Node attribute = node.getAttributes().item(j); if (attribute.getNodeName().equals(attributeName) && attribute.getNodeValue().equals(attributeValue)) { foundNode = node; break; } } if (foundNode != null) break; } return foundNode; }
From source file:Main.java
public static String getDefaultNamespaceUri(URL xmlResource, String rootElementName) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);//from w w w .java 2 s .com DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(xmlResource.openStream()); NodeList nodes = document.getElementsByTagName(rootElementName); if (nodes == null || nodes.getLength() == 0) { throw new IllegalArgumentException("Root element \"" + rootElementName + "\" not found in xml \"" + xmlResource.toExternalForm() + "\"."); } for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); Node xmlns = node.getAttributes().getNamedItem("xmlns"); if (xmlns != null) { String value = xmlns.getNodeValue(); return value.substring(value.indexOf("=") + 1); } } return null; }
From source file:Main.java
@CheckForNull public static Node nodeAttribute(Node node, String attribute) { NamedNodeMap attributes = node.getAttributes(); if (attributes == null) { return null; }//from w ww .jav a 2s . c o m return attributes.getNamedItem(attribute); }
From source file:Main.java
public static String getAttribute(Node node, String name) { NamedNodeMap m = node.getAttributes(); if (m == null) return null; Node att = m.getNamedItem(name); if (att == null) return null; return att.getNodeValue(); }
From source file:Main.java
public static Double getAttributeValueAsDouble(Node node, String attributeName, Double defaultValue) { final Node attributeNode = node.getAttributes().getNamedItem(attributeName); return attributeNode != null ? Double.parseDouble(attributeNode.getTextContent()) : defaultValue; }
From source file:Main.java
public static String getNodeAttributeValue(Node node, String attrName) { NamedNodeMap attrs = node.getAttributes(); if (attrs == null) return (null); Node value = attrs.getNamedItem(attrName); if (value == null) return (null); return (value.getNodeValue()); }
From source file:Main.java
/** * Returns whether the given XML DOM node contains all of the given attributes. * @param node the XML node to examine//from ww w . j ava2 s.co m * @param names a variable-length list of attribute names to check * @return true if all attributes exist, false if not */ public static boolean hasAttributes(Node node, String... names) { NamedNodeMap attrs = node.getAttributes(); for (String name : names) { if (attrs.getNamedItem(name) == null) { return false; } } return true; }
From source file:Main.java
public static String getAttribute(Node node, String attributeName) { String attributeValue = null; if (node.getAttributes().getNamedItem(attributeName) != null) { attributeValue = node.getAttributes().getNamedItem(attributeName).getNodeValue(); }/*from w ww . ja v a2s .c om*/ return attributeValue; }