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 String getAttr(Node node, String name) throws DOMException { assert node != null; try {/*from ww w.jav a 2s . c om*/ NamedNodeMap attributes = node.getAttributes(); if (attributes == null) throw new Exception(""); Node attr = attributes.getNamedItem(name); if (attr == null) throw new Exception(""); String value = attr.getNodeValue(); if (value == null) throw new Exception(""); return value; } catch (Exception e) { throw new DOMException(DOMException.NOT_FOUND_ERR, String.format("attribute %s is missing at node %s", name, node.getNodeName())); } }
From source file:Main.java
public static String lookupNamespaceURI(Node root, String specifiedPrefix) { if (root == null) { return null; }/* ww w . ja v a 2s.c o m*/ 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:com.wso2telco.workflow.utils.WorkflowProperties.java
public static Map<String, String> loadWorkflowPropertiesFromXML() { if (propertiesMap == null) { try {/*from www.ja v a 2 s .co m*/ propertiesMap = new HashMap<String, String>(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); String carbonHome = System.getProperty("carbon.home"); String workflowPropertiesFile = carbonHome + "/repository/conf/" + Constants.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(Constants.SERVICE_HOST) || nodeName.equalsIgnoreCase(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_HOST) || nodeName.equalsIgnoreCase(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_ADDRESS) || nodeName .equalsIgnoreCase(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_PASSWORD) || nodeName.equalsIgnoreCase(Constants.PUBLISHER_ROLE_START_WITH) || nodeName.equalsIgnoreCase(Constants.PUBLISHER_ROLE_END_WITH) || nodeName.equalsIgnoreCase(Constants.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 List<Map<String, String>> readXMLFile(String outFile) { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); List<Map<String, String>> returnlist = new ArrayList<Map<String, String>>(); try {/*from www . ja v a 2 s .c om*/ DocumentBuilder dombuilder = domfac.newDocumentBuilder(); InputStream is = new FileInputStream(outFile); Document doc = dombuilder.parse(is); NodeList nl = doc.getElementsByTagName("row"); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); NodeList fileds = node.getChildNodes(); Map<String, String> map = new HashMap<String, String>(); for (int j = 0; j < fileds.getLength(); j++) { Node filed = fileds.item(j); if (filed.getNodeType() == Node.ELEMENT_NODE) { map.put(filed.getAttributes().getNamedItem("name").getNodeValue(), filed.getFirstChild().getNodeValue()); } } returnlist.add(map); } } catch (Exception e) { e.printStackTrace(); } return returnlist; }
From source file:Main.java
public static void addAttribute(Document document, Node node, String name, String value) { if (value != null) { Attr attribute = document.createAttribute(name); attribute.setValue(value);/*from w w w . j a v a 2 s . co m*/ node.getAttributes().setNamedItem(attribute); } }
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++) {//w w w . ja v a 2 s . co 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 Color toColor(Node n) { if (!n.getNodeName().equals("color")) { throw new IllegalArgumentException(n.getNodeName()); }//w w w . ja v a 2 s. com NamedNodeMap map = n.getAttributes(); String s = map.getNamedItem("name").getNodeValue(); if (s.equals("white")) { return Color.WHITE; } else if (s.equals("green")) { return Color.GREEN; } else if (s.equals("pink")) { return Color.PINK; } else if (s.equals("cyan")) { return Color.CYAN; } else if (s.equals("yellow")) { return Color.YELLOW; } else { return Color.WHITE; } }
From source file:Main.java
/** * Parse the childnodes of a node and look for <property> elements with attributes name and value. * Example://ww w . j a v a 2 s .c o m * <node> * <property name='n' value='v'/> * <node/> * @param n the XML node * @return the parsed properties */ public static Map<String, String> parseProperties(Node n) { NodeList propertyNodes = n.getChildNodes(); Map<String, String> properties = new HashMap<String, String>(); for (int i = 0; i < propertyNodes.getLength(); i++) { Node node = propertyNodes.item(i); if (node.getNodeName().equals("property")) { try { String key = node.getAttributes().getNamedItem("name").getNodeValue(); String value = node.getAttributes().getNamedItem("value").getNodeValue(); properties.put(key, value); } catch (NullPointerException e) { continue; } } } return properties; }
From source file:Main.java
/** * @param n Node to examine/*from w w w . j av a 2s . com*/ * @param attr Attribute to look for * @param def Default value to return if attribute is not present * @return if the Node contains the named Attribute, the value, if not, the * def parameter */ public static String getAttribute(Node n, String attr, String def) { NamedNodeMap attrs = n.getAttributes(); if (attrs == null) { return def; } Node ret = attrs.getNamedItem(attr); if (ret == null) { return def; } else { return ret.getNodeValue(); } }
From source file:Main.java
/** * Add the specified attribute.// w w w. j av a 2 s .co m * @param e The node to add the attribute to. * @param name The name of the attribute. * @param value The value of the attribute. */ public static void addAttribute(final Node e, final String name, final String value) { final Attr attr = e.getOwnerDocument().createAttribute(name); attr.setValue(value); e.getAttributes().setNamedItem(attr); }