List of usage examples for org.jdom2 Document Document
public Document()
From source file:com.tactfactory.harmony.utils.XMLUtils.java
License:Open Source License
/** * Open an XML file./* www . j a v a2 s .c o m*/ * @param fileName The name of the file. * @return The openened Document object. Or null if nothing found. */ public static Document openXML(final String fileName) { Document doc = null; try { // Make engine final SAXBuilder builder = new SAXBuilder(); final File xmlFile = new File(fileName); if (!xmlFile.exists()) { doc = new Document(); } else { // Load XML File doc = builder.build(xmlFile); } } catch (JDOMException e) { ConsoleUtils.displayError(e); } catch (IOException e) { ConsoleUtils.displayError(e); } return doc; }
From source file:com.webfront.model.Config.java
public void setConfig() { FileWriter writer = null;/*w w w.j av a2 s . c o m*/ File defaultStartupFile = new File(home + fileSep + defaultConfigFileName); try (FileWriter defaultWriter = new FileWriter(defaultStartupFile)) { defaultWriter.write(installDir); } catch (IOException ex) { Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex); } try { xmlDoc = new Document(); Element rootElement = new Element(root); Element systemNode = new Element("system"); systemNode.addContent(new Element("installDir").addContent(getInstallDir())); // systemNode.addContent(new Element("tmpDir").addContent(tmpDir)); String tmp = tmpDir; tmp.replaceAll("/", "\\/"); systemNode.addContent(new Element("tmpDir").addContent(tmp)); rootElement.addContent(systemNode); xmlDoc.setRootElement(rootElement); writer = new FileWriter(getInstallDir() + getFileSep() + configFileName); XMLOutputter xml = new XMLOutputter(); xml.setFormat(Format.getPrettyFormat()); writer.write(xml.outputString(xmlDoc)); } catch (IOException ex) { Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (writer != null) { writer.close(); } } catch (IOException ex) { Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:controller.MobilePartnerController.java
public void createXML() throws Exception { Element root = new Element("dbconf"); Document doc = new Document(); Element child1 = new Element("url"); child1.addContent("jdbc:mysql://localhost:3306/mobile_partner"); child1.setAttribute("name", "javax.persistence.jdbc.url"); Element child2 = new Element("user"); child2.addContent("root"); child2.setAttribute("name", "javax.persistence.jdbc.user"); Element child3 = new Element("driver"); child3.addContent("com.mysql.jdbc.Driver"); child3.setAttribute("name", "javax.persistence.jdbc.driver"); Element child4 = new Element("password"); child4.addContent("nbuser"); child4.setAttribute("name", "javax.persistence.jdbc.password"); root.addContent(child1);// w w w . j a va 2s . c o m root.addContent(child2); root.addContent(child3); root.addContent(child4); doc.setRootElement(root); XMLOutputter outter = new XMLOutputter(); outter.setFormat(Format.getPrettyFormat()); outter.output(doc, new FileWriter(new File(DIR + "\\dbconf.xml"))); }
From source file:core.ListComponenXml.java
@Override public void updateFile() { try {/*from w ww . ja v a 2 s . com*/ Element root = new Element("listado"); Document document = new Document(); for (Xml xml : this.getXmls()) { Element autor = new Element("autor"); autor.addContent(new Element("nombre").setText(xml.getAutor().getNombre())); autor.addContent(new Element("descripcion").setText(xml.getAutor().getDescripcion())); autor.addContent(new Element("version").setText(xml.getAutor().getVersion())); Element cuerpo = new Element("cuerpo"); Element tipo = new Element("tipo"); Element status = new Element("status"); tipo.setAttribute("columnas", "" + xml.getCuerpo().getColumnas()); tipo.setAttribute("tipodatocolumna", String.join(",", xml.getCuerpo().getTipo_datos())); cuerpo.addContent(tipo); tipo.addContent(new Element("claseprincipal").setText(xml.getCuerpo().getMain())); status.setAttribute("active", String.valueOf(xml.getStatus().getActive())); Element parametro = new Element("parametro"); for (String dato : xml.getCuerpo().getParametros()) { parametro.addContent(new Element(dato)); } cuerpo.addContent(parametro); Element pluguin = new Element("pluguin"); pluguin.addContent(autor); pluguin.addContent(cuerpo); pluguin.addContent(status); root.addContent(pluguin); } ; document.setRootElement(root); XMLOutputter outter = new XMLOutputter(); outter.setFormat(Format.getPrettyFormat()); String basePath = new File("").getAbsolutePath(); String ruta = basePath + "/src/configuracion/xml_configuracion.xml"; if (System.getProperty("os.name").toLowerCase().contains("windows")) { ruta = basePath + "\\src\\configuracion\\xml_configuracion.xml"; } outter.output(document, new FileWriter(new File(ruta))); } catch (IOException ex) { Logger.getLogger(ListComponenXml.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:core.utileria.java
/** * @param args the command line arguments *//*from www . j av a 2 s .c o m*/ public static void main(String[] args) throws IOException { // TODO code application logic here // File archivo = new File("/home/dark/Escritorio/aaaaaaaaaaa/a3"); // if (!archivo.exists()) // archivo.mkdir(); ListComponenXml l = new ListComponenXml(); String basePath = new File("").getAbsolutePath(); System.out.println(basePath); System.out.println(System.getProperty("os.name").toLowerCase()); String ruta = basePath + "/src/configuracion/xml_configuracion.xml"; if (System.getProperty("os.name").toLowerCase().contains("windows")) { ruta = basePath + "\\src\\configuracion\\xml_configuracion.xml"; } l.loadingFile(ruta); l.readNodeFile(); // for(Xml x : l.getXmls()){ // System.out.println(x.toString()); // } Element root = new Element("CONFIGURATION"); Document doc = new Document(); Element child1 = new Element("BROWSER"); child1.addContent("chrome"); Element child2 = new Element("BASE"); child1.addContent("http:fut"); Element child3 = new Element("EMPLOYEE"); child3.addContent(new Element("EMP_NAME").addContent("Anhorn, Irene")); root.addContent(child1); doc.setRootElement(root); XMLOutputter outter = new XMLOutputter(); outter.setFormat(Format.getPrettyFormat()); outter.output(doc, new FileWriter(new File(basePath + "\\src\\configuracion\\xml_configuracion2.xml"))); }
From source file:core.WriteComponenXml.java
@Override public void writeFile(String ruta, Xml xml) { FileWriter writer = null;//from www . ja va 2 s .co m try { ValidXml vxml = new ValidXml(); System.out.println(vxml.exisFile(ruta)); System.out.println(vxml.validExtencion(ruta)); if (vxml.exisFile(ruta) && vxml.validExtencion(ruta)) { loadingFile(ruta); try { FileInputStream fis = new FileInputStream(this.getFile()); setDocument(this.getBuilder().build(fis)); this.setRootNode(getDocument().detachRootElement()); } catch (FileNotFoundException ex) { Logger.getLogger(WriteComponenXml.class.getName()).log(Level.SEVERE, null, ex); } catch (JDOMException ex) { Logger.getLogger(WriteComponenXml.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(WriteComponenXml.class.getName()).log(Level.SEVERE, null, ex); } } else { String basePath = new File("").getAbsolutePath(); ruta = basePath + "/src/configuracion/xml_configuracion.xml"; if (System.getProperty("os.name").toLowerCase().contains("windows")) { ruta = basePath + "\\src\\configuracion\\xml_configuracion.xml"; } Element root = new Element("listado"); Document doc = new Document(); doc.setRootElement(root); XMLOutputter outter = new XMLOutputter(); outter.setFormat(Format.getPrettyFormat()); this.setFile(new File(ruta)); outter.output(doc, new FileWriter(this.getFile())); FileInputStream fis = new FileInputStream(this.getFile()); setDocument(this.getBuilder().build(this.getFile())); this.setRootNode(getDocument().detachRootElement()); this.setDocument(new Document()); this.setRootNode(new Element("listado")); } Element autor = new Element("autor"); System.out.println(" " + this.getDocument() + " " + this.getRootNode()); autor.addContent(new Element("nombre").setText(xml.getAutor().getNombre())); autor.addContent(new Element("descripcion").setText(xml.getAutor().getDescripcion())); autor.addContent(new Element("version").setText(xml.getAutor().getVersion())); Element cuerpo = new Element("cuerpo"); Element tipo = new Element("tipo"); Element status = new Element("status"); tipo.setAttribute("columnas", "" + xml.getCuerpo().getColumnas()); tipo.setAttribute("tipodatocolumna", String.join(",", xml.getCuerpo().getTipo_datos())); cuerpo.addContent(tipo); tipo.addContent(new Element("claseprincipal").setText(xml.getCuerpo().getMain())); status.setAttribute("active", String.valueOf(xml.getStatus().getActive())); Element parametro = new Element("parametro"); for (String dato : xml.getCuerpo().getParametros()) { parametro.addContent(new Element(dato)); } cuerpo.addContent(parametro); Element pluguin = new Element("pluguin"); pluguin.addContent(autor); pluguin.addContent(cuerpo); pluguin.addContent(status); this.getRootNode().addContent(pluguin); this.getDocument().setContent(this.getRootNode()); writer = new FileWriter(ruta); XMLOutputter outputter = new XMLOutputter(); outputter.setFormat(Format.getPrettyFormat()); outputter.output(this.getDocument(), writer); outputter.output(this.getDocument(), System.out); writer.close(); // close writer } catch (IOException ex) { Logger.getLogger(WriteComponenXml.class.getName()).log(Level.SEVERE, null, ex); } catch (JDOMException ex) { Logger.getLogger(WriteComponenXml.class.getName()).log(Level.SEVERE, null, ex); } finally { } }
From source file:de.bund.bfr.knime.pmm.common.PmmXmlDoc.java
License:Open Source License
public Document toXmlDocument() { Document doc = new Document(); Element rootElement = new Element(ELEMENT_PMMDOC); doc.setRootElement(rootElement);/* w w w. j av a 2s . c o m*/ for (PmmXmlElementConvertable element : elementSet) { rootElement.addContent(element.toXmlElement()); } return doc; }
From source file:de.bund.bfr.knime.pmm.extendedtable.Model1Metadata.java
License:Open Source License
public Document toXmlDocument() { Document doc = new Document(); Element rootElement = new Element(ELEMENT_PMMDOC); doc.setRootElement(rootElement);//from w ww . j a va 2s . c o m if (agentXml != null) { rootElement.addContent(agentXml.toXmlElement()); } if (matrixXml != null) { rootElement.addContent(matrixXml.toXmlElement()); } for (MLiteratureItem literatureItem : modelLiteratureItems) { rootElement.addContent(literatureItem.toXmlElement()); } for (EMLiteratureItem literatureItem : estimatedModelLiteratureItems) { rootElement.addContent(literatureItem.toXmlElement()); } return doc; }
From source file:de.bund.bfr.knime.pmm.extendedtable.TimeSeriesMetadata.java
License:Open Source License
public Document toXmlDocument() { Document doc = new Document(); Element rootElement = new Element(ELEMENT_PMMDOC); doc.setRootElement(rootElement);/*from w w w . j a va 2s . com*/ if (agentXml != null) { rootElement.addContent(agentXml.toXmlElement()); } if (matrixXml != null) { rootElement.addContent(matrixXml.toXmlElement()); } for (MDLiteratureItem literatureItem : literatureItems) { rootElement.addContent(literatureItem.toXmlElement()); } return doc; }
From source file:de.danielluedecke.zettelkasten.DesktopFrame.java
License:Open Source License
@Action public void importArchivedDesktop() { // create document Document archive = new Document(); // retrieve last used importdirectory File importdir = settingsObj.getFilePath(); // let user choose filepath File filepath = FileOperationsUtil.chooseFile(this, (settingsObj.isMacAqua()) ? FileDialog.LOAD : JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, (null == importdir) ? null : importdir.getPath(), (null == importdir) ? null : importdir.getName(), resourceMap.getString("openArchiveTitle"), new String[] { ".zip" }, "Zip", settingsObj); // if we have a valid file, go on if (filepath != null && filepath.exists()) { try {/*from w w w .ja v a 2 s.com*/ // open the zip-file ZipInputStream zip = new ZipInputStream(new FileInputStream(filepath)); ZipEntry entry; // now iterate the zip-file, searching for the requested file in it while ((entry = zip.getNextEntry()) != null) { // get filename of zip-entry String entryname = entry.getName(); // if the found file matches the requested one, start the SAXBuilder if (entryname.equals(Constants.archivedDesktopFileName)) { try { SAXBuilder builder = new SAXBuilder(); archive = builder.build(zip); break; } catch (JDOMException e) { Constants.zknlogger.log(Level.SEVERE, e.getLocalizedMessage()); } } } zip.close(); // tell about success Constants.zknlogger.log(Level.INFO, "Desktop archive successfully opened."); } catch (IOException e) { Constants.zknlogger.log(Level.SEVERE, e.getLocalizedMessage()); // show error message dialog JOptionPane.showMessageDialog(this, resourceMap.getString("openArchiveDlgErr"), resourceMap.getString("openArchiveDlgTitle"), JOptionPane.PLAIN_MESSAGE); // and show error log zknframe.showErrorIcon(); return; } // init variables that indicate the success of the import-progress boolean finished = false; // we have a loop here, because the desktop-name of the imported archiv might already exist. // in this case, the user can retry with new names, until a new name was entered, or the // user cancels the action while (!finished) { // import archive int result = desktopObj.importArchivedDesktop(archive); // here we go on in case the desktop-name of the imported archive // already exists. in this case, the user shoould rename the archive if (DesktopData.IMPORT_ARCHIVE_ERR_DESKTOPNAME_EXISTS == result) { // desktop-name already existed, so desktop was not added... JOptionPane.showMessageDialog(this, resourceMap.getString("errDesktopNameExistsMsg", archive.getRootElement().getAttributeValue("name")), resourceMap.getString("errDesktopNameExistsTitle"), JOptionPane.PLAIN_MESSAGE); // user-input for new desktop-description String newDeskName = (String) JOptionPane.showInputDialog(this, resourceMap.getString("newDesktopMsg"), resourceMap.getString("newDesktopTitle"), JOptionPane.PLAIN_MESSAGE); // check for valid-return value, or if the user cancelled the action if (newDeskName != null && !newDeskName.isEmpty()) { // if everything was ok, set new name archive.getRootElement().setAttribute("name", newDeskName); } else { // else user has cancelled process JOptionPane.showMessageDialog(this, resourceMap.getString("openArchiveCancelled"), resourceMap.getString("openArchiveDlgTitle"), JOptionPane.PLAIN_MESSAGE); return; } } else if (DesktopData.IMPORT_ARCHIVE_ERR_OTHER == result) { // tell user about problem JOptionPane.showMessageDialog(this, resourceMap.getString("openArchiveError"), resourceMap.getString("openArchiveDlgTitle"), JOptionPane.PLAIN_MESSAGE); // and show error log zknframe.showErrorIcon(); return; } else if (DesktopData.IMPORT_ARCHIVE_OK == result) { // everything is ok, so quit while-loop finished = true; } } // show success JOptionPane.showMessageDialog(this, resourceMap.getString("openArchiveOK"), resourceMap.getString("openArchiveTitle"), JOptionPane.PLAIN_MESSAGE); // and update combo box updateComboBox(false); } }