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
/** * Add the first node with a namespace attribute as below: * <asset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> * @nodeName: name of the element// w w w . j a v a 2s .c om * @ * */ public static Document addNodeWithNSAttribute(Document document, String nodeName, String attrName, String namespace) { Element node = document.createElement(nodeName); Attr attr = document.createAttribute(attrName); attr.setValue(namespace); node.setAttributeNodeNS(attr); document.appendChild(node); return document; }
From source file:Main.java
/** * Convert Properties to string/*ww w.ja va 2 s . co m*/ * * @param props * @return xml string * @throws IOException */ public static String writePropToString(Properties props) throws IOException { try { org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); org.w3c.dom.Element conf = doc.createElement("configuration"); doc.appendChild(conf); conf.appendChild(doc.createTextNode("\n")); for (Enumeration e = props.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); Object object = props.get(name); String value; if (object instanceof String) { value = (String) object; } else { continue; } org.w3c.dom.Element propNode = doc.createElement("property"); conf.appendChild(propNode); org.w3c.dom.Element nameNode = doc.createElement("name"); nameNode.appendChild(doc.createTextNode(name.trim())); propNode.appendChild(nameNode); org.w3c.dom.Element valueNode = doc.createElement("value"); valueNode.appendChild(doc.createTextNode(value.trim())); propNode.appendChild(valueNode); conf.appendChild(doc.createTextNode("\n")); } Source source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (Exception e) { throw new IOException(e); } }
From source file:net.bpelunit.framework.control.util.BPELUnitUtil.java
/** * Creates a new document with a dummy root node, intended to store literal data for a receive * or send./* w ww .jav a 2 s . c o m*/ * * CAUTION: This method depends on initialization which is done by calling * {@link initializeParsing}. Not initializing this class will cause uncaught NPEs. * * @return */ public static Element generateDummyElementNode() { Document document = fgDocumentBuilder.newDocument(); Element root = document.createElement(DUMMY_ELEMENT_NAME); document.appendChild(root); return root; }
From source file:Main.java
/** * Return the root element for specified document<br> * Create if it does not already exist with the specified name *///from w w w. j a va2 s. c om private static Element getRootElement(Document doc, boolean create, String name) { if (doc != null) { Element result = doc.getDocumentElement(); if ((result == null) && create) { result = doc.createElement(name); doc.appendChild(result); } return result; } return null; }
From source file:com.hangum.tadpole.engine.sql.util.export.XMLExporter.java
/** * make content/*from w ww . j ava 2s .com*/ * * @param tableName * @param rsDAO * @return */ public static String makeContent(String tableName, QueryExecuteResultDTO rsDAO, int intLimitCnt) throws Exception { final StringWriter stWriter = new StringWriter(); final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.newDocument(); final Element results = doc.createElement(tableName); doc.appendChild(results); Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName(); for (int i = 0; i < dataList.size(); i++) { Map<Integer, Object> mapColumns = dataList.get(i); Element row = doc.createElement("Row"); results.appendChild(row); for (int j = 1; j < mapColumns.size(); j++) { String columnName = mapLabelName.get(j); String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(strValue)); row.appendChild(node); } if (i == intLimitCnt) break; } DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 4); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//"ISO-8859-1"); StreamResult sr = new StreamResult(stWriter); transformer.transform(domSource, sr); return stWriter.toString(); }
From source file:com.hangum.tadpole.engine.sql.util.export.XMLExporter.java
/** * make content file//from w w w . j a v a 2 s . c o m * * @param tableName * @param rsDAO * @return * @throws Exception */ public static String makeContentFile(String tableName, QueryExecuteResultDTO rsDAO) throws Exception { String strTmpDir = PublicTadpoleDefine.TEMP_DIR + tableName + System.currentTimeMillis() + PublicTadpoleDefine.DIR_SEPARATOR; String strFile = tableName + ".xml"; String strFullPath = strTmpDir + strFile; final StringWriter stWriter = new StringWriter(); final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.newDocument(); final Element results = doc.createElement(tableName); doc.appendChild(results); Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName(); for (int i = 0; i < dataList.size(); i++) { Map<Integer, Object> mapColumns = dataList.get(i); Element row = doc.createElement("Row"); results.appendChild(row); for (int j = 1; j < mapColumns.size(); j++) { String columnName = mapLabelName.get(j); String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(strValue)); row.appendChild(node); } } DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 4); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//"ISO-8859-1"); StreamResult sr = new StreamResult(stWriter); transformer.transform(domSource, sr); FileUtils.writeStringToFile(new File(strFullPath), stWriter.toString(), true); return strFullPath; }
From source file:Main.java
public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName, String pathSaveFile) {//from ww w . j a v a 2 s. co m try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); // creating a new instance of a DOM to build a DOM tree. Document doc = docBuilder.newDocument(); Element root = doc.createElement(rootElement); // adding a node after the last child node of the specified node. doc.appendChild(root); Element interfaceElement = null; Element child = null; Text text; for (Object obj : listObject) { Class srcClass = obj.getClass(); Field[] field = srcClass.getFields(); interfaceElement = doc.createElement(interfaceName); for (int i = 0; i < field.length; i++) { // System.out.println(field[i].getName() + ":" + // field[i].get(obj)); child = doc.createElement(field[i].getName()); text = doc.createTextNode((field[i].get(obj)).toString()); child.appendChild(text); interfaceElement.appendChild(child); } root.appendChild(interfaceElement); } // TransformerFactory instance is used to create Transformer // objects. TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = sw.toString(); File file = new File(pathSaveFile); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); bw.write(xmlString); bw.flush(); bw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
/** * //from w w w .j a v a2s. c o m */ public static Document createVmlDocument() { Document document = createDocument(); Element root = document.createElement("html"); root.setAttribute("xmlns:v", "urn:schemas-microsoft-com:vml"); root.setAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office"); document.appendChild(root); Element head = document.createElement("head"); Element style = document.createElement("style"); style.setAttribute("type", "text/css"); style.appendChild(document.createTextNode("<!-- v\\:* {behavior: url(#default#VML);} -->")); head.appendChild(style); root.appendChild(head); Element body = document.createElement("body"); root.appendChild(body); return document; }
From source file:Main.java
public static void append(Document source, String path, Document target) throws XPathExpressionException, IOException { Element e = target.getDocumentElement(); if (e == null) { e = target.createElement("result"); target.appendChild(e); }//from ww w .ja va2 s. c om append(source.getDocumentElement(), path, e); }
From source file:Main.java
public static Document CreateCustomerFile(JFrame mainFrame) { Document CustomerDoc = null; try {/*from w w w . j ava 2 s. c o m*/ DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance(); DocumentBuilder Build = Factory.newDocumentBuilder(); CustomerDoc = Build.newDocument(); Element rootElem = CustomerDoc.createElement("Customers"); CustomerDoc.appendChild(rootElem); } catch (ParserConfigurationException ex) { System.out.println(ex.getMessage()); JOptionPane.showMessageDialog(mainFrame, "There Was an Error Saving The File. Please Restart the Application."); System.exit(1); } return CustomerDoc; }