List of usage examples for org.w3c.dom Element getAttributes
public NamedNodeMap getAttributes();
NamedNodeMap
containing the attributes of this node (if it is an Element
) or null
otherwise. From source file:Main.java
/** * Whenever you'd need to print a configuration node and/or its children. * * @param root the root node to print./*from w w w . jav a2 s . co m*/ * @param out the print stream that should be used to outpu * @param recurse boolean * @param prefix String */ public static void printChildElements(Element root, PrintStream out, boolean recurse, String prefix) { out.print(prefix + "<" + root.getNodeName()); NamedNodeMap attrs = root.getAttributes(); Node node; for (int i = 0; i < attrs.getLength(); i++) { node = attrs.item(i); out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); } out.println(">"); String data = getText(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t" + data); data = getCData(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t<![CDATA[" + data + "]]>"); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (recurse) printChildElements((Element) node, out, recurse, prefix + "\t"); else out.println(prefix + node.getNodeName()); } } out.println(prefix + "</" + root.getNodeName() + ">"); }
From source file:Main.java
/** * Rename an element, replacing it in its document. * @param element the element to rename. * @param name the new element name./*from ww w .java2 s . c o m*/ * @return the renamed element. */ public static Element renameElement(Element element, String name) { if (element.getNodeName().equals(name)) return element; Element el = element.getOwnerDocument().createElement(name); //Copy the attributes NamedNodeMap attributes = element.getAttributes(); int nAttrs = attributes.getLength(); for (int i = 0; i < nAttrs; i++) { Node attr = attributes.item(i); el.setAttribute(attr.getNodeName(), attr.getNodeValue()); } //Copy the children Node node = element.getFirstChild(); while (node != null) { Node clone = node.cloneNode(true); el.appendChild(clone); node = node.getNextSibling(); } //Replace the element element.getParentNode().replaceChild(el, element); return el; }
From source file:com.evolveum.midpoint.cli.common.ToolsUtils.java
public static void setNamespaceDeclaration(Element element, String prefix, String namespaceUri) { Document doc = element.getOwnerDocument(); NamedNodeMap attributes = element.getAttributes(); Attr attr;//from w w w. ja v a 2 s . c om if (prefix == null || prefix.isEmpty()) { // default namespace attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE); } else { attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix); } checkValidXmlChars(namespaceUri); attr.setValue(namespaceUri); attributes.setNamedItem(attr); }
From source file:Main.java
public static void removeInvalidAttributes(Element element, String... validAttributeNames) { Set<String> validNames = new HashSet<String>(); for (String name : validAttributeNames) { validNames.add(name);/*from w w w .j av a 2s .com*/ } boolean elementModified = false; NamedNodeMap nnm = element.getAttributes(); for (int i = 0; i < nnm.getLength(); i++) { String attributeName = nnm.item(i).getNodeName(); if (!validNames.contains(attributeName)) { element.removeAttribute(attributeName); elementModified = true; } } if (elementModified) { flagDocumentAsCorrected(element); } }
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/*ww w . j a va 2 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:com.evolveum.midpoint.prism.util.PrismUtil.java
private static void fortifyNamespaceDeclarations(Element definitionElement, Element childElement) { Document doc = definitionElement.getOwnerDocument(); NamedNodeMap attributes = childElement.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); Element namespaceElement = doc.createElementNS(PrismConstants.A_NAMESPACE.getNamespaceURI(), PrismConstants.A_NAMESPACE.getLocalPart()); namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_PREFIX, prefix); namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_URL, namespace); definitionElement.insertBefore(namespaceElement, childElement); }/*from w ww .ja v a 2 s . c o m*/ } }
From source file:Main.java
/** * // w ww. j a v a2s . c om * @param currentNode * @param tagName * @param attributeValue * @return */ public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName, String attributeValue) { String result = ""; NodeList childNodeList = currentNode.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); switch (childNode.getNodeType()) { case Node.DOCUMENT_NODE: break; case Node.ELEMENT_NODE: Element childElement = (Element) childNodeList.item(i); // logger.debug("childElement name : " + childElement.getTagName()); if (childElement != null && childElement.getNodeName().equals(tagName)) { NamedNodeMap attributes = childElement.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { Node current = attributes.item(j); if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) { result = childElement.getTextContent(); break; } } } case Node.TEXT_NODE: // logger.debug("textElement name : " + currentNode.getNodeValue()); break; case Node.COMMENT_NODE: break; case Node.PROCESSING_INSTRUCTION_NODE: break; case Node.ENTITY_REFERENCE_NODE: break; case Node.DOCUMENT_TYPE_NODE: break; } } return result; }
From source file:Main.java
/** * Changes the tag name of an element./*from ww 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
/** Helper method - Converts an XML <I>Element</I> to a <I>String</I> object. @param pElement An <I>org.w3c.dom.Element</I> object to be serialized to a <I>String</I>. @return <I>String</I> representation of the <I>Element</I>. *///from w ww.j av a 2 s. c om public static String convertElementToString(Element pElement) { // Local variables. int nCount = 0; // Start the element. String strName = pElement.getNodeName(); String strReturn = "<" + strName; // Get the list of attributes. NamedNodeMap pNodeMap = pElement.getAttributes(); nCount = pNodeMap.getLength(); // Build the attributes. for (int i = 0; i < nCount; i++) { Attr pAttr = (Attr) pNodeMap.item(i); strReturn += " " + pAttr.getNodeName() + "=\"" + pAttr.getNodeValue() + "\""; } // Get the list of children. NodeList pChildren = pElement.getChildNodes(); nCount = pChildren.getLength(); // If no children exist, return a single node. if (0 == nCount) return strReturn + " />"; // Close node. strReturn += ">"; // Build out the children nodes. for (int i = 0; i < nCount; i++) { Node pChild = pChildren.item(i); if (pChild instanceof CharacterData) strReturn += ((CharacterData) pChild).getData(); else if (pChild instanceof Element) strReturn += convertElementToString((Element) pChild); } return strReturn + "</" + strName + ">"; }
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 ww w. ja v a 2 s . c o 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; }