List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
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 ww w. j av a2 s . c o m } } return val.toString().trim(); }
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()); }/*w w w.j a v a 2s .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(); 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:Main.java
/** * Copy one node to another node.//from w ww . j a v a2 s .c om * @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
/** * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found. * If it is not found, returns a null.// www . j a v a 2 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 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; }/*from w w w . j a va 2 s .com*/ } 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:com.bstek.dorado.config.xml.XmlParseException.java
private static String populateErrorMessage(String message, Node node, Resource resource) { StringBuffer sb = new StringBuffer(); if (message != null) sb.append(message);// w w w .j av a2 s .c o m if (resource != null) { sb.append(" - ").append(resource); } if (node != null) { Element element; if (node instanceof Element) { element = (Element) node; } else { element = (Element) node.getParentNode(); } if (element != null) { sb.append(" - ").append("<" + element.getTagName() + " "); NamedNodeMap names = element.getAttributes(); for (int i = 0; i < 3 && i < names.getLength(); i++) { sb.append(populateXmlAttribute(element, names.item(i).getNodeName())).append(" "); } sb.append("... "); } } return sb.toString(); }
From source file:Main.java
static boolean canBeMerged(Node node1, Node node2, String requiredTagName) { if (node1.getNodeType() != Node.ELEMENT_NODE || node2.getNodeType() != Node.ELEMENT_NODE) return false; Element element1 = (Element) node1; Element element2 = (Element) node2; if (!equals(requiredTagName, element1.getTagName()) || !equals(requiredTagName, element2.getTagName())) return false; NamedNodeMap attributes1 = element1.getAttributes(); NamedNodeMap attributes2 = element2.getAttributes(); if (attributes1.getLength() != attributes2.getLength()) return false; for (int i = 0; i < attributes1.getLength(); i++) { final Attr attr1 = (Attr) attributes1.item(i); final Attr attr2; if (isNotEmpty(attr1.getNamespaceURI())) attr2 = (Attr) attributes2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName()); else/*from w w w. ja va2 s . c o m*/ attr2 = (Attr) attributes2.getNamedItem(attr1.getName()); if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent())) return false; } return true; }
From source file:DocWriter.java
private static String nodeWithAttrs(Node node, String indent) { StringBuffer sb = new StringBuffer(); // indent bug - leave out //sb.append( indent ); sb.append("<"); sb.append(node.getNodeName());//from w w w .jav a2 s . co m NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { sb.append(" "); Node attrNode = attrs.item(i); sb.append(attrNode.getNodeName()); sb.append("=\""); sb.append(attrNode.getNodeValue()); sb.append("\""); } if (!node.hasChildNodes()) { sb.append("/>"); } else { sb.append(">"); } return sb.toString(); }
From source file:Main.java
public static Node findNode(Node root, String elementName, boolean deep, boolean elementsOnly) { //Check to see if root has any children if not return null if (!(root.hasChildNodes())) return null; //Root has children, so continue searching for them Node matchingNode = null;//from w w w . j a v a2 s .c o m String nodeName = null; Node child = null; NodeList childNodes = root.getChildNodes(); int noChildren = childNodes.getLength(); for (int i = 0; i < noChildren; i++) { if (matchingNode == null) { child = childNodes.item(i); nodeName = child.getNodeName(); if ((nodeName != null) & (nodeName.equals(elementName))) return child; if (deep) matchingNode = findNode(child, elementName, deep, elementsOnly); } else break; } if (!elementsOnly) { NamedNodeMap childAttrs = root.getAttributes(); noChildren = childAttrs.getLength(); for (int i = 0; i < noChildren; i++) { if (matchingNode == null) { child = childAttrs.item(i); nodeName = child.getNodeName(); if ((nodeName != null) & (nodeName.equals(elementName))) return child; } else break; } } return matchingNode; }
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 w w .j av a 2 s .c om*/ } String fortifiedXmlnss = StringUtils.join(xmlnss, FORTIFIED_NAMESPACE_DECLARATIONS_SEPARATOR); element.setAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME, fortifiedXmlnss); }