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:cz.mzk.editor.server.fedora.utils.DocumentSaver.java

License:Open Source License

public static boolean writeDocument(Document document, OutputStream out, PrintType printType) {
    OutputFormat format = chooseOutputFormat(printType);
    try {/*from w  ww  .ja  v  a 2  s. c  om*/
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        writer.flush();
        return true;
    } catch (UnsupportedEncodingException ex) {
        logger.log(Level.SEVERE, null, ex);
        return false;
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:DAO.LocationDAO.java

public boolean addNewLocation(String webAppPath, String location) {
    try {/*from www . ja  va 2  s  . co m*/
        SAXReader reader = new SAXReader();
        Document document = reader.read(webAppPath + "/xml/Locations.xml");
        Element root = document.getRootElement();
        int max = 0;
        for (Iterator i = root.elementIterator("Location"); i.hasNext();) {
            Element elt = (Element) i.next();
            int cur = Integer.parseInt(elt.element("LocationId").getText());
            if (cur > max) {
                max = cur;
            }
        }

        Integer newLocationId = max + 1;
        Element newLocation = root.addElement("Location");
        newLocation.addElement("LocationId").setText(newLocationId.toString());
        newLocation.addElement("Name").setText(location);
        root.appendAttributes(newLocation);
        try {
            FileOutputStream fos = new FileOutputStream(webAppPath + "/xml/Locations.xml");
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter xmlwriter = new XMLWriter(fos, format);
            xmlwriter.write(document);
            System.out.println("dead");
            xmlwriter.close();
        } catch (Exception ex) {
            System.out.println("Failed!");
            ex.printStackTrace();
        }
        //Node node = document.selectSingleNode("/Locations/Location[not(../Location/LocationId > LocationId)]");
        //String id = node.valueOf("LocationId");
        //System.out.println(id);
        return true;
    } catch (DocumentException ex) {
        System.out.println("Add New Location Failed!");
        Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:datasource.XMLTools.java

License:Open Source License

public static long writeToXML(Document doc, String Filename) {
    long errorcode = 0;

    try {/*  w w w  .  j  a v  a 2  s .  co  m*/
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setEncoding("UTF-8");
        FileOutputStream out = new FileOutputStream(Filename);
        XMLWriter writer = new XMLWriter(out, outformat);
        writer.write(doc);
        writer.flush();
    } catch (Exception x) {
        System.out.println(x.getMessage());
        errorcode = x.hashCode();
    }
    return errorcode;
}

From source file:de.ailis.wlandsuite.game.blocks.GameBlock.java

License:Open Source License

/**
 * Writes the block to a stream as XML/* w  w w . ja  va  2 s .c om*/
 *
 * @param stream
 *            The output stream
 * @throws IOException
 *             When file operation fails.
 */

public void writeXml(final OutputStream stream) throws IOException {
    XMLWriter writer;
    Document document;
    OutputFormat format;

    format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);

    writer = new XMLWriter(stream, format);
    try {
        final Element rootElement = toXml();
        document = DocumentHelper.createDocument(rootElement);
        writer.write(document);
    } finally {
        writer.close();
    }
}

From source file:de.ailis.xadrian.utils.XmlUtils.java

License:Open Source License

/**
 * Writes the specified document to the specified file.
 *
 * @param document//from  w  w  w. ja  v  a 2  s .c  om
 *            The document to write
 * @param file
 *            The file to write to
 * @throws IOException
 *             If file could not be written
 */
public static void write(final Document document, final File file) throws IOException {
    final OutputFormat format = OutputFormat.createPrettyPrint();
    final XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), format);
    try {
        writer.write(document);
    } finally {
        writer.close();
    }
}

From source file:de.awtools.xml.TransformerUtils.java

License:Open Source License

/**
 * Schreibt ein dom4j Dokument in eine Datei.
 *
 * @param document Ein dom4j Dokument./*from  w w w . j  ava2 s  . c o m*/
 * @param file Die zu schreibende Datei.
 * @param encoding Das Encoding.
 * @throws UnhandledException Da ging was schief.
 */
public static void toFile(final Document document, final File file, final String encoding)
        throws UnhandledException {

    XMLWriter writer = null;
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        writer = new XMLWriter(new FileWriter(file), format);
        writer.write(document);
    } catch (IOException ex) {
        log.debug("Fehler: ", ex);
        throw new UnhandledException(ex);
    } finally {
        XMLUtils.close(writer);
    }
}

From source file:de.codecentric.multitool.xml.XmlLibrary.java

License:Apache License

/**
 * Konvertiert ein XML-Document in einen XML-String
 * /*from  w ww.  j  a va 2  s.co  m*/
 * | ${xmlString} = | Get Xml From String | ${xmlDoc} |
 */
public String getStringFromXml(Document document) throws IOException {
    OutputFormat format = OutputFormat.createCompactFormat();
    format.setSuppressDeclaration(true);
    StringWriter writer = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(document);
    return writer.toString();
}

From source file:de.fmaul.android.cmis.utils.StorageUtils.java

License:Apache License

public static void storeFeedInCache(Application app, String url, Document doc, String workspace)
        throws StorageException {
    File cacheFile = getFeedFile(app, workspace, md5(url));
    ensureOrCreatePathAndFile(cacheFile);

    try {/*from  w  w w .j  a  v  a  2  s . co  m*/
        XMLWriter writer = new XMLWriter(new FileOutputStream(cacheFile));
        writer.write(doc);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:de.innovationgate.utils.WGUtils.java

License:Apache License

/**
 * Creates a directory link file pointing to a target path
 * @param parentDir The directory to contain the link file
 * @param target The target path that the directory link should point to
 * @throws IOException//from  w w  w.jav  a  2 s. co m
 */
public static void createDirLink(File parentDir, String target) throws IOException {
    File link = new File(parentDir, DIRLINK_FILE);
    Document doc = DocumentFactory.getInstance().createDocument();
    Element dirlink = doc.addElement("dirlink");
    Element path = dirlink.addElement("path");
    path.addAttribute("location", target);
    XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
    writer.setOutputStream(new FileOutputStream(link));
    writer.write(doc);
    writer.close();
}

From source file:de.innovationgate.wga.common.beans.hdbmodel.ModelDefinition.java

License:Open Source License

public static ModelDefinition read(InputStream in) throws Exception {

    // First read XML manually and validate against the DTD provided in this OpenWGA distribution (to prevent it being loaded from the internet, which might not work, see #00003612) 
    SAXReader reader = new SAXReader();
    reader.setIncludeExternalDTDDeclarations(true);
    reader.setIncludeInternalDTDDeclarations(true);
    reader.setEntityResolver(new EntityResolver() {

        @Override/*w w  w.ja  v a  2 s.com*/
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {

            if (systemId.equals("http://doc.openwga.com/hdb-model-definition-6.0.dtd")) {
                return new InputSource(ModelDefinition.class.getClassLoader().getResourceAsStream(
                        WGUtils.getPackagePath(ModelDefinition.class) + "/hdb-model-definition-6.0.dtd"));
            } else {
                // use the default behaviour
                return null;
            }

        }

    });

    org.dom4j.Document domDoc = reader.read(in);

    // Remove doctype (we already have validated) and provide the resulting XML to the serializer
    domDoc.setDocType(null);
    StringWriter out = new StringWriter();
    XMLWriter writer = new XMLWriter(out);
    writer.write(domDoc);
    String xml = out.toString();

    return _serializer.read(ModelDefinition.class, new StringReader(xml));
}