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
private static void convert(Node toCopy, Node saveTo, Document doc) { Node newNode;/*from w w w . j a va2 s . c o m*/ switch (toCopy.getNodeType()) { case Node.ELEMENT_NODE: Element newElement = doc.createElementNS(toCopy.getNamespaceURI(), toCopy.getNodeName()); newNode = newElement; Element baseElement = (Element) toCopy; NamedNodeMap children = baseElement.getAttributes(); for (int i = 0; i < children.getLength(); i++) { convertAttribute((Attr) children.item(i), newElement, doc); } break; case Node.TEXT_NODE: newNode = doc.createTextNode(toCopy.getTextContent()); break; default: newNode = null; } if (newNode != null) { NodeList children = toCopy.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { convert(children.item(i), newNode, doc); } saveTo.appendChild(newNode); } }
From source file:Main.java
/** * Returns a Map of all attributes of the given element with the given namespace. * Why can we not create our own NamedNodeMaps? This would save trouble. * @param element the elment from which to retrieve the attributes. * @param namespaceURI the namespace of the attributes to retrieve. * @return a Map containing the attributes names and their values. *//*from w ww .j a v a 2s. c o m*/ public static Map getAttributesWithNS(Element element, String namespaceURI) { Map result = new HashMap(); NamedNodeMap attributes = element.getAttributes(); if (attributes == null) return result; for (int i = 0; i != attributes.getLength(); i++) { Node attribute = attributes.item(i); if (namespaceURI == null && attribute.getNamespaceURI() == null) { result.put(attribute.getNodeName(), attribute.getNodeValue()); } else if (attribute.getNamespaceURI() != null && attribute.getNamespaceURI().equals(namespaceURI)) { result.put(attribute.getNodeName(), attribute.getNodeValue()); } } return result; }
From source file:Utils.java
/** * Get all prefixes defined on this element for the specified namespace. * //from w ww .jav a 2 s .com * @param element * @param namespaceUri * @param prefixes */ public static void getPrefixes(Element element, String namespaceUri, List<String> prefixes) { NamedNodeMap atts = element.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { Node node = atts.item(i); String name = node.getNodeName(); if (namespaceUri.equals(node.getNodeValue()) && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) { prefixes.add(node.getPrefix()); } } }
From source file:Main.java
protected static Map<String, String> createAttributeTable(Element e) { Map<String, String> attributeTable = new HashMap<>(); NamedNodeMap attrs = e.getAttributes(); if (attrs != null) { int numAttrs = attrs.getLength(); for (int i = 0; i < numAttrs; i++) { Node na = attrs.item(i); if (na.getNodeType() != Node.ATTRIBUTE_NODE) { continue; }// w ww. j ava 2 s . co m Attr a = (Attr) na; attributeTable.put(a.getName(), a.getValue()); } } return attributeTable; }
From source file:Main.java
@Nullable public static Map<String, String> getAllAttributesAsMap(@Nullable final Element aSrcNode) { if (aSrcNode != null) { final NamedNodeMap aNNM = aSrcNode.getAttributes(); if (aNNM != null) { final Map<String, String> aMap = new LinkedHashMap<String, String>(aNNM.getLength()); final int nMax = aNNM.getLength(); for (int i = 0; i < nMax; ++i) { final Attr aAttr = (Attr) aNNM.item(i); aMap.put(aAttr.getName(), aAttr.getValue()); }/*w ww .j av a2 s .c om*/ return aMap; } } return null; }
From source file:Main.java
/** * Extract default values of variables defined in the DOM subtree below node. * Default values are used to populate the variableDefs map. Variables * already defined in this map will NOT be modified. * * @param node root node of DOM subtree to extract default values from. * @param variableDefs map which default values will be added to. *//* w w w . jav a 2s. c o m*/ public static void extractVariableDefaults(final Node node, Map<String, String> variableDefs) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: final Element element = (Element) node; final NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); extractVariableDefaultsFromString(attr.getValue(), variableDefs); } break; case Node.CDATA_SECTION_NODE: String content = node.getTextContent(); extractVariableDefaultsFromString(content, variableDefs); break; default: break; } final NodeList children = node.getChildNodes(); for (int childIndex = 0; childIndex < children.getLength(); childIndex++) extractVariableDefaults(children.item(childIndex), variableDefs); }
From source file:Main.java
public static Element overrideXml(Element target, Element parent) { if (parent != null) { NamedNodeMap namedNodeMap = parent.getAttributes(); for (int i = 0; i < namedNodeMap.getLength(); i++) { Node attributeNode = namedNodeMap.item(i); String parentAttributeName = attributeNode.getNodeName(); String parentAttributeValue = attributeNode.getNodeValue(); // attribute override if (!target.hasAttribute(parentAttributeName)) { target.setAttribute(parentAttributeName, parentAttributeValue); }// w w w . jav a 2 s . com // children override if (parent.getChildNodes().getLength() > 0) { if (target.getChildNodes().getLength() == 0) { for (int j = 0; j < target.getChildNodes().getLength(); j++) { target.appendChild(target.getChildNodes().item(j)); } } } } } return target; }
From source file:Main.java
public static String elementToString(Element e) { StringBuilder buf = new StringBuilder(); buf.append(e.getTagName()).append("{"); for (int i = 0; i < e.getAttributes().getLength(); i++) { if (i > 0) { buf.append(", "); }/*w ww. jav a 2s . c o m*/ Node n = e.getAttributes().item(i); buf.append(attributeName((Attr) n)).append("=").append(n.getNodeValue()); } buf.append("}"); return buf.toString(); }
From source file:Main.java
public static void addAttribute(Document doc, String name, Element e, String value) { Node attrNode = doc.createAttribute(name); attrNode.setNodeValue(value);/*from w w w. j ava 2 s .com*/ NamedNodeMap attrs = e.getAttributes(); attrs.setNamedItem(attrNode); }
From source file:Main.java
public static String findPrefixForNamespace(Element elm, String namespace) { while (elm != null) { NamedNodeMap attributes = elm.getAttributes(); for (int c = 0; c < attributes.getLength(); c++) { if (attributes.item(c).getNodeValue().equals(namespace) && attributes.item(c).getNodeName().startsWith("xmlns:")) { return attributes.item(c).getNodeName().substring(6); }// w ww .j av a 2 s. co m } if (elm.getParentNode().getNodeType() != Node.ELEMENT_NODE) break; elm = (Element) elm.getParentNode(); } return null; }