Example usage for org.dom4j.io XMLWriter write

List of usage examples for org.dom4j.io XMLWriter write

Introduction

In this page you can find the example usage for org.dom4j.io XMLWriter write.

Prototype

public void write(Object object) throws IOException 

Source Link

Document

Writes the given object which should be a String, a Node or a List of Nodes.

Usage

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

/**
     * write all the records of the given table.
     * @param dbTable the class name of the table
     * @return creates an xml file with name of the table
     *///from   w ww.  java2s . c o m
    @SuppressWarnings("unchecked")
    public void writeXMLfile(String dataBase) {
        FileOutputStream fout;

        Session dom4jSession = session.getSession(EntityMode.DOM4J);
        String query = "from " + dataBase + " where id = 1"; //$NON-NLS-1$ //$NON-NLS-2$

        System.out.println(query);

        List userXML = dom4jSession.createQuery(query).list();
        try {
            fout = new FileOutputStream(importFolderPath + dataBase + ".xml"); //$NON-NLS-1$
            PrintStream p = new PrintStream(fout);
            p.print("<root>"); //$NON-NLS-1$
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(fout, format);

            for (int i = 0; i < userXML.size(); i++) {
                Element writeMe = (Element) userXML.get(i);
                writer.write(writeMe);
            }
            p.println("\n</root>"); //$NON-NLS-1$
            p.close();
            fout.close();
            writer.close();
            System.out.println("Wrote: " + dataBase + ".xml"); //$NON-NLS-1$ //$NON-NLS-2$
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            ex.printStackTrace();
        }

    }

From source file:edu.ku.brc.specify.tasks.services.CollectingEventLocalityKMLGenerator.java

License:Open Source License

/**
 * Write the KML out to a file./*from   ww  w.jav  a 2  s  . c  o m*/
 *
 * @param filename the name of the output file
 * @throws IOException a file I/O exception occurred
 */
public void outputToFile(final String filename) throws IOException {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("kml").addAttribute("xmlns", KML_NAMESPACE_DECL);
    Element kmlDocument = root.addElement("Document");
    if (StringUtils.isNotEmpty(description)) {
        kmlDocument.addElement("description").addText(description);
    }
    GenericKMLGenerator.generateStyle(kmlDocument, placemarkIconURL, balloonStyleBgColor, balloonStyleTextColor,
            balloonStyleText);

    boolean isDoingCollectingEvents = false;
    DataProviderSessionIFace session = null;
    try {
        session = DataProviderFactory.getInstance().createSession();
        for (int i = 0; i < dataObjs.size(); ++i) {
            String label = labels.get(i);
            FormDataObjIFace dataObj = dataObjs.get(i);

            session.attach(dataObj);

            if (dataObj instanceof CollectingEvent) {
                generatePlacemark(kmlDocument, (CollectingEvent) dataObj, label);
                isDoingCollectingEvents = true;

            } else if (dataObj instanceof Locality) {
                generatePlacemark(kmlDocument, (Locality) dataObj, label);

            } else if (dataObj instanceof CollectionObject) {
                generatePlacemark(kmlDocument, (CollectionObject) dataObj, label);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();

    } finally {
        if (session != null) {
            session.close();
        }
    }

    if (isDoingCollectingEvents) {
        /*String kmlStr = generatePathForLocalities();
        if (kmlStr != null)
          {
        writer.write(kmlStr);
          }*/
    }

    FileWriter out = new FileWriter(filename);
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(out, format);
    writer.write(document);
    writer.close();
    out.close();
}

From source file:edu.ku.brc.specify.toycode.L18NStringResApp.java

License:Open Source License

/**
 * @param file// w w w.  j a  v  a 2s  . c om
 */
public void process(final File fileArg, final boolean doDiffs) {
    try {
        String dirName = RES_PATH + "values-" + destLocale.getLanguage();
        String path = dirName + File.separator + fileArg.getName();

        File file = fileArg;
        if (doDiffs) {
            file = new File(path);
        }
        Document doc = readFileToDOM4J(new FileInputStream(file));
        Node root = doc.getRootElement();

        for (Object nodeObj : root.selectNodes("/resources/string")) {
            Node node = (Node) nodeObj;
            String name = XMLHelper.getAttr((Element) node, "name", null);

            if (doDiffs) {
                if (baseHash.get(name) != null) {
                    continue;
                }
            }

            String text = node.getText();
            String transText = translate(text);
            if (transText != null) {
                node.setText(transText);
            }
            System.out.println(name + "[" + text + "][" + transText + "]");
        }

        File dir = new File(dirName);
        if (!dir.exists()) {
            dir.mkdir();
        }

        FileOutputStream fos = new FileOutputStream(path);
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(doc);
        writer.flush();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.ku.brc.util.services.GenericKMLGenerator.java

License:Open Source License

/**
 * Generates KML output based on the current points, names and descriptions given to the generator.
 * /*from w  ww .  jav  a 2  s . c  o m*/
 * @param out a stream to which the KML is written
 * @throws IOException if an I/O error occurs
 */
public void generateKML(final FileWriter out) throws IOException {
    Document kml = generateKML();
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(out, format);
    writer.write(kml);
    writer.close();
}

From source file:edu.scripps.fl.pubchem.promiscuity.XMLDocument.java

License:Apache License

public void write(Document doc, File toFile) throws IOException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(toFile), format);
    writer.write(doc);
    writer.close();//w  ww.j  a  va  2s .c  o m
}

From source file:edu.scripps.fl.pubchem.web.session.WebSessionBase.java

License:Apache License

protected void debugDocumentToTempFile(Document doc, boolean displayFile) throws IOException {
    File file = File.createTempFile(getClass().getName(), ".html");
    XMLWriter writer = new XMLWriter(new FileOutputStream(file));
    writer.write(doc);
    writer.close();//from www  . j ava 2s  .c  om
    if (displayFile)
        Desktop.getDesktop().open(file);
}

From source file:edu.scripps.fl.pubchem.xml.PubChemXMLDoc.java

License:Apache License

public void write(Document doc, File toFile) throws IOException {
    fixAttribute(doc);/*from w  w  w  . j  a va 2  s  .  com*/
    organizeXMLDoc(doc);

    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(toFile), format);
    writer.write(doc);
    writer.close();
}

From source file:edu.ucsd.library.xdre.web.CollectionOperationController.java

/**
 * Serialize xml document to file/*from  w  w w  .jav a 2  s .c  om*/
 * @param destFile
 * @param doc
 * @throws IOException
 */
public static void writeXml(File destFile, Document doc) throws IOException {
    OutputStreamWriter out = null;
    XMLWriter writer = null;
    OutputFormat pretty = OutputFormat.createPrettyPrint();
    try {
        out = new FileWriter(destFile);
        writer = new XMLWriter(out, pretty);
        writer.write(doc);
    } finally {
        CollectionHandler.close(out);
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            writer = null;
        }
    }
}

From source file:edu.umd.cs.findbugs.AddMessages.java

License:Open Source License

@SuppressFBWarnings("DM_EXIT")
public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println("Usage: " + AddMessages.class.getName() + " <input collection> <output collection>");
        System.exit(1);/*from w ww. j av  a2s  .  c  om*/
    }

    // Load plugins, in order to get message files
    DetectorFactoryCollection.instance();

    String inputFile = args[0];
    String outputFile = args[1];
    Project project = new Project();

    SortedBugCollection inputCollection = new SortedBugCollection(project);
    inputCollection.readXML(inputFile);

    Document document = inputCollection.toDocument();

    AddMessages addMessages = new AddMessages(inputCollection, document);
    addMessages.execute();

    XMLWriter writer = new XMLWriter(new BufferedOutputStream(new FileOutputStream(outputFile)),
            OutputFormat.createPrettyPrint());
    writer.write(document);
    writer.close();
}

From source file:edu.umd.cs.findbugs.ml.GenerateUIDs.java

License:Open Source License

@SuppressWarnings("unchecked")
public void execute() throws IOException, DocumentException {
    InputStream in = null;//  w  w w  .jav  a2 s. c om
    try {
        if (inputFilename.equals("-")) {
            in = System.in;
        } else {
            in = new BufferedInputStream(new FileInputStream(inputFilename));
            if (inputFilename.endsWith(".gz"))
                in = new GZIPInputStream(in);
        }

        bugCollection.readXML(in);
        in = null;
    } finally {
        if (in != null)
            in.close();
    }
    Document document = DocumentFactory.getInstance().createDocument();
    Dom4JXMLOutput xmlOutput = new Dom4JXMLOutput(document);
    bugCollection.writeXML(xmlOutput);

    int count = 0;

    List<Element> bugInstanceList = document.selectNodes("/BugCollection/BugInstance");
    for (Element element : bugInstanceList) {
        Attribute uidAttr = element.attribute("uid");
        if (uidAttr == null) {
            element.addAttribute("uid", Integer.toString(count++));
        }
    }

    OutputStream out;
    if (outputFilename.equals("-")) {
        out = System.out;
    } else {
        out = new BufferedOutputStream(new FileOutputStream(outputFilename));
    }

    XMLWriter xmlWriter = new XMLWriter(out, OutputFormat.createPrettyPrint());
    try {
        xmlWriter.write(document);
    } finally {
        xmlWriter.close();
    }
}