List of usage examples for org.w3c.dom Document createAttribute
public Attr createAttribute(String name) throws DOMException;
Attr
of the given name. From source file:Main.java
/** * Copies all attributes from one element to another in the official way. *//* w ww. ja v a 2 s. com*/ public static void copyAttributes(Element elementFrom, Element elementTo) { NamedNodeMap nodeList = elementFrom.getAttributes(); if (nodeList == null) { // No attributes to copy: just return return; } Attr attrFrom = null; Attr attrTo = null; // Needed as factory to create attrs Document documentTo = elementTo.getOwnerDocument(); int len = nodeList.getLength(); // Copy each attr by making/setting a new one and // adding to the target element. for (int i = 0; i < len; i++) { attrFrom = (Attr) nodeList.item(i); // Create an set value attrTo = documentTo.createAttribute(attrFrom.getName()); attrTo.setValue(attrFrom.getValue()); // Set in target element elementTo.setAttributeNode(attrTo); } }
From source file:Main.java
/** * Clones the given DOM node into the given DOM document. * //from w w w. ja v a 2s. c o m * @param node * The DOM node to clone. * @param doc * The target DOM document. * @return The cloned node in the target DOM document. */ public static Node cloneNode(Node node, Document doc) { Node clone = null; switch (node.getNodeType()) { case Node.ELEMENT_NODE: clone = doc.createElement(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attrNode = attrs.item(i); Attr attrClone = doc.createAttribute(attrNode.getNodeName()); attrClone.setNodeValue(attrNode.getNodeValue()); ((Element) clone).setAttributeNode(attrClone); } // Iterate through each child nodes. NodeList childNodes = node.getChildNodes(); if (childNodes != null) { for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode == null) { continue; } Node childClone = cloneNode(childNode, doc); if (childClone == null) { continue; } clone.appendChild(childClone); } } break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: clone = doc.createTextNode(node.getNodeName()); clone.setNodeValue(node.getNodeValue()); break; } return clone; }
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 .ja v a 2 s .c o m*/ 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); }
From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java
private static void addProperty(Document document, Node sessionFactoryElement, String propertyName, String propertyValue) {/*from ww w . j a v a2 s.c o m*/ Element property = document.createElement("property"); //$NON-NLS-1$ Attr name = document.createAttribute("name"); //$NON-NLS-1$ name.setValue(propertyName); property.getAttributes().setNamedItem(name); property.appendChild(document.createTextNode(propertyValue)); sessionFactoryElement.appendChild(property); }
From source file:com.hphoto.server.ApiServlet.java
private static void addAttribute(Document doc, Element node, String name, String value) { Attr attribute = doc.createAttribute(name); attribute.setValue(getLegalXml(value)); node.getAttributes().setNamedItem(attribute); }
From source file:de.codesourcery.spring.contextrewrite.XMLRewrite.java
private static Rule wrap(InsertAttributeRule r) { return new Rule(r.xpath(), r.id()) { @Override/*from ww w .j av a 2 s. c o m*/ public void apply(Document document, Node matchedNode) throws Exception { final Attr attribute = document.createAttribute(r.name()); attribute.setValue(r.value()); matchedNode.getAttributes().setNamedItem(attribute); } @Override public String toString() { return "INSERT ATTRIBUTE: " + r.xpath() + " with " + r.name() + "=" + r.value(); } }; }
From source file:Main.java
/** * Adds a new node to a file.// www . j a va 2 s. c o m * * @param nodeType {@link String} The type of the element to add. * @param idField {@link String} The name of the field used to identify this * node. * @param nodeID {@link String} The identifier for this node, so its data * can be later retrieved and modified. * @param destFile {@link File} The file where the node must be added. * @param attributes {@link ArrayList} of array of String. The arrays must * be bidimensional (first index must contain attribute name, second one * attribute value). Otherwise, an error will be thrown. However, if * <value>null</value>, it is ignored. */ public static void addNode(String nodeType, String idField, String nodeID, File destFile, ArrayList<String[]> attributes) { if (attributes != null) { for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) { if (it.next().length != 2) { throw new IllegalArgumentException("Invalid attribute combination"); } } } /* * XML DATA CREATION - BEGINNING */ DocumentBuilder docBuilder; Document doc; try { docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = docBuilder.parse(destFile); } catch (SAXException | IOException | ParserConfigurationException ex) { return; } Node index = doc.getFirstChild(), newElement = doc.createElement(nodeType); NamedNodeMap elementAttributes = newElement.getAttributes(); Attr attrID = doc.createAttribute(idField); attrID.setValue(nodeID); elementAttributes.setNamedItem(attrID); if (attributes != null) { for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) { String[] x = it.next(); Attr currAttr = doc.createAttribute(x[0]); currAttr.setValue(x[1]); elementAttributes.setNamedItem(currAttr); } } index.appendChild(newElement); /* * XML DATA CREATION - END */ /* * XML DATA DUMP - BEGINNING */ Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException ex) { return; } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); try { transformer.transform(source, result); } catch (TransformerException ex) { return; } String xmlString = result.getWriter().toString(); try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFile))) { bufferedWriter.write(xmlString); } catch (IOException ex) { } /* * XML DATA DUMP - END */ }
From source file:Main.java
/** * Clone given Node into target Document. If targe is null, same Document will be used. * If deep is specified, all children below will also be cloned. *//*from ww w.jav a2s . c o m*/ public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException { if (target == null || node.getOwnerDocument() == target) // same Document return node.cloneNode(deep); else { //DOM level 2 provides this in Document, so once xalan switches to that, //we can take out all the below and just call target.importNode(node, deep); //For now, we implement based on the javadocs for importNode Node newNode; int nodeType = node.getNodeType(); switch (nodeType) { case Node.ATTRIBUTE_NODE: newNode = target.createAttribute(node.getNodeName()); break; case Node.DOCUMENT_FRAGMENT_NODE: newNode = target.createDocumentFragment(); break; case Node.ELEMENT_NODE: Element newElement = target.createElement(node.getNodeName()); NamedNodeMap nodeAttr = node.getAttributes(); if (nodeAttr != null) for (int i = 0; i < nodeAttr.getLength(); i++) { Attr attr = (Attr) nodeAttr.item(i); if (attr.getSpecified()) { Attr newAttr = (Attr) cloneNode(attr, target, true); newElement.setAttributeNode(newAttr); } } newNode = newElement; break; case Node.ENTITY_REFERENCE_NODE: newNode = target.createEntityReference(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: newNode = target.createTextNode(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: newNode = target.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: newNode = target.createComment(node.getNodeValue()); break; case Node.NOTATION_NODE: case Node.ENTITY_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.DOCUMENT_NODE: default: throw new IllegalArgumentException("Importing of " + node + " not supported yet"); } if (deep) for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) newNode.appendChild(cloneNode(child, target, true)); return newNode; } }
From source file:Main.java
/** * Clone given Node into target Document. If targe is null, same Document will be used. * If deep is specified, all children below will also be cloned. *//*from w w w. j a v a2 s . com*/ public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException { if ((target == null) || (node.getOwnerDocument() == target)) { // same Document return node.cloneNode(deep); } else { //DOM level 2 provides this in Document, so once xalan switches to that, //we can take out all the below and just call target.importNode(node, deep); //For now, we implement based on the javadocs for importNode Node newNode; int nodeType = node.getNodeType(); switch (nodeType) { case Node.ATTRIBUTE_NODE: newNode = target.createAttribute(node.getNodeName()); break; case Node.DOCUMENT_FRAGMENT_NODE: newNode = target.createDocumentFragment(); break; case Node.ELEMENT_NODE: Element newElement = target.createElement(node.getNodeName()); NamedNodeMap nodeAttr = node.getAttributes(); if (nodeAttr != null) { for (int i = 0; i < nodeAttr.getLength(); i++) { Attr attr = (Attr) nodeAttr.item(i); if (attr.getSpecified()) { Attr newAttr = (Attr) cloneNode(attr, target, true); newElement.setAttributeNode(newAttr); } } } newNode = newElement; break; case Node.ENTITY_REFERENCE_NODE: newNode = target.createEntityReference(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: newNode = target.createTextNode(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: newNode = target.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: newNode = target.createComment(node.getNodeValue()); break; case Node.NOTATION_NODE: case Node.ENTITY_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.DOCUMENT_NODE: default: throw new IllegalArgumentException("Importing of " + node + " not supported yet"); } if (deep) { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { newNode.appendChild(cloneNode(child, target, true)); } } return newNode; } }
From source file:com.photon.phresco.framework.commons.QualityUtil.java
private static Attr createAttribute(Document document, String attrName, String attrValue) { Attr attr = document.createAttribute(attrName); attr.setValue(attrValue);//from ww w .j a v a 2 s. c o m return attr; }