List of usage examples for org.w3c.dom NamedNodeMap getLength
public int getLength();
From source file:Main.java
/** * Get all attributes as a Map from a node * * @param node the node taht contains the attributes to read * @return the values or a empty map/*from w ww . j a va 2 s .c o m*/ */ public static HashMap<String, String> getAttributes(Node node) { final HashMap<String, String> result = new HashMap<>(); final NamedNodeMap atts = node.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { final Node att = atts.item(i); result.put(att.getNodeName(), att.getNodeValue()); } return result; }
From source file:Main.java
public static void printElement(Element e) { List lst = getChildrenElement(e); if (lst.size() == 0) { System.out.print(e.getTagName() + "="); String value = getNodeText(e); if (value.trim().length() > 0) System.out.println(value); else/* www. j a v a 2 s .c om*/ System.out.println(); NamedNodeMap nn = e.getAttributes(); for (int i = 0; i < nn.getLength(); i++) { Attr attr = (Attr) e.getAttributes().item(i); System.out.println(attr.getName() + "=" + attr.getValue()); } } else { System.out.println(e.getTagName()); for (int i = 0; i < lst.size(); i++) { printElement((Element) lst.get(i)); } } }
From source file:Main.java
public static HashMap<String, String> convertXmlAttributeToHashMap(NamedNodeMap inputNodeMap) { HashMap<String, String> outputHashMap = new HashMap<String, String>(); for (int i = 0; i < inputNodeMap.getLength(); i++) { Attr inputNodeMapAttribute = (Attr) inputNodeMap.item(i); outputHashMap.put(inputNodeMapAttribute.getName().trim(), inputNodeMapAttribute.getValue().trim()); }//from w ww . java2 s .c o m return outputHashMap; }
From source file:Main.java
/** * @return A String[] of length two, [prefix, URI]. *//*from w w w.j a va 2s. co m*/ public static String[] getNamespaceDeclaration(Element ele, String prefixHint) { String[] ns = new String[2]; // prefix, URI NamedNodeMap attribs = ele.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Attr attr = (Attr) attribs.item(i); if (attr.getName().startsWith("xmlns")) { if ((prefixHint != null && attr.getName().endsWith(prefixHint)) || attr.getName().equals("xmlns")) { ns[0] = attr.getLocalName(); // prefix ns[1] = attr.getTextContent(); // URI // catch default namespace change if (ns[0] == "xmlns") { ns[0] = UUID.randomUUID().toString(); } } } } if (ns[1] == null) { return getNamespaceDeclaration((Element) ele.getParentNode(), prefixHint); } else { return ns; } }
From source file:Main.java
/** * Changes the tag name of an element.// w w w .j a v a 2s .c o m * * @param elem Element which name should be changed * @param newName new tag name of the element * @return true if the name was changed successfully or false otherwise */ static public boolean changeTagName(Element elem, String newName) { if (elem == null) return false; // not Found! Document doc = elem.getOwnerDocument(); Element newElem = doc.createElement(newName); // Copy the attributes to the new element NamedNodeMap attrs = elem.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr2 = (Attr) doc.importNode(attrs.item(i), true); newElem.getAttributes().setNamedItem(attr2); } // Copy all Child Elements for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling()) newElem.appendChild(node.cloneNode(true)); // insert Node parent = elem.getParentNode(); parent.replaceChild(newElem, elem); return true; }
From source file:Main.java
public static String getPrefix(String uri, Node e) { while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) { NamedNodeMap attrs = e.getAttributes(); for (int n = 0; n < attrs.getLength(); n++) { Attr a = (Attr) attrs.item(n); String name;//from w ww. j av a2s.c om if ((name = a.getName()).startsWith("xmlns:") && a.getNodeValue().equals(uri)) { return name.substring(6); } } e = e.getParentNode(); } return null; }
From source file:Main.java
/** * Convert an XML fragment from one namespace to another. * * @param from element to translate * @param namespace namespace to be translated to * @return the element in the new namespace * * @since 8.4/* ww w . j a va 2 s . c o m*/ */ public static Element translateXML(Element from, String namespace) { Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName()); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode; if (node.getNodeType() == Node.ELEMENT_NODE) { newNode = translateXML((Element) node, namespace); } else { newNode = node.cloneNode(true); } to.appendChild(newNode); } NamedNodeMap m = from.getAttributes(); for (int i = 0; i < m.getLength(); i++) { Node attr = m.item(i); to.setAttribute(attr.getNodeName(), attr.getNodeValue()); } return to; }
From source file:Main.java
/** * Gets the ID attribute of a DOM element. * //from w w w.ja va 2s . com * @param domElement the DOM element * * @return the ID attribute or null if there isn't one */ public static Attr getIdAttribute(Element domElement) { if (!domElement.hasAttributes()) { return null; } NamedNodeMap attributes = domElement.getAttributes(); Attr attribute; for (int i = 0; i < attributes.getLength(); i++) { attribute = (Attr) attributes.item(i); if (attribute.isId()) { return attribute; } } return null; }
From source file:Main.java
private static boolean equalElements(final Element a, final Element b) { if (!a.getTagName().equals(b.getTagName())) { return false; }//from ww w . j ava 2 s . c o m final NamedNodeMap attributes = a.getAttributes(); int customAttributeCounter = 0; for (int i = 0, n = attributes.getLength(); i < n; i++) { final Node node = attributes.item(i); if (node != null && !node.getNodeName().startsWith("_")) { if (!node.getNodeName().equals("z") && (b.getAttribute(node.getNodeName()).length() == 0 || !b.getAttribute(node.getNodeName()).equals(node.getNodeValue()))) { return false; } } else { customAttributeCounter++; } } if (a.getAttributes().getLength() - customAttributeCounter != b.getAttributes().getLength()) { return false; } return true; }
From source file:Utils.java
/** * Starting from a node, find the namespace declaration for a prefix. for a matching namespace * declaration.//from w w w. j av a 2 s.c o m * * @param node search up from here to search for namespace definitions * @param searchPrefix the prefix we are searching for * @return the namespace if found. */ public static String getNamespace(Node node, String searchPrefix) { Element el; while (!(node instanceof Element)) { node = node.getParentNode(); } el = (Element) node; NamedNodeMap atts = el.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { Node currentAttribute = atts.item(i); String currentLocalName = currentAttribute.getLocalName(); String currentPrefix = currentAttribute.getPrefix(); if (searchPrefix.equals(currentLocalName) && XMLNAMESPACE.equals(currentPrefix)) { return currentAttribute.getNodeValue(); } else if (isEmpty(searchPrefix) && XMLNAMESPACE.equals(currentLocalName) && isEmpty(currentPrefix)) { return currentAttribute.getNodeValue(); } } Node parent = el.getParentNode(); if (parent instanceof Element) { return getNamespace((Element) parent, searchPrefix); } return null; }