Example usage for org.jdom2 Document setRootElement

List of usage examples for org.jdom2 Document setRootElement

Introduction

In this page you can find the example usage for org.jdom2 Document setRootElement.

Prototype

public Document setRootElement(Element rootElement) 

Source Link

Document

This sets the root Element for the Document.

Usage

From source file:com.js.quickestquail.ui.actions.io.ExportToXMLAction.java

private void writeAll(File outputFile) {

    // progress dialog
    JProgressDialog dialog = new JProgressDialog(UI.get(), false);
    dialog.setMaximum(DriveManager.get().getSelected().size());
    dialog.setTitle(java.util.ResourceBundle.getBundle("i18n/i18n").getString("export.xml"));
    dialog.setVisible(true);/* ww w  .ja v  a 2  s  . c  om*/

    // process in new thread
    new Thread() {
        @Override
        public void run() {
            try {
                Element rootElement = new Element("movies");

                List<Entry<File, String>> entries = new ArrayList<>(
                        DriveManager.get().getSelected().entrySet());
                java.util.Collections.sort(entries, new Comparator<Entry<File, String>>() {
                    @Override
                    public int compare(Entry<File, String> o1, Entry<File, String> o2) {
                        Movie mov1 = CachedMovieProvider.get().getMovieByID(o1.getValue());
                        Movie mov2 = CachedMovieProvider.get().getMovieByID(o2.getValue());
                        return mov1.getTitle().compareTo(mov2.getTitle());
                    }
                });

                int nofMovies = 0;
                for (Entry<File, String> en : entries) {
                    Movie mov = CachedMovieProvider.get().getMovieByID(en.getValue());

                    // update progress dialog
                    dialog.setText(mov.getTitle());
                    dialog.setProgress(nofMovies);

                    Element movieElement = new Element("movie");

                    movieElement.addContent(makeElement("imdbid", mov.getImdbID()));
                    movieElement.addContent(makeElement("imdbrating", mov.getImdbRating() + ""));
                    movieElement.addContent(makeElement("imdbvotes", mov.getImdbVotes() + ""));

                    movieElement.addContent(makeElement("title", mov.getTitle()));
                    movieElement.addContent(makeElement("year", mov.getYear() + ""));
                    movieElement.addContent(makeElement("countries", "country", mov.getCountry()));
                    movieElement.addContent(makeElement("genres", "genre", mov.getGenre()));

                    movieElement.addContent(makeElement("writers", "writer", mov.getWriter()));
                    movieElement.addContent(makeElement("directors", "director", mov.getDirector()));
                    movieElement.addContent(makeElement("actors", "actor", mov.getActors()));

                    movieElement.addContent(makeElement("poster", mov.getPoster()));

                    movieElement.addContent(makeElement("plot", mov.getPlot()));

                    movieElement.addContent(makeElement("file", en.getKey().getAbsolutePath()));

                    rootElement.addContent(movieElement);

                    nofMovies++;
                }

                Document doc = new Document();
                doc.setRootElement(rootElement);

                // close IO
                XMLOutputter xmlOutput = new XMLOutputter();
                xmlOutput.setFormat(Format.getPrettyFormat());
                xmlOutput.output(doc, new FileWriter(outputFile));

                // close dialog
                dialog.setVisible(false);

            } catch (Exception ex) {
            }
        }
    }.start();
}

From source file:com.rometools.propono.atom.common.AtomService.java

License:Apache License

/**
 * Serialize an AtomService object into an XML document
 *//*www.  j  a v a2  s  .c  om*/
public Document serviceToDocument() {
    final AtomService service = this;

    final Document doc = new Document();
    final Element root = new Element("service", ATOM_PROTOCOL);
    doc.setRootElement(root);
    final List<Workspace> spaces = service.getWorkspaces();
    for (final Workspace space : spaces) {
        root.addContent(space.workspaceToElement());
    }
    return doc;
}

From source file:com.rometools.propono.atom.server.AtomServlet.java

License:Apache License

/**
 * Handles an Atom GET by calling handler and writing results to response.
 *//*  w ww. j  a  v a2s.  co  m*/
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse res)
        throws ServletException, IOException {
    LOG.debug("Entering");
    final AtomHandler handler = createAtomRequestHandler(req, res);
    final String userName = handler.getAuthenticatedUsername();
    if (userName != null) {
        final AtomRequest areq = new AtomRequestImpl(req);
        try {
            if (handler.isAtomServiceURI(areq)) {
                // return an Atom Service document
                final AtomService service = handler.getAtomService(areq);
                final Document doc = service.serviceToDocument();
                res.setContentType("application/atomsvc+xml; charset=utf-8");
                final Writer writer = res.getWriter();
                final XMLOutputter outputter = new XMLOutputter();
                outputter.setFormat(Format.getPrettyFormat());
                outputter.output(doc, writer);
                writer.close();
                res.setStatus(HttpServletResponse.SC_OK);
            } else if (handler.isCategoriesURI(areq)) {
                final Categories cats = handler.getCategories(areq);
                res.setContentType("application/xml");
                final Writer writer = res.getWriter();
                final Document catsDoc = new Document();
                catsDoc.setRootElement(cats.categoriesToElement());
                final XMLOutputter outputter = new XMLOutputter();
                outputter.output(catsDoc, writer);
                writer.close();
                res.setStatus(HttpServletResponse.SC_OK);
            } else if (handler.isCollectionURI(areq)) {
                // return a collection
                final Feed col = handler.getCollection(areq);
                col.setFeedType(FEED_TYPE);
                final WireFeedOutput wireFeedOutput = new WireFeedOutput();
                final Document feedDoc = wireFeedOutput.outputJDom(col);
                res.setContentType("application/atom+xml; charset=utf-8");
                final Writer writer = res.getWriter();
                final XMLOutputter outputter = new XMLOutputter();
                outputter.setFormat(Format.getPrettyFormat());
                outputter.output(feedDoc, writer);
                writer.close();
                res.setStatus(HttpServletResponse.SC_OK);
            } else if (handler.isEntryURI(areq)) {
                // return an entry
                final Entry entry = handler.getEntry(areq);
                if (entry != null) {
                    res.setContentType("application/atom+xml; type=entry; charset=utf-8");
                    final Writer writer = res.getWriter();
                    Atom10Generator.serializeEntry(entry, writer);
                    writer.close();
                } else {
                    res.setStatus(HttpServletResponse.SC_NOT_FOUND);
                }
            } else if (handler.isMediaEditURI(areq)) {
                final AtomMediaResource entry = handler.getMediaResource(areq);
                res.setContentType(entry.getContentType());
                res.setContentLength((int) entry.getContentLength());
                Utilities.copyInputToOutput(entry.getInputStream(), res.getOutputStream());
                res.getOutputStream().flush();
                res.getOutputStream().close();
            } else {
                res.setStatus(HttpServletResponse.SC_NOT_FOUND);
            }
        } catch (final AtomException ae) {
            res.sendError(ae.getStatus(), ae.getMessage());
            LOG.debug("An error occured while processing GET", ae);
        } catch (final Exception e) {
            res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
            LOG.debug("An error occured while processing GET", e);
        }
    } else {
        res.setHeader("WWW-Authenticate", "BASIC realm=\"AtomPub\"");
        res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    }
    LOG.debug("Exiting");
}

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);/*from www. j  a  v a2  s  . 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 www . j  a  v  a 2  s  .  c o m
        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   w  ww.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   w w  w . ja  v  a2  s  . c o 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);

    for (PmmXmlElementConvertable element : elementSet) {
        rootElement.addContent(element.toXmlElement());
    }/*from  ww w  . j a v  a2s .  c o m*/
    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);

    if (agentXml != null) {
        rootElement.addContent(agentXml.toXmlElement());
    }//from   ww w .j  a v  a  2s  .c  o  m
    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);

    if (agentXml != null) {
        rootElement.addContent(agentXml.toXmlElement());
    }//from   w  w  w . j a v  a 2s  . c  o m
    if (matrixXml != null) {
        rootElement.addContent(matrixXml.toXmlElement());
    }
    for (MDLiteratureItem literatureItem : literatureItems) {
        rootElement.addContent(literatureItem.toXmlElement());
    }

    return doc;
}