List of usage examples for org.w3c.dom NamedNodeMap item
public Node item(int index);
index
th item in the map. From source file:Main.java
/** * Clones the given DOM node into the given DOM document. * /*w ww.jav a 2 s. c o m*/ * @param node * The DOM node to clone. * @param doc * The target DOM document. * @return The cloned node in the target DOM document. */ public static Node cloneNode(Node node, Document doc) { Node clone = null; switch (node.getNodeType()) { case Node.ELEMENT_NODE: clone = doc.createElement(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attrNode = attrs.item(i); Attr attrClone = doc.createAttribute(attrNode.getNodeName()); attrClone.setNodeValue(attrNode.getNodeValue()); ((Element) clone).setAttributeNode(attrClone); } // Iterate through each child nodes. NodeList childNodes = node.getChildNodes(); if (childNodes != null) { for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode == null) { continue; } Node childClone = cloneNode(childNode, doc); if (childClone == null) { continue; } clone.appendChild(childClone); } } break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: clone = doc.createTextNode(node.getNodeName()); clone.setNodeValue(node.getNodeValue()); break; } return clone; }
From source file:Main.java
public static StringBuilder append(Node n, StringBuilder buf, int level) { if (n instanceof CharacterData) return _level(buf, level).append(n.getNodeValue()).append("\n"); _level(buf, level).append("<").append(n.getNodeName()); NamedNodeMap attr = n.getAttributes(); if (attr != null) { for (int i = 0; i < attr.getLength(); i++) { Node a = attr.item(i); buf.append(" ").append(a.getNodeName()).append("=\"").append(a.getNodeValue()).append("\" "); }/* ww w . ja va 2 s . c o m*/ } NodeList children = n.getChildNodes(); if (children == null || children.getLength() == 0) return buf.append("/>\n"); buf.append(">\n"); for (int i = 0; i < children.getLength(); i++) { Node c = children.item(i); append(c, buf, level + 1); } return _level(buf, level).append("</").append(n.getNodeName()).append(">\n"); }
From source file:Main.java
public static void removeNamespaceDeclarations(Node node, String... namespaces) { NamedNodeMap namedNodeMap = ((Element) node).getAttributes(); for (int nameIndex = 0; nameIndex < namedNodeMap.getLength(); nameIndex++) { Node namedNode = namedNodeMap.item(nameIndex); String uri = namedNode.getNamespaceURI(); String localName = namedNode.getLocalName(); if (uri != null && uri.equals("http://www.w3.org/2000/xmlns/")) { for (String removeableNamespace : namespaces) { if (namedNode.getNodeValue().equals(removeableNamespace)) { ((Element) node).removeAttributeNS("http://www.w3.org/2000/xmlns/", localName); nameIndex--;/* ww w . ja va 2 s.co m*/ } } } } }
From source file:Utils.java
/** * Starting from a node, find the namespace declaration for a prefix. for a matching namespace * declaration.//from w ww. j a v a 2 s .com * * @param node search up from here to search for namespace definitions * @param searchPrefix the prefix we are searching for * @return the namespace if found. */ public static String getNamespace(Node node, String searchPrefix) { Element el; while (!(node instanceof Element)) { node = node.getParentNode(); } el = (Element) node; NamedNodeMap atts = el.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { Node currentAttribute = atts.item(i); String currentLocalName = currentAttribute.getLocalName(); String currentPrefix = currentAttribute.getPrefix(); if (searchPrefix.equals(currentLocalName) && XMLNAMESPACE.equals(currentPrefix)) { return currentAttribute.getNodeValue(); } else if (isEmpty(searchPrefix) && XMLNAMESPACE.equals(currentLocalName) && isEmpty(currentPrefix)) { return currentAttribute.getNodeValue(); } } Node parent = el.getParentNode(); if (parent instanceof Element) { return getNamespace((Element) parent, searchPrefix); } return null; }
From source file:Main.java
/** * Gets the ID attribute of a DOM element. * /* ww w. j a va2s .co m*/ * @param domElement the DOM element * * @return the ID attribute or null if there isn't one */ public static Attr getIdAttribute(Element domElement) { if (!domElement.hasAttributes()) { return null; } NamedNodeMap attributes = domElement.getAttributes(); Attr attribute; for (int i = 0; i < attributes.getLength(); i++) { attribute = (Attr) attributes.item(i); if (attribute.isId()) { return attribute; } } return null; }
From source file:Main.java
/** * Rename an element, replacing it in its document. * @param element the element to rename. * @param name the new element name.// www. j a va2 s . c om * @return the renamed element. */ public static Element renameElement(Element element, String name) { if (element.getNodeName().equals(name)) return element; Element el = element.getOwnerDocument().createElement(name); //Copy the attributes NamedNodeMap attributes = element.getAttributes(); int nAttrs = attributes.getLength(); for (int i = 0; i < nAttrs; i++) { Node attr = attributes.item(i); el.setAttribute(attr.getNodeName(), attr.getNodeValue()); } //Copy the children Node node = element.getFirstChild(); while (node != null) { Node clone = node.cloneNode(true); el.appendChild(clone); node = node.getNextSibling(); } //Replace the element element.getParentNode().replaceChild(el, element); return el; }
From source file:Main.java
private static void fixupAttrsSingle(Element e) throws DOMException { removeXmlBase(e);//from w w w . j a va2s . c o m Map<String, String> replace = new HashMap<String, String>(); NamedNodeMap attrs = e.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Attr attr = (Attr) attrs.item(j); if (attr.getNamespaceURI() == null && !attr.getName().equals("xmlns")) { // NOI18N replace.put(attr.getName(), attr.getValue()); } } for (Map.Entry<String, String> entry : replace.entrySet()) { e.removeAttribute(entry.getKey()); e.setAttributeNS(null, entry.getKey(), entry.getValue()); } }
From source file:Main.java
/** * Puts the node attributes (if it has any) into HashMap<String,String> Retrieves only those * attributes that are in attr ArrayList Ignores other attributes values that are in node * //from w ww .j av a2 s. c om * @param node : XML node to look for its attributes * @param attrNames : attributes to fetch from node * @return Hashmap<String,String> of the node attributes */ public static HashMap<String, String> getAttributesByName(Node node, ArrayList<String> attrNames) { String attrName = null; String attrValue = null; HashMap<String, String> attributesMap = new HashMap<String, String>(); NamedNodeMap attributes = node.getAttributes(); for (int attrIndex = 0; attrIndex < attributes.getLength(); attrIndex++) { attrName = attributes.item(attrIndex).getNodeName(); attrValue = attributes.item(attrIndex).getNodeValue(); if (attrNames.contains(attrName)) { attributesMap.put(attrName, attrValue); } } return attributesMap.size() == 0 ? null : attributesMap; }
From source file:DOMEdit.java
private static void outputElement(Element node, String indent) { System.out.print(indent + "<" + node.getTagName()); NamedNodeMap nm = node.getAttributes(); for (int i = 0; i < nm.getLength(); i++) { Attr attr = (Attr) nm.item(i); System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\""); }/* ww w . j av a2 s . com*/ System.out.println(">"); NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) outputloop(list.item(i), indent + TAB); System.out.println(indent + "</" + node.getTagName() + ">"); }
From source file:Main.java
public static Map<String, String> getAttrs(Element ele) { NamedNodeMap nodeMap = ele.getAttributes(); Map<String, String> attrs = new HashMap<String, String>(nodeMap.getLength()); for (int i = 0; i < nodeMap.getLength(); i++) { attrs.put(nodeMap.item(i).getNodeName(), nodeMap.item(i).getNodeValue()); }/* w w w.j a v a 2 s.c om*/ return attrs; }