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
/** * Rename an element in a DOM document. It happens to involves a node * replication./*from www . j ava2 s . com*/ * * @param document * The document containing the element (some way to verify * that?). */ public static Element renameElement(Document document, Element element, String newName, String namespace) { if (namespace == null) { throw new IllegalArgumentException("No namespace provided for element " + element); } Element newElement = document.createElementNS(namespace, newName); NamedNodeMap attributes = element.getAttributes(); for (int i = 0, iEnd = attributes.getLength(); i < iEnd; i++) { Attr attr2 = (Attr) document.importNode(attributes.item(i), true); newElement.getAttributes().setNamedItem(attr2); } while (element.hasChildNodes()) { newElement.appendChild(element.getFirstChild()); } element.getParentNode().replaceChild(newElement, element); return newElement; }
From source file:Main.java
private static void serializeElement(StringBuilder sb, Element element, int tabIndex) { sb.append('\n'); for (int i = 0; i < tabIndex; i++) { sb.append('\t'); }//from w ww . ja v a 2 s. c om sb.append("<"); sb.append(element.getTagName()); if (element.hasAttributes()) { NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node attribute = attributes.item(i); sb.append(" "); sb.append(attribute.getNodeName()); sb.append("="); sb.append("\""); String value = attribute.getNodeValue(); sb.append(value.replace("\"", "\\\"")); sb.append("\""); } } sb.append(">"); NodeList nodeList = element.getChildNodes(); ArrayList<Element> childElements = new ArrayList<Element>(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode instanceof Element) { childElements.add((Element) childNode); } } if (childElements.size() == 0) { sb.append(escapeInvalidCharacters(getTextContent(element))); } else { for (Element childElement : childElements) { serializeElement(sb, childElement, tabIndex + 1); } sb.append('\n'); for (int i = 0; i < tabIndex; i++) { sb.append('\t'); } } sb.append("</"); sb.append(element.getTagName()); sb.append(">"); }
From source file:Main.java
/** * Print SAML Attribute Element and replace its prefix with the input * prefix.// w w w. j a va2 s . com * * @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
protected static void writeXMLwalkTree(Node node, int indent, PrintWriter out) { if (node == null) throw new NullPointerException("Null node passed to writeXMLwalkTree()"); if (node.hasChildNodes()) { if (node instanceof Element) { Element elem = (Element) node; //elem.normalize(); out.print("\n"); for (int j = 0; j < indent; j++) { out.print(" "); }/* www. ja v a 2 s. com*/ out.print("<" + elem.getTagName()); NamedNodeMap attrs = elem.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr a = (Attr) attrs.item(i); out.print(" " + a.getName() + "=\"" + a.getValue() + "\""); } out.print(">"); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { writeXMLwalkTree(nl.item(i), indent + 2, out); } // for(int j=0;j<indent;j++) { // out.print(" "); // } out.println("</" + elem.getTagName() + ">"); } } else { if (node instanceof Element) { Element elem = (Element) node; out.print("\n"); for (int j = 0; j < indent; j++) { out.print(" "); } out.print("<" + elem.getTagName()); NamedNodeMap attrs = elem.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr a = (Attr) attrs.item(i); out.print(" " + a.getName() + "=\"" + a.getValue() + "\""); } out.println("/>"); } else if (node instanceof CDATASection) { CDATASection cdata = (CDATASection) node; // for(int j=0;j<indent;j++) { // out.print(" "); // } out.print("<![CDATA[" + cdata.getData() + "]]>"); } else if (node instanceof Text) { Text text = (Text) node; StringBuilder buf = new StringBuilder(text.getData().length()); for (int i = 0; i < text.getData().length(); i++) { if (text.getData().charAt(i) == '\n' || text.getData().charAt(i) == '\r' || text.getData().charAt(i) == ' ' || text.getData().charAt(i) == '\t') { if (buf.length() > 0 && buf.charAt(buf.length() - 1) != ' ') { buf.append(' '); } } else { buf.append(text.getData().charAt(i)); } } if (buf.length() > 0 && !buf.toString().equals(" ")) { StringBuilder buf2 = new StringBuilder(buf.length() + indent); // for(int j=0;j<indent;j++) { // buf2.append(' '); // } buf2.append(buf.toString()); out.print(buf2); } } } }
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// w w w .j av a 2 s. co m 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
private static Element copyNode(Document destDocument, Element dest, Element src) { NamedNodeMap namedNodeMap = src.getAttributes(); for (int i = 0; i < namedNodeMap.getLength(); i++) { Attr attr = (Attr) namedNodeMap.item(i); dest.setAttribute(attr.getName(), attr.getValue()); }//from ww w.jav a 2s . c om NodeList childNodeList = src.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node child = childNodeList.item(i); if (child.getNodeType() == Node.TEXT_NODE) { Text text = destDocument.createTextNode(child.getTextContent()); dest.appendChild(text); } else if (child.getNodeType() == Node.ELEMENT_NODE) { Element element = destDocument.createElement(((Element) child).getTagName()); element = copyNode(destDocument, element, (Element) child); dest.appendChild(element); } } return dest; }
From source file:Main.java
/** * @param element/*from w ww. ja v a2 s .c om*/ * @param map * @return */ private static boolean fillAttsIntoMap(Element element, Map<String, String> map) { /* * this case is applicable only if there are no child elements */ if (element.getChildNodes().getLength() > 0) { return false; } /* * in case this is not a special case, it will not have attributes, and * hence we are safe */ NamedNodeMap attrs = element.getAttributes(); int n = attrs.getLength(); for (int i = 0; i < n; i++) { Node att = attrs.item(i); map.put(att.getNodeName(), att.getNodeValue()); } return true; }
From source file:org.jasypt.spring31.xml.encryption.EncryptorConfigBeanDefinitionParser.java
private static Class<?> computeConfigClass(final Element element) { boolean isSimpleConfig = false; boolean isStringConfig = false; boolean isEnvironmentConfig = false; boolean isStringEnvironmentConfig = false; final NamedNodeMap attributesMap = element.getAttributes(); final int attributesLen = attributesMap.getLength(); for (int i = 0; i < attributesLen; i++) { final Node attribute = attributesMap.item(i); final String attributeName = attribute.getNodeName(); if (!isSimpleConfig && PARAMS_SIMPLE.contains(attributeName)) { isSimpleConfig = true;//from w ww . ja va2s . co m } if (!isStringConfig && PARAMS_STRING.contains(attributeName)) { isStringConfig = true; } if (!isEnvironmentConfig && PARAMS_ENVIRONMENT.contains(attributeName)) { isEnvironmentConfig = true; } if (!isStringEnvironmentConfig && PARAMS_STRING_ENVIRONMENT.contains(attributeName)) { isStringEnvironmentConfig = true; } } if (isStringEnvironmentConfig || (isEnvironmentConfig && isStringConfig)) { return EnvironmentStringPBEConfig.class; } if (isEnvironmentConfig) { return EnvironmentPBEConfig.class; } if (isStringConfig) { return SimpleStringPBEConfig.class; } return SimplePBEConfig.class; }
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);//from w w w .j a va2 s .c om 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:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java
private static void addEvent(Document document, Node sessionFactoryElement, String eventType, String listenerClass) {//from ww w. j a v a2 s.c om Element event = document.createElement("event"); //$NON-NLS-1$ Attr type = document.createAttribute("type"); //$NON-NLS-1$ type.setValue(eventType); event.getAttributes().setNamedItem(type); { Element listener = document.createElement("listener"); //$NON-NLS-1$ Attr clazz = document.createAttribute("class"); //$NON-NLS-1$ clazz.setValue(listenerClass); listener.getAttributes().setNamedItem(clazz); event.appendChild(listener); } sessionFactoryElement.appendChild(event); }