List of usage examples for javax.xml.parsers DocumentBuilder newDocument
public abstract Document newDocument();
From source file:Main.java
public static String generateXml() { DocumentBuilder documentBuilder = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//from w w w .j a va2 s. com documentBuilder = docFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); DOMSource docSource = new DOMSource(document); StreamResult result = new StreamResult(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); result.setOutputStream(baos); transformer.transform(docSource, result); } catch (Exception e) { e.printStackTrace(); } return baos.toString(); }
From source file:Main.java
public static void writeXMLObject(String path, String name, String tag, String model, double ry, String version, double x, double y, double z) { try {/*from w w w. ja v a2 s .com*/ id++; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // Root elements Document doc = docBuilder.newDocument(); doc.setXmlStandalone(true); Element rootElement = doc.createElement(tag); doc.appendChild(rootElement); rootElement.setAttributeNode(addAttribut(doc, "action", "void")); rootElement.setAttributeNode(addAttribut(doc, "actionDist", "10.0")); rootElement.setAttributeNode(addAttribut(doc, "collidable", "true")); rootElement.setAttributeNode(addAttribut(doc, "id", id + "")); rootElement.setAttributeNode(addAttribut(doc, "model", model)); rootElement.setAttributeNode(addAttribut(doc, "pCameraFacing", "true")); rootElement.setAttributeNode(addAttribut(doc, "pControlFlow", "false")); rootElement.setAttributeNode(addAttribut(doc, "pEmitH", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEmitInnerRadius", "0.0")); rootElement.setAttributeNode(addAttribut(doc, "pEmitOutterRadius", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEmitType", "0")); rootElement.setAttributeNode(addAttribut(doc, "pEmitW", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndA", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndB", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndMass", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndR", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndSize", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndV", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pFactoryNumber", "500")); rootElement.setAttributeNode(addAttribut(doc, "pInitialVelocity", "0.0030")); rootElement.setAttributeNode(addAttribut(doc, "pMaxAngle", "10.0")); rootElement.setAttributeNode(addAttribut(doc, "pMaxLife", "2000.0")); rootElement.setAttributeNode(addAttribut(doc, "pMinAngle", "0.0")); rootElement.setAttributeNode(addAttribut(doc, "pMinLife", "1000.0")); rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSecVar", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSeconds", "100")); rootElement.setAttributeNode(addAttribut(doc, "pSpeed", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartA", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartB", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartMass", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartR", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartSize", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartV", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "rx", "0.0")); rootElement.setAttributeNode(addAttribut(doc, "ry", ry + "")); rootElement.setAttributeNode(addAttribut(doc, "rz", "0.0")); rootElement.setAttributeNode(addAttribut(doc, "s", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "type", "basic")); rootElement.setAttributeNode(addAttribut(doc, "versionCode", version)); rootElement.setAttributeNode(addAttribut(doc, "x", x + "")); rootElement.setAttributeNode(addAttribut(doc, "y", y + "")); rootElement.setAttributeNode(addAttribut(doc, "z", z + "")); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(path + "\\" + name + id + ".xml"); transformer.transform(source, result); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } }
From source file:com.sonar.maven.it.ItUtils.java
/** * Creates a settings xml with a sonar profile, containing all the given properties * Also adds repox to continue to use QAed artifacts *///from w w w. j a v a2 s . com public static String createSettingsXml(Map<String, String> props) throws Exception { DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element settings = doc.createElement("settings"); Element profiles = doc.createElement("profiles"); Element profile = doc.createElement("profile"); Element id = doc.createElement("id"); id.setTextContent("sonar"); Element properties = doc.createElement("properties"); for (Map.Entry<String, String> e : props.entrySet()) { Element el = doc.createElement(e.getKey()); el.setTextContent(e.getValue()); properties.appendChild(el); } profile.appendChild(id); profile.appendChild(properties); profile.appendChild(createRepositories(doc)); profile.appendChild(createPluginRepositories(doc)); profiles.appendChild(profile); settings.appendChild(profiles); doc.appendChild(settings); Writer writer = new StringWriter(); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
/** * * @return a brand new {@link Document} instance. *//*w w w . java 2s . c o m*/ public static Document newDocument() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { throw new RuntimeException(e); // TODO proper exception handling } return builder.newDocument(); }
From source file:Main.java
static public Document createDefaultMessage(String messagetype) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentbuilder = null; try {/*from w ww. j a va 2s. c om*/ documentbuilder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } Document doc = documentbuilder.newDocument(); Element root = doc.createElement("message"); doc.appendChild(root); long timestamp = System.currentTimeMillis(); root.setAttribute("timestamp", Long.toString(timestamp)); root.setAttribute("type", messagetype); return doc; }
From source file:com.hangum.tadpole.engine.sql.util.export.XMLExporter.java
/** * make content//from w w w . j a v a2 s . c om * * @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/* www . 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 Document newDocument() { final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder; try {/*from w ww .j a v a 2 s . com*/ docBuilder = dbfac.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } final Document document = docBuilder.newDocument(); document.setXmlStandalone(true); return document; }
From source file:Main.java
public static void exportXmlFile(ArrayList<?> listObject, String rootElement, String interfaceName, String pathSaveFile) {/*from w ww.jav a 2s .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
/** * Creates an empty node list. Creates an empty document then selects nodes * using a random UUID to ensure an empty result. * * @return an empty Node list/* w ww. j a va2s .co m*/ */ public static NodeList createEmptyNodeList() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException("Problem creating document", e); } Document document = builder.newDocument(); assert document != null; final NodeList emptyNodesList = document.getElementsByTagName(UUID.randomUUID().toString()); return emptyNodesList; }