Example usage for org.dom4j.io XMLWriter XMLWriter

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

Introduction

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

Prototype

public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException 

Source Link

Usage

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

License:Open Source License

/**
     * write a single record// w  ww  .j  av  a2  s  . com
     * @param dbTable the class name of the table
     * @param id the id number of the record
     */
    @SuppressWarnings("unchecked")
    public void writeSingleRecordXML(String dbTable, int id) {
        FileOutputStream fout;

        Session dom4jSession = session.getSession(EntityMode.DOM4J);
        // load the object by using its primary key
        DBTableInfo info = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
        String primaryKey = info.getPrimaryKeyName();
        String query = "from " + dbTable + " where " + primaryKey + " = " + id; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

        List userXML = dom4jSession.createQuery(query).list();

        try {
            fout = new FileOutputStream(importFolderPath + dbTable + ".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: " + dbTable + ".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();
        }
        System.out.println();
    }

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 ww  w  .  j a v  a  2  s.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 www . j  a  v  a2  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.tasks.services.CollectingEventLocalityKMLGenerator.java

License:Open Source License

private String XMLtoString(Element el) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    Writer writer = new StringWriter();
    try {/*  w w w  .j av  a 2s  .c om*/
        new XMLWriter(writer, format).write(el);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return writer.toString();
}

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

License:Open Source License

/**
 * @param file/*from  w ww. j  ava  2  s . co  m*/
 */
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 w  w.j  a v 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.northwestern.bioinformatics.studycalendar.xml.AbstractStudyCalendarXmlSerializer.java

License:BSD License

protected String createDocumentString(Document doc) {
    StringWriter capture = new StringWriter();
    try {/* w  w  w . j ava  2  s. c o m*/
        new XMLWriter(capture, OUTPUT_FORMAT).write(doc);
    } catch (IOException e) {
        throw new StudyCalendarSystemException("Unexpected error when serializing XML", e);
    }
    return capture.toString();
}

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);/*from  w w  w .j  a v  a  2s .  c  o  m*/
    writer.close();
}

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

License:Apache License

public void write(Document doc, File toFile) throws IOException {
    fixAttribute(doc);/*  w  ww .  j  a  va 2 s . c o m*/
    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/* ww w. j  ava2  s. c o  m*/
 * @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;
        }
    }
}