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
/** * Print SAML Attribute Element and replace its prefix with the input * prefix./*from w w w . ja va2 s. c o m*/ * * @param node * A DOM tree Node * @param prefix * A String representing the new prefix * @return An xml String representation of the DOM tree. */ public static String printAttributeValue(Element node, String prefix) { if (node == null) { return null; } StringBuffer xml = new StringBuffer(100); xml.append('<'); xml.append(prefix).append(node.getLocalName()); NamedNodeMap attrs = node.getAttributes(); int length = attrs.getLength(); for (int i = 0; i < length; i++) { Attr attr = (Attr) attrs.item(i); xml.append(' '); xml.append(attr.getNodeName()); xml.append("=\""); // xml.append(normalize(attr.getNodeValue())); xml.append(attr.getNodeValue()); xml.append('"'); } xml.append('>'); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { xml.append(print(children.item(i))); } } xml.append("</"); xml.append(prefix).append(node.getLocalName()); xml.append('>'); return xml.toString(); }
From source file:Main.java
/** * Compares the attributes of two XML <code>Node</code> objects. This method * returns <code>true</code> if all attribute name-value pairs match * disregarding their order of placement. * /*from www. ja v a 2 s. c o m*/ * @param n1 first <code>Node</code> * @param n2 second <code>Node</code> * * @return boolean * */ public static boolean diffAttributes(Node n1, Node n2) { NamedNodeMap n1Atts = n1.getAttributes(); NamedNodeMap n2Atts = n2.getAttributes(); if (n1Atts.getLength() != n2Atts.getLength()) { return false; } for (int i = 0; i < n1Atts.getLength(); i++) { Node a1 = n1Atts.item(i); Node a2Val = n2Atts.getNamedItem(a1.getNodeName()); if (a2Val == null || !a1.getNodeValue().equals(a2Val.getNodeValue())) { return false; } } return true; }
From source file:Main.java
protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI) throws Exception { StringBuilder b = new StringBuilder(); if (node == null) { return ""; }//from w w w .ja va2s .co m if (node instanceof Element) { Element element = (Element) node; b.append("<"); b.append(element.getNodeName()); Map<String, String> thisLevelPrefixes = new HashMap(); if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) { thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI()); } if (element.hasAttributes()) { NamedNodeMap map = element.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Node attr = map.item(i); if (attr.getNodeName().startsWith("xmlns")) continue; if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) { thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI()); } b.append(" "); b.append(attr.getNodeName()); b.append("=\""); b.append(attr.getNodeValue()); b.append("\""); } } if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI) && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) { b.append(" xmlns=\"").append(namespaceURI).append("\""); } for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) { b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\""); parentPrefixes.add(entry.getKey()); } NodeList children = element.getChildNodes(); boolean hasOnlyAttributes = true; for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() != Node.ATTRIBUTE_NODE) { hasOnlyAttributes = false; break; } } if (!hasOnlyAttributes) { b.append(">"); for (int i = 0; i < children.getLength(); i++) { b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI())); } b.append("</"); b.append(element.getNodeName()); b.append(">"); } else { b.append("/>"); } for (String thisLevelPrefix : thisLevelPrefixes.keySet()) { parentPrefixes.remove(thisLevelPrefix); } } else if (node.getNodeValue() != null) { b.append(encodeText(node, node.getNodeValue())); } return b.toString(); }
From source file:Main.java
/** * Serializes the DOM-tree to a stringBuffer. Recursive method! * * @param node Node to start examining the tree from. * @param writeString The StringBuffer you want to fill with xml. * @return The StringBuffer containing the xml. * //from w w w .j a va 2 s. c om * @since 2002-12-12 * @author Mattias Bogeblad */ public static StringBuffer serializeDom(Node node, StringBuffer writeString) { int type = node.getNodeType(); try { switch (type) { // print the document element case Node.DOCUMENT_NODE: { writeString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writeString = serializeDom(((Document) node).getDocumentElement(), writeString); break; } // print element with attributes case Node.ELEMENT_NODE: { writeString.append("<"); writeString.append(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); String outString = " " + attr.getNodeName() + "=\"" + replaceSpecialCharacters(attr.getNodeValue()) + "\""; writeString.append(outString); } writeString.append(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) writeString = serializeDom(children.item(i), writeString); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { String outString = "&" + node.getNodeName() + ";"; writeString.append(outString); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { String outString = "<![CDATA[" + node.getNodeValue() + "]]>"; writeString.append(outString); break; } // print text case Node.TEXT_NODE: { writeString.append(replaceSpecialCharacters(node.getNodeValue())); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { String data = node.getNodeValue(); String outString = "<?" + node.getNodeName() + " " + data + "?>"; writeString.append(outString); break; } } if (type == Node.ELEMENT_NODE) { String outString = "</" + node.getNodeName() + ">"; writeString.append(outString); } } catch (Exception e) { } return writeString; }
From source file:Main.java
public static Map<String, String> attrbiuteToMap(NamedNodeMap attributes) { if (attributes == null) return new LinkedHashMap<String, String>(); Map<String, String> result = new LinkedHashMap<String, String>(); for (int i = 0; i < attributes.getLength(); i++) { result.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue()); }//from www .j av a 2s . c o m return result; }
From source file:Main.java
/** * @param n1 first Node to test/*from ww w . java 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 void removeAttributes(Element elem) { if (elem.hasAttributes()) { NamedNodeMap attributeMap = elem.getAttributes(); // since node maps are live, hold attributes in a separate collection int n = attributeMap.getLength(); Attr[] attributes = new Attr[n]; for (int i = 0; i < n; i++) attributes[i] = (Attr) attributeMap.item(i); // now remove each attribute from the element for (int i = 0; i < n; i++) elem.removeAttributeNode(attributes[i]); }/*from w w w . jav a2s. c om*/ }
From source file:Main.java
public static LinkedHashMap<String, String> attrbiuteToMap(NamedNodeMap attributes) { if (attributes == null) return new LinkedHashMap<String, String>(); LinkedHashMap<String, String> result = new LinkedHashMap<String, String>(); for (int i = 0; i < attributes.getLength(); i++) { result.put(attributes.item(i).getNodeName(), attributes.item(i).getNodeValue()); }// w ww . j a v a2s. c o m return result; }
From source file:Main.java
/** * Sorts Attributes of a given Node// www. j a v a 2s. 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); }
From source file:com.enioka.jqm.tools.ResourceParser.java
private static void importXml() throws NamingException { InputStream is = ResourceParser.class.getClassLoader().getResourceAsStream(Helpers.resourceFile); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); try {//from www .j av a2 s . c o m DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(is); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("resource"); String jndiAlias = null, resourceClass = null, description = "no description", scope = null, auth = "Container", factory = null; boolean singleton = false; for (int i = 0; i < nList.getLength(); i++) { Node n = nList.item(i); Map<String, String> otherParams = new HashMap<String, String>(); NamedNodeMap attrs = n.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Node attr = attrs.item(j); String key = attr.getNodeName(); String value = attr.getNodeValue(); if ("name".equals(key)) { jndiAlias = value; } else if ("type".equals(key)) { resourceClass = value; } else if ("description".equals(key)) { description = value; } else if ("factory".equals(key)) { factory = value; } else if ("auth".equals(key)) { auth = value; } else if ("singleton".equals(key)) { singleton = Boolean.parseBoolean(value); } else { otherParams.put(key, value); } } if (resourceClass == null || jndiAlias == null || factory == null) { throw new NamingException("could not load the resource.xml file"); } JndiResourceDescriptor jrd = new JndiResourceDescriptor(resourceClass, description, scope, auth, factory, singleton); for (Map.Entry<String, String> prm : otherParams.entrySet()) { jrd.add(new StringRefAddr(prm.getKey(), prm.getValue())); } xml.put(jndiAlias, jrd); } } catch (Exception e) { NamingException pp = new NamingException("could not initialize the JNDI local resources"); pp.setRootCause(e); throw pp; } finally { IOUtils.closeQuietly(is); } }