List of usage examples for org.w3c.dom Element getAttributes
public NamedNodeMap getAttributes();
NamedNodeMap
containing the attributes of this node (if it is an Element
) or null
otherwise. From source file:Main.java
/** * get attribute map from the element//from w w w .jav a 2 s . c om * @param el - element * @return mapping of attributes */ public static Map<String, String> getAttributes(Element el) { Map<String, String> list = new LinkedHashMap<String, String>(); NamedNodeMap map = el.getAttributes(); for (int i = 0; i < map.getLength(); i++) { list.put(map.item(i).getNodeName(), map.item(i).getNodeValue()); } return list; }
From source file:Main.java
public static HashMap<String, String> getEleAttrNameValueMap(Element ele) { HashMap<String, String> ht = new HashMap<String, String>(); NamedNodeMap nnm = ele.getAttributes(); int len = nnm.getLength(); Node tmpn = null;/*w w w . j a va 2 s.c o m*/ for (int k = 0; k < len; k++) { tmpn = nnm.item(k); String tmps = tmpn.getNodeValue(); if (tmps == null) { tmps = ""; } ht.put(tmpn.getNodeName(), tmps); } return ht; }
From source file:Main.java
/** * Add an attribute to an Element./*from w w w. ja va 2 s . c o m*/ * @param doc XML document * @param element XML Element * @param attribute attribute name * @param value attribute value */ public static void addAttribute(final Document doc, final Element element, final String attribute, final String value) { final NamedNodeMap atts = element.getAttributes(); final Attr newAttr = doc.createAttribute(attribute); newAttr.setValue(value); atts.setNamedItem(newAttr); }
From source file:Main.java
public static void edit(Document doc) { Element element = doc.getDocumentElement(); Element element2 = doc.createElement("newname"); NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr2 = (Attr) doc.importNode(attrs.item(i), true); element2.getAttributes().setNamedItem(attr2); }// w w w.j a v a2s . c o m while (element.hasChildNodes()) { element2.appendChild(element.getFirstChild()); } element.getParentNode().replaceChild(element2, element); }
From source file:Main.java
public static List<Attr> getAttributes(Element element) { List<Attr> attributes = new LinkedList<Attr>(); NamedNodeMap namedNodeMap = element.getAttributes(); for (int i = 0; i < namedNodeMap.getLength(); i++) { Node node = namedNodeMap.item(i); if (node.getNodeType() == Node.ATTRIBUTE_NODE) { Attr a = (Attr) node; attributes.add(a);//from w w w . ja va2 s. co m } } return attributes; }
From source file:Main.java
/** * Copies all attributes from one element to another in the official way. *//* w w w.j a v a 2 s. c o m*/ public static void copyAttributes(Element elementFrom, Element elementTo) { NamedNodeMap nodeList = elementFrom.getAttributes(); if (nodeList == null) { // No attributes to copy: just return return; } Attr attrFrom = null; Attr attrTo = null; // Needed as factory to create attrs Document documentTo = elementTo.getOwnerDocument(); int len = nodeList.getLength(); // Copy each attr by making/setting a new one and // adding to the target element. for (int i = 0; i < len; i++) { attrFrom = (Attr) nodeList.item(i); // Create an set value attrTo = documentTo.createAttribute(attrFrom.getName()); attrTo.setValue(attrFrom.getValue()); // Set in target element elementTo.setAttributeNode(attrTo); } }
From source file:Main.java
/** * This method will compare the attributes of a given src element with the attributes of a given dest element. * Both must contain the same attributes, but you can specify a String array of attribute names whose values * are ignored. Both elements must have the ignore attributes, their contents can be different and they will * still be considered equal.//from w w w . j a v a 2 s . c o m * @param src - the src element whose attributes are to be compared * @param dest - the dest element whose attributes are to be compared * @param ignore - the string array of attributes whose values are to be ignored during the compare. * @return true if the attributes of both nodes meet the criteria of being equal, false otherwise. */ private static boolean compareAttributes(Element src, Element dest, String[] ignore) { NamedNodeMap srcAttrs = src.getAttributes(); if (srcAttrs.getLength() != dest.getAttributes().getLength()) return false; for (int ctr = 0; ctr < srcAttrs.getLength(); ctr++) { Node srcAttr = srcAttrs.item(ctr); String name = srcAttr.getNodeName(); if (Arrays.binarySearch(ignore, name) < 0) { Node destAttr = dest.getAttributeNode(name); if (destAttr == null || !srcAttr.isEqualNode(destAttr)) { return false; } } } return true; }
From source file:Main.java
public static void addAttribute(Document doc, Element node, String name, String value) { Attr attribute = doc.createAttribute(name); attribute.setValue(value);//from w w w . j ava 2s.c o m node.getAttributes().setNamedItem(attribute); }
From source file:Main.java
/** Return the attributes from the specified element as a Map */ public static Map<String, String> getAttributesAsMap(Element e) { Map<String, String> result = new HashMap<String, String>(); NamedNodeMap attrs = e.getAttributes(); if (attrs != null) { int len = attrs.getLength(); for (int i = 0; i < len; i++) { Node n = attrs.item(i); if (n instanceof Attr) { Attr a = (Attr) n; result.put(a.getName(), a.getValue()); }/*w w w.j a v a 2 s . c o m*/ } } return result; }
From source file:Main.java
public static String getAttributeValue(Element start, String attrName) { if (start.getNodeType() == Element.ELEMENT_NODE) { NamedNodeMap startAttr = start.getAttributes(); for (int i = 0; i < startAttr.getLength(); i++) { Node attr = startAttr.item(i); if (attrName.equals(attr.getNodeName().trim())) return attr.getNodeValue().trim(); }/*from w w w . j av a2 s .c om*/ return null; } else return null; }