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 void removeNodeAttributes(Node node) { NamedNodeMap attrs = node.getAttributes(); if ((attrs != null) && (attrs.getLength() > 0)) { String[] names = new String[attrs.getLength()]; for (int i = 0; i < names.length; i++) { names[i] = attrs.item(i).getNodeName(); }//from w w w .j a v a 2 s .c o m for (int i = 0; i < names.length; i++) { attrs.removeNamedItem(names[i]); } } }
From source file:Main.java
/** * recursively transform the prefix and the namespace of a node where getNamespaceURI is NULL * @param node the node to transform//from www .j ava 2 s . co m * @param prefix the new prefix * @param namespaceuri the new namespace uri * @return the new node with NS and prefix changed */ static public Node setPrefixNamespace(Node node, String prefix, String namespaceuri) { Node dest = null; if (node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; if (e.getNamespaceURI() == null) { Element e2 = node.getOwnerDocument().createElementNS(namespaceuri, (prefix != null ? prefix + ':' + e.getLocalName() : e.getLocalName())); NamedNodeMap nodes = e.getAttributes(); for (int i = 0; i < nodes.getLength(); ++i) { Attr att = (Attr) (node.getOwnerDocument().importNode(nodes.item(i), true)); e2.setAttributeNode(att); } dest = e2; } else { dest = node.getOwnerDocument().importNode(node, false); } } else { dest = node.getOwnerDocument().importNode(node, false); } for (Node c = node.getFirstChild(); c != null; c = c.getNextSibling()) { dest.appendChild(setPrefixNamespace(c, prefix, namespaceuri)); } return dest; }
From source file:Main.java
public static String format(Node n, int indentLevel) { String indent = ""; for (int i = 0; i < indentLevel; i++) { indent += "\t"; }/*from ww w . j a v a 2 s . c o m*/ StringBuilder result = new StringBuilder(); result.append(indent); if (n.getNodeType() == Node.ELEMENT_NODE) { result.append("<"); result.append(n.getNodeName()); NamedNodeMap attrs = n.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); result.append(" "); result.append(attr.getNodeName()); result.append("=\""); result.append(attr.getNodeValue()); result.append("\""); } result.append(">\n"); } if (n.getNodeType() == Node.TEXT_NODE) { String str = n.getNodeValue(); result.append(str + "\n"); } NodeList childNodes = n.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node n2 = childNodes.item(i); if (isEmptyTextNode(n2)) { continue; } result.append(format(n2, indentLevel + 1)); } if (n.getNodeType() == Node.ELEMENT_NODE) { result.append(indent); result.append("</"); result.append(n.getNodeName()); result.append(">\n"); } return result.toString(); }
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()); nodeMap.put("@" + attrNode.getNodeName(), attrNode.getNodeValue()); }/* www. j av a2 s.c om*/ 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(); return subNodes.item(0).getNodeValue(); 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()); nodeMap.put(subNode.getNodeName(), subNode.getNodeValue()); } 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:Utils.java
public static String elementToString(Node n) { String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; }//from ww w .j ava2 s .co m if (name.startsWith("#")) { return ""; } StringBuffer sb = new StringBuffer(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); ; } else { sb.append("/>").append('\n'); } } else { sb.append('>').append('\n'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append(childToString); hasValidChildren = true; } } if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:Main.java
private static Map<String, Object> getAttributes(Node node) { NamedNodeMap attribs = node.getAttributes(); int attribCount = attribs.getLength(); Map<String, Object> map = new LinkedHashMap<>(attribCount); for (int j = 0; j < attribCount; j++) { Node attrib = attribs.item(j); map.put(attrib.getNodeName(), attrib.getNodeValue()); }/*from w w w.j a v a 2 s. c o m*/ return map; }
From source file:Main.java
public static void spreadNamespaces(Node node, String tns, boolean overwrite) { Document doc = node instanceof Document ? (Document) node : node.getOwnerDocument(); boolean isParent = false; while (node != null) { Node next = null;//from w w w . j a v a2 s . com if (!isParent && node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNamespaceURI() == null) { node = doc.renameNode(node, tns, node.getNodeName()); } else { if (overwrite) { tns = node.getNamespaceURI(); } } NamedNodeMap nodeMap = node.getAttributes(); int nodeMapLengthl = nodeMap.getLength(); for (int i = 0; i < nodeMapLengthl; i++) { Node attr = nodeMap.item(i); if (attr.getNamespaceURI() == null) { doc.renameNode(attr, tns, attr.getNodeName()); } } } isParent = (isParent || (next = node.getFirstChild()) == null) && (next = node.getNextSibling()) == null; node = isParent ? node.getParentNode() : next; if (isParent && node != null) { if (overwrite) { tns = node.getNamespaceURI(); } } } }
From source file:Main.java
public static String elementToString(Node n) { String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; }//from w w w . ja v a 2s . c om if (name.startsWith("#")) { return ""; } StringBuilder sb = new StringBuilder(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { // if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) { if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); } else { sb.append("/>"); } } else { sb.append('>'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append('\n').append(childToString); hasValidChildren = true; } } if (hasValidChildren) { sb.append('\n'); } // if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) { if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:Main.java
/** Transform xml element and children into a string * * @param nd Node root of elements to transform * @return String representation of xml//from ww w . java 2 s . co m */ public static String elementToString(Node nd, boolean add_newlines) { //short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == nd.getNodeType()) { return "<![CDATA[" + nd.getNodeValue() + "]]>"; } // return if simple element type final String name = nd.getNodeName(); if (name.startsWith("#")) { if (name.equals("#text")) return nd.getNodeValue(); return ""; } // output name String ret = "<" + name; // output attributes NamedNodeMap attrs = nd.getAttributes(); if (attrs != null) { for (int idx = 0; idx < attrs.getLength(); idx++) { Node attr = attrs.item(idx); ret += " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""; } } final String text = nd.getTextContent(); final NodeList child_ndls = nd.getChildNodes(); String all_child_str = ""; for (int idx = 0; idx < child_ndls.getLength(); idx++) { final String child_str = elementToString(child_ndls.item(idx), add_newlines); if ((child_str != null) && (child_str.length() > 0)) { all_child_str += child_str; } } if (all_child_str.length() > 0) { // output children ret += ">" + (add_newlines ? "\n" : " "); ret += all_child_str; ret += "</" + name + ">"; } else if ((text != null) && (text.length() > 0)) { // output text ret += text; ret += "</" + name + ">"; } else { // output nothing ret += "/>" + (add_newlines ? "\n" : " "); } return ret; }
From source file:com.evolveum.midpoint.util.UglyHacks.java
public static void fortifyNamespaceDeclarationsSingleElement(Element element) { List<String> xmlnss = new ArrayList<String>(); NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (DOMUtil.isNamespaceDefinition(attr)) { String prefix = DOMUtil.getNamespaceDeclarationPrefix(attr); String namespace = DOMUtil.getNamespaceDeclarationNamespace(attr); xmlnss.add(prefix + "=" + namespace); }//from w ww . j a v a2 s .co m } String fortifiedXmlnss = StringUtils.join(xmlnss, FORTIFIED_NAMESPACE_DECLARATIONS_SEPARATOR); element.setAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME, fortifiedXmlnss); }