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
public static String domNode2String(Node node, boolean escapeStrings) { String ret = ""; switch (node.getNodeType()) { case Node.DOCUMENT_NODE: // recurse on each child NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { ret += domNode2String(nodes.item(i), escapeStrings); }/*from www.ja v a 2s . c o m*/ } break; case Node.ELEMENT_NODE: String name = node.getNodeName(); ret += "<" + name; NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); ret += " " + current.getNodeName() + "=\"" + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue()) + "\""; } ret += ">"; // recurse on each child NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { ret += domNode2String(children.item(i), escapeStrings); } } ret += "</" + name + ">"; break; case Node.TEXT_NODE: ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue(); break; case Node.COMMENT_NODE: ret += "<!--" + node.getNodeValue() + "-->"; break; } return ret; }
From source file:Main.java
private static Node convertFromNamespaceForm(final Node node) { if (node instanceof Element) { final Document document = node.getOwnerDocument(); final Element newElement = document.createElementNS(null, node.getLocalName()); final NodeList children = node.getChildNodes(); for (int i = 0, n = children.getLength(); i < n; i++) { final Node oldChildNode = children.item(i); final Node newChildNode = convertFromNamespaceForm(oldChildNode); newElement.appendChild(newChildNode); }/*w w w.j a v a 2 s . c o m*/ final NamedNodeMap attributes = node.getAttributes(); for (int i = 0, n = attributes.getLength(); i < n; i++) { final Attr attr = (Attr) attributes.item(i); final String attrQualifiedName = attr.getNodeName(); final String attrLocalName = attr.getLocalName(); if (!attrQualifiedName.equals(XMLNS) && !attrQualifiedName.startsWith(XMLNS_COLON) && !attrLocalName.equals(XSI_SCHEMA_LOCATION_ATTR)) { newElement.setAttributeNS(null, attrLocalName, attr.getValue()); } } return newElement; } else { return node.cloneNode(true); } }
From source file:Main.java
static public List<Node> getAttributeNodeList(Element element, Pattern name) { NamedNodeMap nodes = element.getAttributes(); List<Node> attributes = new ArrayList<>(); int i, size = nodes.getLength(); Node node;//from www. j av a 2 s. c o m for (i = 0; i < size; i++) { node = nodes.item(i); if (name.matcher(node.getNodeName()).find()) attributes.add(node); } return attributes; }
From source file:Main.java
/** * Extract a named attribute from a node map. * * @param map attribute node map/* w w w .ja v a 2s . c o m*/ * @param attrName name of the attribute to extract from the list * @return found node or null if not found */ public static Node getAttributeFromList(final NamedNodeMap map, final String attrName) { Node node = null; if (map != null) { for (int i = 0; i < map.getLength(); i++) { if (attrName.equalsIgnoreCase(map.item(i).getNodeName())) { node = map.item(i); } } } return node; }
From source file:Main.java
public static String retrieveNodeAsString(Node node) { String nodeStr = new String("<"); nodeStr += node.getNodeName() + internal; if (node.hasAttributes()) { NamedNodeMap attrs = node.getAttributes(); // add the attrubite name-value pairs for (int i = 0; i < attrs.getLength(); i++) { Node a = attrs.item(i); nodeStr += a.getNodeName() + "=" + a.getNodeValue() + internal; }/*w w w .j a va 2s .co m*/ } if (node.hasChildNodes()) { nodeStr += ">\n"; NodeList ns = node.getChildNodes(); for (int i = 0; i < ns.getLength(); i++) { nodeStr += logXMLSubNode(ns.item(i), 1); } nodeStr += "<" + node.getNodeName() + "/>\n"; } else { nodeStr += "/>\n"; } return nodeStr; }
From source file:Main.java
public static Object transformXmlNodesIntoMap(Node node) { Map<String, Object> nodeMap = new HashMap<String, Object>(); NodeList subNodes = node.getChildNodes(); NamedNodeMap nodeAttrs = node.getAttributes(); for (int nodeAttrIdx = 0; nodeAttrIdx < nodeAttrs.getLength(); nodeAttrIdx++) { Node attrNode = nodeAttrs.item(nodeAttrIdx); nodeMap.put("@" + attrNode.getNodeName(), attrNode.getTextContent()); }//w ww .ja v a2s .c o m if (nodeAttrs.getLength() == 0) if (subNodes.getLength() == 0) return ""; else if (subNodes.getLength() == 1 && subNodes.item(0).getNodeType() == Node.TEXT_NODE) return subNodes.item(0).getTextContent(); for (int subNodeIdx = 0; subNodeIdx < subNodes.getLength(); subNodeIdx++) { Node subNode = subNodes.item(subNodeIdx); if (subNode.getNodeType() == Node.TEXT_NODE) { nodeMap.put(subNode.getNodeName(), subNode.getTextContent()); } else { if (nodeMap.containsKey(subNode.getNodeName())) { Object subObject = nodeMap.get(subNode.getNodeName()); if (subObject instanceof List<?>) { ((List<Object>) subObject).add(transformXmlNodesIntoMap(subNode)); } else { List<Object> subObjectList = new ArrayList<Object>(); subObjectList.add(subObject); subObjectList.add(transformXmlNodesIntoMap(subNode)); nodeMap.put(subNode.getNodeName(), subObjectList); } } else { nodeMap.put(subNode.getNodeName(), transformXmlNodesIntoMap(subNode)); } } } return nodeMap; }
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 w w w. ja v a2 s . c o m * @param nnm NamedNodeMap * @param name String * @return String */ public static String getAttributeValueByName(final NamedNodeMap nnm, final 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
public static String getNamespaceURI(Element el, String prefix) { if ((prefix == null) || (prefix.length() < 1)) { return ""; }/* w w w . j a va 2s.c o m*/ prefix = prefix.trim(); try { NamedNodeMap map = el.getOwnerDocument().getDocumentElement().getAttributes(); for (int j = 0; j < map.getLength(); j++) { Node n = map.item(j); String attrName = ((Attr) n).getName(); if (attrName != null) { if (attrName.trim().equals("xmlns:" + prefix)) { return ((Attr) n).getValue(); } } } } catch (Exception e) { } return ""; }
From source file:Main.java
/** * Rename an element in a DOM document. It happens to involves a node * replication.// ww w . j a v a 2 s . c o m * * @param document * The document containing the element (some way to verify * that?). */ public static Element renameElement(Document document, Element element, String newName, String namespace) { if (namespace == null) { throw new IllegalArgumentException("No namespace provided for element " + element); } Element newElement = document.createElementNS(namespace, newName); NamedNodeMap attributes = element.getAttributes(); for (int i = 0, iEnd = attributes.getLength(); i < iEnd; i++) { Attr attr2 = (Attr) document.importNode(attributes.item(i), true); newElement.getAttributes().setNamedItem(attr2); } while (element.hasChildNodes()) { newElement.appendChild(element.getFirstChild()); } element.getParentNode().replaceChild(newElement, element); return newElement; }
From source file:Main.java
private static String logXMLSubNode(Node node, int deepth) { int i;/* w w w. j a va 2 s . co m*/ String nodeStr = new String(); String interStr = new String(); for (i = 0; i < deepth; i++) interStr += "\t"; nodeStr += interStr + "<" + node.getNodeName() + internal; if (node.hasAttributes()) { NamedNodeMap attrs = node.getAttributes(); // add the attrubite name-value pairs for (i = 0; i < attrs.getLength(); i++) { Node a = attrs.item(i); nodeStr += a.getNodeName() + "=" + a.getNodeValue() + internal; } } if (node.hasChildNodes()) { nodeStr += ">\n"; NodeList ns = node.getChildNodes(); for (i = 0; i < ns.getLength(); i++) { nodeStr += logXMLSubNode(ns.item(i), deepth + 1); } nodeStr += interStr + "</" + node.getNodeName() + ">\n"; } else { if (node.getNodeValue() != null) { nodeStr += ">" + node.getNodeValue() + "<" + node.getNodeName(); } nodeStr += "/>\n"; } return nodeStr; }