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:dk.netarkivet.common.utils.StreamUtils.java

License:Open Source License

/**
 * Write document tree to stream. Note, the stream is flushed, but not closed.
 *
 * @param doc the document tree to save.
 * @param os the stream to write xml to/*from  w w  w . j av a  2s  . c  o m*/
 * @throws IOFailure On trouble writing XML to stream.
 */
public static void writeXmlToStream(Document doc, OutputStream os) {
    ArgumentNotValid.checkNotNull(doc, "Document doc");
    ArgumentNotValid.checkNotNull(doc, "OutputStream os");
    XMLWriter xwriter = null;
    try {
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding(UTF8_CHARSET);
            xwriter = new XMLWriter(os, format);
            xwriter.write(doc);
        } finally {
            if (xwriter != null) {
                xwriter.close();
            }
            os.flush();
        }
    } catch (IOException e) {
        String errMsg = "Unable to write XML to stream";
        log.warn(errMsg, e);
        throw new IOFailure(errMsg, e);
    }
}

From source file:dk.netarkivet.harvester.datamodel.H1HeritrixTemplate.java

License:Open Source License

@Override
public void writeTemplate(OutputStream os) throws IOException, ArgumentNotValid {
    XMLWriter writer;
    try {/*from   w w  w.  java2s  . co m*/
        writer = new XMLWriter(os);
        writer.write(this.template);
    } catch (UnsupportedEncodingException e) {
        String errMsg = "The encoding of this template is unsupported by this environment";
        log.error(errMsg, e);
        throw new ArgumentNotValid(errMsg, e);
    }
}

From source file:dk.netarkivet.harvester.tools.HarvestTemplateApplication.java

License:Open Source License

/**
 * Download the template with a given name.
 * The template is assumed to exist.//from  w  ww  .j  a  va2  s .  co m
 * @param templateName The name of a given template
 */
private static void download(final String templateName) {
    System.out.println("Downloading template '" + templateName + "'.");
    try {
        TemplateDAO dao = TemplateDAO.getInstance();
        HeritrixTemplate doc = dao.read(templateName);
        OutputStream os = new FileOutputStream(templateName + ".xml");
        XMLWriter writer = new XMLWriter(os);
        writer.write(doc.getTemplate());
    } catch (IOException e) {
        System.err.println("Error downloading template '" + templateName + "': " + e);
        e.printStackTrace(System.err);
    }
}

From source file:dk.nsi.stamdata.replication.tools.SchemaGenerator.java

License:Mozilla Public License

public static void generate(Class<? extends View> entity, Writer writer) throws IOException {
    Document doc = DocumentFactory.getInstance().createDocument();

    String targetNamespace = entity.getPackage().getAnnotation(XmlSchema.class).namespace();
    String entityName = entity.getSimpleName().toLowerCase();

    Element all = generate(doc, targetNamespace, entityName);

    for (Field method : entity.getDeclaredFields()) {
        if (method.isAnnotationPresent(XmlTransient.class))
            continue;

        String name = method.getName();
        String type = convert2SchemaType(method);
        addElement(all, name, type, null);
    }/*from w w  w  .j a v a  2s  .com*/

    XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint());
    xmlWriter.write(doc);
}

From source file:dk.nsi.stamdata.replication.tools.SchemaGenerator.java

License:Mozilla Public License

public static void generate(RecordSpecification specification, Writer writer, String register)
        throws IOException {
    Document doc = DocumentFactory.getInstance().createDocument();

    // FIXME: Register added to record spec.

    String namespace = Namespace.STAMDATA_3_0 + "/" + register;
    String entityName = specification.getTable().toLowerCase();

    Element all = generate(doc, namespace, entityName);

    for (RecordSpecification.FieldSpecification field : specification.getFieldSpecs()) {
        addElement(all, field.name, convert2XsdType(field.type), field.length);
    }/*from   ww  w .  j ava 2s  .c  o m*/

    all.addElement("xs:element").addAttribute("name", "validFrom").addAttribute("type", "xs:dateTime");
    all.addElement("xs:element").addAttribute("name", "validTo").addAttribute("type", "xs:dateTime");

    XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint());
    xmlWriter.write(doc);
}

From source file:dom4j.Dom4JExample.java

public void write(Document document) throws IOException {

    // lets write to a file
    XMLWriter writer;
    //        = new XMLWriter(
    //            new BufferedOutputStream(outputStream));
    //        writer.write( document );
    //        writer.close();

    // Pretty print the document to System.out
    System.out.println("\n\nPretty format");
    OutputFormat format = OutputFormat.createPrettyPrint();
    writer = new XMLWriter(System.out, format);
    writer.write(document);

    // Compact format to System.out
    System.out.println("\n\nCompact format");
    format = OutputFormat.createCompactFormat();
    writer = new XMLWriter(System.out, format);
    writer.write(document);/* w w w. j av  a  2s.  c  o  m*/
}

From source file:edu.ccut.saturn.manager.dict.core.dbmanager.Dom4jUtils.java

License:Apache License

public static String writeXml(String file, Document doc) {

    String message = null;/*from www .jav  a  2 s . c o  m*/
    XMLWriter writer = null;
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("GB2312");
    format.setIndent(true);
    format.setIndent(" ");
    format.setNewlines(true);
    OutputStream out = null;
    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e1) {
        try {
            String path = Thread.currentThread().getContextClassLoader().getResource(BLANK_STRING).getFile()
                    + file.substring(1);

            out = new FileOutputStream(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    try {
        writer = new XMLWriter(out, format);
        writer.write(doc);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    message = TRUE;
    return message;
}

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

License:Open Source License

/**
     * print all the records of a table.
     * @param dbTable the class name of the table
     *//*ww  w  . ja  va2 s .co m*/
    @SuppressWarnings("unchecked")
    public void printXML(String dbTable) {
        Session dom4jSession = session.getSession(EntityMode.DOM4J);
        String query = "from " + dbTable; //$NON-NLS-1$

        List userXML = dom4jSession.createQuery(query).list();
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(System.out, format);

            for (int i = 0; i < userXML.size(); i++) {
                Element writeMe = (Element) userXML.get(i);
                writer.write(writeMe);
            }
        } 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

/**
     * print a single record//from  w ww. j a  va  2s. c o  m
     * @param dbTable the class name of the table
     * @param id the id number of the record
     */
    @SuppressWarnings("unchecked")
    public void printSingleRecordXML(String dbTable, int id) {
        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 {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(System.out, format);

            for (int i = 0; i < userXML.size(); i++) {
                Element writeMe = (Element) userXML.get(i);
                writer.write(writeMe);
            }
        } 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 a single record/*from w ww .j a  v  a  2  s.  c o m*/
     * @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();
    }