List of usage examples for org.w3c.dom Node appendChild
public Node appendChild(Node newChild) throws DOMException;
newChild
to the end of the list of children of this node. From source file:Main.java
/** * Sets the text value of an Element. if current text of the element is * replaced with the new text if text is null any * current text value is deleted.// w ww . j a va2s .com * * @param elem the Element for which the text value should be set * @param text the new text value of the element * @return true if the text could be set or false otherwise */ static public boolean setElementText(Node elem, Object text) { if (elem == null) return false; // Fehler // Find Text Node node = elem.getFirstChild(); while (node != null) { // Find all Text nodes if (node.getNodeType() == Node.TEXT_NODE) break; // gefunden node = node.getNextSibling(); } if (node != null) { // Set or remove text if (text != null) node.setNodeValue(text.toString()); else elem.removeChild(node); } else if (text != null) { // Add Text elem.appendChild(elem.getOwnerDocument().createTextNode(text.toString())); } return true; }
From source file:com.hphoto.server.ApiServlet.java
private static Element addNode(Document doc, Node parent, String name, String text) { Element child = doc.createElement(name); child.appendChild(doc.createTextNode(getLegalXml(text))); parent.appendChild(child); return child; }
From source file:Main.java
public static Element getElement(Document document, String[] path, String value, String translate, String module) {// ww w. j av a2s . c o m Node node = document; NodeList list; for (int i = 0; i < path.length; ++i) { list = node.getChildNodes(); boolean found = false; for (int j = 0; j < list.getLength(); ++j) { Node testNode = list.item(j); if (testNode.getNodeName().equals(path[i])) { found = true; node = testNode; break; } } if (found == false) { Element element = document.createElement(path[i]); node.appendChild(element); node = element; } } if (value != null) { Text text = document.createTextNode(value); list = node.getChildNodes(); boolean found = false; for (int j = 0; j < list.getLength(); ++j) { Node testNode = list.item(j); if (testNode instanceof Text) { node.replaceChild(text, testNode); found = true; break; } } if (!found) node.appendChild(text); } if (node instanceof Element) { Element element = (Element) node; if (translate != null && element.getAttribute("translate").equals("")) { element.setAttribute("translate", translate); } if (module != null && element.getAttribute("module").equals("")) { element.setAttribute("module", module); } } return (Element) node; }
From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java
private static void addProperty(Document document, Node sessionFactoryElement, String propertyName, String propertyValue) {/*from w w w .java 2s .c om*/ 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 Element addNode(Document doc, Node parent, String ns, String name, String text) { Element child = doc.createElementNS((String) NS_MAP.get(ns), ns + ":" + name); child.appendChild(doc.createTextNode(getLegalXml(text))); parent.appendChild(child); return child; }
From source file:de.codesourcery.spring.contextrewrite.XMLRewrite.java
private static Rule wrap(InsertElementRule r) { return new Rule(r.xpath(), r.id()) { @Override//from w w w. j a va 2 s . co m public void apply(Document document, Node matchedNode) throws Exception { final Document newDocument = parseXMLFragment(r.insert()); final Node firstChild = newDocument.getFirstChild(); final Node adoptedNode = document.importNode(firstChild, true); matchedNode.appendChild(adoptedNode); } @Override public String toString() { return "INSERT ELEMENT: " + r.xpath() + " with '" + r.insert(); } }; }
From source file:Main.java
/** Goes through and adds newlines and indent to the current node and all its children * @param current the current node/*from w w w . ja va 2 s .c om*/ * @param indent the current level of indent this is increased recursively*/ private static void addFormatting(Document doc, Node current, String indent) { // go through each of the children adding space as required Node child = current.getFirstChild(); String childIndent = indent + "\t"; while (child != null) { Node nextChild = child.getNextSibling(); if (child.getNodeType() != Node.TEXT_NODE) { // Only if we aren't a text node do we add the space current.insertBefore(doc.createTextNode("\n" + childIndent), child); if (child.hasChildNodes()) { addFormatting(doc, child, childIndent); } if (nextChild == null) { // Because this is the last child, we need to add some space after it current.appendChild(doc.createTextNode("\n" + indent)); } } child = nextChild; } }
From source file:Main.java
/** * Adds a new node to a file.//from www . j ava2 s.c om * * @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:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java
private static void setPropertyValue(Document document, String propertyName, String value) throws XPathExpressionException { XPathExpression compile = pathFactory .compile("hibernate-configuration/session-factory/property[@name='" + propertyName + "']"); //$NON-NLS-1$ //$NON-NLS-2$ Node node = (Node) compile.evaluate(document, XPathConstants.NODE); if (node != null) { node.setTextContent(value);/*from w ww . j a va 2s.c o m*/ } else { XPathExpression parentNodeExpression = pathFactory.compile("hibernate-configuration/session-factory"); //$NON-NLS-1$ Node parentNode = (Node) parentNodeExpression.evaluate(document, XPathConstants.NODE); // Create a new property element for this datasource-specified property (TMDM-4927). Element property = document.createElement("property"); //$NON-NLS-1$ Attr propertyNameAttribute = document.createAttribute("name"); //$NON-NLS-1$ property.setAttributeNode(propertyNameAttribute); propertyNameAttribute.setValue(propertyName); property.setTextContent(value); parentNode.appendChild(property); } }
From source file:Main.java
/** * Add a CDATA element with attributes.// w ww . j a v a 2 s. co m * <p/> * NOTE: Serializing a XML document via TRAX changes "\r\n" to "\r\r\n" in * a CDATA section. Serializing with the Xalan XMLSerializer works fine. * * @param parent parent node * @param name node name * @param data node data * @param attrs attributes */ public static Element addCDATAElement(Node parent, String name, String data, Attr[] attrs) { Element element; CDATASection cdata; if (parent instanceof Document) { element = ((Document) parent).createElement(name); /* * Creates a <code>CDATASection</code> node whose value is the * specified. */ cdata = ((Document) parent).createCDATASection(data); } else { element = parent.getOwnerDocument().createElement(name); cdata = parent.getOwnerDocument().createCDATASection(data); } if (attrs != null && attrs.length > 0) { for (Attr attr : attrs) { element.setAttributeNode(attr); } } element.appendChild(cdata); parent.appendChild(element); return element; }