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
/** * Gets the names of all the attributes of the given {@link Element}. * * @param element The {@link Element} to get the attribute names of. * @return Returns an array, possibly empty, of the attribute names. *///w ww . j a va2s . com public static String[] getAttributesNamesOf(Element element) { final NamedNodeMap map = element.getAttributes(); final int numAttributes = map.getLength(); final String[] result = new String[numAttributes]; for (int i = 0; i < numAttributes; ++i) result[i] = map.item(i).getNodeName(); return result; }
From source file:DOMCopy.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() + "\""); }/*from w w w .ja va 2 s . c om*/ 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
/** * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found. * If it is not found, returns a null.//from www . ja va2 s . co m * @param nnm NamedNodeMap * @param name String * @return String */ public static String getAttributeValueByName(NamedNodeMap nnm, String name) { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); if (attr.getName().equalsIgnoreCase(name)) { return attr.getValue(); } } return null; }
From source file:Main.java
/** Takes XML node and prints to String. * * @param node Element to print to String * @return XML node as String/*from w w w . j a v a 2 s . c om*/ */ private static void printNode(StringBuffer sBuffer, Node node) { if (node.getNodeType() == Node.TEXT_NODE) { sBuffer.append(encodeXMLText(node.getNodeValue().trim())); } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) { sBuffer.append("<![CDATA["); sBuffer.append(node.getNodeValue()); sBuffer.append("]]>"); } else if (node.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) node; sBuffer.append("<").append(el.getTagName()); NamedNodeMap attribs = el.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Attr nextAtt = (Attr) attribs.item(i); sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\""); } NodeList nodes = node.getChildNodes(); if (nodes.getLength() == 0) { sBuffer.append("/>"); } else { sBuffer.append(">"); for (int i = 0; i < nodes.getLength(); i++) { printNode(sBuffer, nodes.item(i)); } sBuffer.append("</").append(el.getTagName()).append(">"); } } }
From source file:Main.java
/** * Identify variables in attribute values and CDATA contents and replace * with their values as defined in the variableDefs map. Variables without * definitions are left alone./*www . ja v a 2 s.c o m*/ * * @param node the node to do variable replacement in * @param variableDefs map from variable names to values. */ public static void replaceVariables(final Node node, Map<String, String> variableDefs) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: final Element element = (Element) node; final NamedNodeMap atts = element.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { final Attr attr = (Attr) atts.item(i); attr.setNodeValue(replaceVariablesInString(attr.getValue(), variableDefs)); } break; case Node.CDATA_SECTION_NODE: String content = node.getTextContent(); node.setNodeValue(replaceVariablesInString(content, variableDefs)); break; default: break; } // process children final NodeList children = node.getChildNodes(); for (int childIndex = 0; childIndex < children.getLength(); childIndex++) replaceVariables(children.item(childIndex), variableDefs); }
From source file:Main.java
/** * This is an ugly hack, there must be a nicer way to do it. * Using getPrefix doesn't work because it stops us from getting * xmlns: attributes, which is what I'm using this for. Using * getNamespace doesn't seem to work on xmlns attributes either. * Trims the prefix on the map key./*w w w . j a v a 2s . co m*/ * @param element the element from which to retrieve the attributes. * @param prefix the prefix of the attributes to retrieve * @return a Map containing the attributes names and their values. */ public static Map getAttributesWithPrefix(Element element, String prefix) { Map result = new HashMap(); prefix += ":"; NamedNodeMap attributes = element.getAttributes(); if (attributes == null) return result; for (int i = 0; i != attributes.getLength(); i++) { Node attribute = attributes.item(i); if (attribute.getNodeName().startsWith(prefix)) { result.put(attribute.getNodeName().substring(prefix.length()), attribute.getNodeValue()); } } return result; }
From source file:Main.java
/** * //from www.ja v a 2s.co m * @param currentNode * @param tagName * @param attributeValue * @return */ public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName, String attributeValue) { String result = ""; NodeList childNodeList = currentNode.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); switch (childNode.getNodeType()) { case Node.DOCUMENT_NODE: break; case Node.ELEMENT_NODE: Element childElement = (Element) childNodeList.item(i); // logger.debug("childElement name : " + childElement.getTagName()); if (childElement != null && childElement.getNodeName().equals(tagName)) { NamedNodeMap attributes = childElement.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { Node current = attributes.item(j); if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) { result = childElement.getTextContent(); break; } } } case Node.TEXT_NODE: // logger.debug("textElement name : " + currentNode.getNodeValue()); break; case Node.COMMENT_NODE: break; case Node.PROCESSING_INSTRUCTION_NODE: break; case Node.ENTITY_REFERENCE_NODE: break; case Node.DOCUMENT_TYPE_NODE: break; } } return result; }
From source file:Main.java
/** * Copy one node to another node.//from ww w.j av a2 s .c o m * @param source source Node * @param dest destination Node * @return destination Node */ public static synchronized Node copyNode(Node source, Node dest) { if (source.getNodeType() == Node.TEXT_NODE) { Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue()); return tn; } Node attr = null; NamedNodeMap attrs = source.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { attr = attrs.item(i); ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue()); } } Node child = null; NodeList list = source.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { child = list.item(i); if (!(child instanceof Text)) { Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName()); if (child.getNodeValue() != null) { en.setNodeValue(child.getNodeValue()); } Node n = copyNode(child, en); dest.appendChild(n); } else if (child instanceof CDATASection) { CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue()); dest.appendChild(cd); } else { Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue()); dest.appendChild(tn); } } return dest; }
From source file:Main.java
public static String getAttrsAsString(Node node) { if (node == null) return ""; NamedNodeMap attrs = node.getAttributes(); StringBuilder val = new StringBuilder(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node nval = attrs.item(i); if (nval != null) { if (i > 0) val.append(", "); val.append(nval.getNodeName()).append("=").append(nval.getNodeValue()); }/*from w ww . j a va 2 s . c o m*/ } } return val.toString().trim(); }
From source file:Main.java
private static void print(Node e, String tab) { if (e.getNodeType() == Node.TEXT_NODE) { System.out.println(tab + e.getNodeValue()); return;/*w w w.j a v a 2 s. c o m*/ } System.out.print(tab + e.getNodeName()); NamedNodeMap as = e.getAttributes(); if (as != null && as.getLength() > 0) { System.out.print(" attributes=["); for (int i = 0; i < as.getLength(); i++) System.out.print((i == 0 ? "" : ", ") + as.item(i)); System.out.print("]"); } System.out.println(); if (e.getNodeValue() != null) System.out.println(tab + " " + e.getNodeValue()); NodeList childs = e.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) print(childs.item(i), tab + " "); }