List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
From source file:Main.java
/** * Extract a named attribute from a node map. * * @param map attribute node map// w ww .jav a2s .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
/** * 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./*from w w w .j av a2s . c o 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: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.j a v a2 s . co m*/ 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
/** * @param n1 first Node to test/* w w w . j a v a 2 s .c o m*/ * @param n2 second Node to test * @return true if a deep compare show the same children and attributes in the same order */ public static boolean equals(Node n1, Node n2) { // compare type if (!n1.getNodeName().equals(n2.getNodeName())) return false; // compare attributes NamedNodeMap nnm1 = n1.getAttributes(); NamedNodeMap nnm2 = n2.getAttributes(); if (nnm1.getLength() != nnm2.getLength()) return false; for (int i = 0; i < nnm1.getLength(); i++) { Node attr1 = nnm1.item(i); if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName()))) return false; } // compare children Node c1 = n1.getFirstChild(); Node c2 = n2.getFirstChild(); for (;;) { while ((c1 != null) && c1.getNodeName().startsWith("#")) c1 = c1.getNextSibling(); while ((c2 != null) && c2.getNodeName().startsWith("#")) c2 = c2.getNextSibling(); if ((c1 == null) && (c2 == null)) break; if ((c1 == null) || (c2 == null)) return false; if (!equals(c1, c2)) return false; c1 = c1.getNextSibling(); c2 = c2.getNextSibling(); } return true; }
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()); }//from w ww.j a v a 2s . 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(); 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
/** * Output a DOM node including children using a log4j logger. * If logger is null it will just output to system out. * It will indent by the number of tabs passed in. * This method recursively calls itself to output * children nodes./*from ww w . j av a 2s. c o m*/ * * @param logger * @param n * @param tabs */ public static void outputNode(Logger logger, Node n, int tabs) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < tabs; i++) { sb.append("\t"); } sb.append("<" + n.getNodeName()); if (n.hasAttributes()) { NamedNodeMap nnMap = n.getAttributes(); for (int i = 0; i < nnMap.getLength(); i++) { Node att = nnMap.item(i); sb.append(" " + att.getNodeName() + "=\"" + att.getNodeValue() + "\""); } } sb.append(">"); sb = printBuffer(logger, sb, true); for (int i = 0; i < tabs + 1; i++) { sb.append("\t"); } sb.append(n.getNodeValue()); sb = printBuffer(logger, sb, true); if (n.hasChildNodes()) { NodeList nodes = n.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { outputNode(nodes.item(i), tabs + 1); } } for (int i = 0; i < tabs; i++) { sb.append("\t"); } sb.append("</" + n.getNodeName() + ">"); sb = printBuffer(logger, sb, true); }
From source file:Main.java
/** * Are elements equal./*from ww w.j a v a 2 s . co m*/ * * @param element1 * the element1 * @param element2 * the element2 * @return true, if successful */ public static boolean areElementsEqual(Element element1, Element element2) { if (!element1.getTagName().equals(element2.getTagName())) { return false; } NamedNodeMap nodeAttrMap = element1.getAttributes(); NamedNodeMap pathAttrMap = element2.getAttributes(); if ((nodeAttrMap == null && pathAttrMap == null) || (pathAttrMap.getLength() == 0 && nodeAttrMap.getLength() == 0)) { return true; } else { if (element1.hasAttribute("name") && element2.hasAttribute("name")) { if (element1.getAttribute("name").equals(element2.getAttribute("name"))) { return true; } } else if (nodeAttrMap != null && pathAttrMap != null && (nodeAttrMap.getLength() == pathAttrMap.getLength())) { for (int k = 0; k < nodeAttrMap.getLength(); k++) { Node nodeAttr = nodeAttrMap.item(k); String nodeAttrName = nodeAttr.getNodeName(); String nodeAttrValue = nodeAttr.getNodeValue(); if (element2.hasAttribute(nodeAttrName) && nodeAttrValue.equals(element2.getAttribute(nodeAttrName))) { return true; } } } } return false; }
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); }/*w w w .ja v a 2 s. c om*/ } 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
/** * 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.jav a 2 s.com*/ * @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
/** * Sorts Attributes of a given Node// ww w. ja v a2s. c o m * * @param attrs the NamedNodeMap containing Node Attributes * @return Array containing sorted Attributes */ protected static Attr[] sortAttributes(NamedNodeMap attrs) { int len = (attrs != null) ? attrs.getLength() : 0; Attr array[] = new Attr[len]; for (int i = 0; i < len; i++) { array[i] = (Attr) attrs.item(i); } for (int i = 0; i < len - 1; i++) { String name = array[i].getNodeName(); int index = i; for (int j = i + 1; j < len; j++) { String curName = array[j].getNodeName(); if (curName.compareTo(name) < 0) { name = curName; index = j; } } if (index != i) { Attr temp = array[i]; array[i] = array[index]; array[index] = temp; } } return (array); }