List of usage examples for org.w3c.dom Document 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
/** * Create a new document object with input element as the root. * /*from w ww . ja v a 2s .c o m*/ * @param inputElement * Input Element object * @param deep * Include child nodes of this element true/false * @return XML Document object * @throws IllegalArgumentException * if input is invalid * @throws ParserConfigurationException */ public static Document getDocument(final Element inputElement, final boolean deep) throws IllegalArgumentException, ParserConfigurationException { // Validate input element if (inputElement == null) { throw new IllegalArgumentException("Input element cannot be null in " + "XmlUtils.getDocument method"); } // Create a new document final Document outputDocument = getDocument(); // Import data from input element and // set as root element for output document outputDocument.appendChild(outputDocument.importNode(inputElement, deep)); // return output document return outputDocument; }
From source file:Main.java
/** * Converts a Map's keys and values to an XML document where the keys are * the elments, and the values are the textnodes (where value is String) or * subelements (where value is Map). In the case of a map entry with a List * as the value, it will create multiple elements, named by the key, and the * values will be the contents of the list, in list order. Note: use a * sorted list if you care about order./* ww w. j av a 2 s . c o m*/ * <p/> * The following code, will produce the XML below it. For more examples, * you may look at the unit tests for MapTest * <pre> * final Map parameters; * final Map subElements; * final Map innerElements; * parameters = new HashMap(); * subElements = new HashMap(); * innerElements = new HashMap(); * parameters.put("element1", "value1"); * parameters.put("element2", "value2"); * parameters.put("element3", "value3"); * parameters.put("element4", subElements); * subElements.put("subelement1", "value1"); * subElements.put("subelement2", "value2"); * subElements.put("innermost", innerElements); * innerElements.put("innerelement1", "innervalue1"); * innerElements.put("innerelement2", "innervalue2"); * System.out.println(XMLUtil.mapToXML("root", parameters)); * </pre> * <pre> * <?xml version="1.0" encoding="UTF-8"?> * <root> * <element4> * <subelement2>value2</subelement2> * <subelement1>value1</subelement1> * <innermost> * <innerelement1>innervalue1</innerelement1> * <innerelement2>innervalue2</innerelement2> * </innermost> * </element4> * <element2>value2</element2> * <element3>value3</element3> * <element1>value1</element1> * </root> * </pre> * * @param rootElementName the name that you want the root element to have * @param elements the elements, whether a list of elements or a map * of key/value pairs * * @return the string representation of the XML document * * @throws TransformerException if an XSL transformation exception * occurs * @throws ParserConfigurationException if a JAXP configuration error * occurs */ public static String mapToXML(final String rootElementName, final Object elements) throws TransformerException, ParserConfigurationException { final Document mapDoc; final Element parent; mapDoc = createDocument(); parent = mapDoc.createElement(rootElementName); mapDoc.appendChild(parent); mapToNode(elements, parent, mapDoc, null); return documentToString(mapDoc); }
From source file:Main.java
public static String map2Xml(Map<String, String> content, String root_elem_id, String item_elem_id) throws Exception { DocumentBuilder b = documentBuilder(); Document doc = b.newDocument(); String str = null;//ww w.java 2 s . co m SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); Element root = doc.createElement(root_elem_id); doc.appendChild(root); // Now, add all the children for (Entry<String, String> e : content.entrySet()) { Element item = doc.createElement(item_elem_id); item.setAttribute("id", e.getKey()); CDATASection data = doc.createCDATASection(e.getValue()); item.appendChild(data); root.appendChild(item); } try { DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(out); TransformerHandler th = tf.newTransformerHandler(); th.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); // format.put("{http://xml. customer .org/xslt}indent-amount", "4"); // format.put("indent-amount", "4"); // format.put(OutputKeys.DOCTYPE_SYSTEM, "myfile.dtd"); format.put(OutputKeys.ENCODING, "UTF-8"); format.put(OutputKeys.INDENT, "yes"); th.getTransformer().setOutputProperties(format); th.setResult(sr); th.getTransformer().transform(ds, sr); str = out.toString(); } catch (Exception e) { e.printStackTrace(); } return str; }
From source file:com.google.visualization.datasource.render.HtmlRenderer.java
/** * Appends <html>, <head>, <title>, and <body> elements to the document. * * @param document The containing document. * * @return The <body> element. *//* www .j a v a2s . c o m*/ private static Element appendHeadAndBody(Document document) { Element htmlElement = document.createElement("html"); document.appendChild(htmlElement); Element headElement = document.createElement("head"); htmlElement.appendChild(headElement); Element titleElement = document.createElement("title"); titleElement.setTextContent("Google Visualization"); headElement.appendChild(titleElement); Element bodyElement = document.createElement("body"); htmlElement.appendChild(bodyElement); return bodyElement; }
From source file:no.kantega.commons.util.XMLHelper.java
public static Element setChild(Document doc, Document parent, String name) { Element child = doc.getDocumentElement(); if (child == null) { child = doc.createElement(name); parent.appendChild(child); }/* ww w . ja v a 2s . c o m*/ return child; }
From source file:Main.java
/** * Create new XML document.//from w ww . j av a2 s .c o m * * @param rootElementName name of the root element to add, or <code>null</code> if the * document should not have any root just yet * @throws ParserConfigurationException */ public static Document createEmptyDocument(String rootElementName, String namespace) throws ParserConfigurationException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); Document result = builder.newDocument(); if (rootElementName != null) { Element rootElement; if (namespace != null && !namespace.isEmpty()) { rootElement = result.createElementNS(rootElementName, namespace); } else { rootElement = result.createElement(rootElementName); } result.appendChild(rootElement); } return result; }
From source file:com.twentyn.patentExtractor.Util.java
public static Document nodeToDocument(DocumentBuilder docBuilder, String documentContainer, Node n) { /* With help from://from ww w .j av a 2s. c o m * http://examples.javacodegeeks.com/core-java/xml/dom/copy-nodes-subtree-from-one-dom-document-to-another/ */ org.w3c.dom.Document newDoc = docBuilder.newDocument(); Element rootElement = newDoc.createElement(documentContainer); Node newNode = newDoc.importNode(n, true); rootElement.appendChild(newNode); newDoc.appendChild(rootElement); return newDoc; }
From source file:com.viettel.ws.client.JDBCUtil.java
/** * Create Empty Document//from w w w.j a v a2 s.c o m * * @return A empty document * @throws ParserConfigurationException - If error when create document */ public static Document createDocument() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature(FEATURE_GENERAL_ENTITIES, false); factory.setFeature(FEATURE_PARAMETER_ENTITIES, false); factory.setXIncludeAware(false); factory.setExpandEntityReferences(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results); return doc; }
From source file:com.viettel.ws.client.JDBCUtil.java
/** * Create document using DOM api//from ww w. j a va2 s . c o m * * @param rs a result set * @return A document of a result set * @throws ParserConfigurationException - If error when parse string * @throws SQLException - If error when read data from database */ public static Document toDocument(ResultSet rs) throws ParserConfigurationException, SQLException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature(FEATURE_GENERAL_ENTITIES, false); factory.setFeature(FEATURE_PARAMETER_ENTITIES, false); factory.setXIncludeAware(false); factory.setExpandEntityReferences(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results); ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } return doc; }
From source file:Main.java
public static void vectorToXML222(String xmlFile, String xpath, String parentNodeName, Vector<HashMap> vector) { File file = new File(xmlFile); try {/*from ww w. j av a2 s . c om*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document; Element rootNode; if (file.exists()) { document = documentBuilder.parse(new File(xmlFile)); rootNode = document.getDocumentElement(); } else { document = documentBuilder.newDocument(); rootNode = document.createElement(xpath); document.appendChild(rootNode); } for (int x = 0; x < vector.size(); x++) { Element parentNode = document.createElement(parentNodeName); rootNode.appendChild(parentNode); HashMap hashmap = vector.get(x); Set set = hashmap.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); // System.out.println("key=" + // me.getKey().toString()); Element em = document.createElement(me.getKey().toString()); em.appendChild(document.createTextNode(me.getValue().toString())); parentNode.appendChild(em); // System.out.println("write " + // me.getKey().toString() + // "=" // + me.getValue().toString()); } } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream fo = new FileOutputStream(xmlFile); StreamResult result = new StreamResult(fo); transformer.transform(source, result); } catch (Exception ex) { file.delete(); ex.printStackTrace(); } }