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:com.cladonia.xml.ExchangerDocument.java

License:Open Source License

private void writeText() {
    if (DEBUG)//from  www.  j a  v  a 2  s . co  m
        System.out.println("ExchangerDocument.writeText()");
    try {
        XElement root = getRoot();
        if (root != null) {
            elementNames = new Hashtable();
            attributeNames = new Hashtable();
            namespaces = new Vector();

            setNames(elementNames, attributeNames, namespaces, getRoot());
        }

        //         System.out.println( document.asXML());

        StringWriter writer = new StringWriter();
        ExchangerOutputFormat format = new ExchangerOutputFormat("", false, getEncoding());

        if (hasDeclaration()) {
            if (getStandalone() != STANDALONE_NONE) {
                format.setStandalone(getStandalone());
                format.setOmitStandalone(false);
            }

            format.setVersion(getVersion());
            format.setOmitEncoding(!hasEncoding());
            format.setSuppressDeclaration(false);
        } else {
            format.setSuppressDeclaration(true);
        }

        //         if ( !getRoot().hasContent()) {
        //            format.setExpandEmptyElements( true);
        //         }

        XMLWriter formatter = new ExchangerXMLWriter(writer, format);
        formatter.write(document);
        formatter.flush();

        text = writer.toString();

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

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Writes the document to the location specified by the URL,
 * using the default XML writer!/*from   ww w  . j  ava2 s  . c o  m*/
 *
 * @param document the dom4j document.
 * @param url the URL of the document.
 */
public static synchronized void write(XDocument document, URL url) throws IOException {
    if (DEBUG)
        System.out.println("XMLUtilities.write( " + document + ", " + url + ")");

    File documentFile = null;

    documentFile = new File(url.getFile());

    if (documentFile != null) {
        FileOutputStream out = new FileOutputStream(documentFile);

        //      XMLWriter writer = new XMLWriter( out, format);
        XMLWriter writer = new XMLWriter(out,
                new OutputFormat(TextPreferences.getTabString(), true, document.getEncoding()));
        writer.write(document);
        writer.flush();

        out.flush();
        out.close();

    } else {
        throw new IOException(url.toExternalForm() + " (The system cannot find the path specified)");
    }
}

From source file:com.cnd.greencube.server.util.dom4j.XmlUtils.java

License:Open Source License

public String Dom2String(Document doc) {

    XMLWriter writer = null;
    try {/*  w ww.j  ava  2  s . c  o m*/
        StringWriter sw = new StringWriter();
        // OutputFormat format = OutputFormat.createPrettyPrint();
        // format.setEncoding("gb2312");

        writer = new XMLWriter(sw);
        writer.write(doc);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != writer)
            try {
                writer.close();
            } catch (Exception ie) {
            }
    }
    return null;
}

From source file:com.cnd.greencube.server.util.dom4j.XmlUtils.java

License:Open Source License

public String Dom2String(Document doc, String encoding) {

    XMLWriter writer = null;
    try {//from  w  ww. jav a 2  s .  c  o m
        StringWriter sw = new StringWriter();
        writer = new XMLWriter(sw);
        writer.write(doc);
        String xml = sw.toString();

        String pattern = ">";
        int idx = xml.indexOf(pattern);
        String last = xml.substring(idx + 1);
        xml = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>" + last;
        return xml;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != writer)
            try {
                writer.close();
            } catch (Exception ie) {
            }
    }
    return null;
}

From source file:com.cnd.greencube.server.util.dom4j.XmlUtils.java

License:Open Source License

public void saveXml2File(Document doc, File file) {
    XMLWriter writer = null;
    try {/*w  w  w. j  a v a  2s. com*/
        FileWriter fw = new FileWriter(file);
        // ??xml?????XML
        // OutputFormat format = OutputFormat.createCompactFormat();
        // format.setEncoding("UTF-8");

        writer = new XMLWriter(fw);
        writer.write(doc);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != writer)
            try {
                writer.close();
            } catch (Exception ie) {
            }
    }
}

From source file:com.controlj.addon.weather.util.Logging.java

License:Open Source License

private static String toXMLString(String reason, Document document) throws IOException {
    StringWriter stringWriter = new StringWriter();
    PrintWriter pw = new PrintWriter(stringWriter);
    pw.println(reason);/*from   w  w  w  . jav  a  2 s .c  o  m*/
    pw.flush();

    XMLWriter writer = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
    writer.write(document);
    return stringWriter.toString();
}

From source file:com.davis.bluefolder.BlueUtils.java

public String formatXML(String input) {
    try {//  w w  w  .  j a v a  2  s. com
        org.dom4j.Document doc = DocumentHelper.parseText(input);
        StringWriter sw = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setIndent(true);
        format.setIndentSize(3);
        XMLWriter xw = new XMLWriter(sw, format);
        xw.write(doc);

        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return input;
    }
}

From source file:com.dockingsoftware.dockingpreference.PreferenceProcessor.java

License:Apache License

/**
 * Store user preference information to a specified file.
 * /*from  w  ww.  j  av  a2 s.  com*/
 * @param obj 
 * @param fullPath Specified the full path of the configuration file.
 */
public void store(Object obj, String fullPath) {
    try {
        FileWriter out;
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement(getName(obj.getClass()));
        root.addAttribute(GlobalConstant.CLASS, obj.getClass().getName());

        List<Field> pFields = getPreferenceFieldList(obj.getClass());
        for (int i = 0; i < pFields.size(); i++) {
            store(obj, pFields.get(i), root);
        }

        OutputFormat format = new OutputFormat("  ", true);
        out = new FileWriter(new File(fullPath));
        XMLWriter w = new XMLWriter(out, format);
        w.write(document);
        w.close();
        out.close();

    } catch (IOException ex) {
        Logger.getLogger(PreferenceProcessor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalArgumentException ex) {
        Logger.getLogger(PreferenceProcessor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(PreferenceProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.doculibre.constellio.izpack.PersistenceXMLUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void run(AbstractUIProcessHandler handler, String[] args) {
    if (args.length != 5) {
        System.out.println("persistence_mysqlPath defaultPersistencePath server login password");
        return;//www . j  a  v a 2  s .c  o m
    }

    String persistence_mysqlPath = args[0];
    String defaultPersistencePath = args[1];
    String server = args[2];
    String login = args[3];
    String password = args[4];

    Document xmlDocument;
    try {
        xmlDocument = new SAXReader().read(persistence_mysqlPath);
        Element root = xmlDocument.getRootElement();
        Iterator<Element> it = root.elementIterator("persistence-unit");
        if (!it.hasNext()) {
            System.out.println("Corrupt persistence file :" + persistence_mysqlPath);
            return;
        }
        it = it.next().elementIterator("properties");
        if (!it.hasNext()) {
            System.out.println("Corrupt persistence file :" + persistence_mysqlPath);
            return;
        }
        Element properties = it.next();
        for (it = properties.elementIterator("property"); it.hasNext();) {
            Element property = it.next();
            String id = property.attributeValue("name");
            if (id.equals(SERVER_ELEMENT_ID)) {
                Attribute att = property.attribute("value");
                att.setText(BEFORE_SERVER_NAME + server + AFTER_SERVER_NAME);
            } else {
                if (id.equals(LOGIN_ELEMENT_ID)) {
                    Attribute att = property.attribute("value");
                    att.setText(login);
                } else {
                    if (id.equals(PASSWORD_ELEMENT_ID)) {
                        Attribute att = property.attribute("value");
                        att.setText(password);
                    }
                }
            }

        }

        OutputFormat format = OutputFormat.createPrettyPrint();

        File xmlFile = new File(persistence_mysqlPath);
        XMLWriter writer2 = new XMLWriter(new FileOutputStream(xmlFile), format);

        writer2.write(xmlDocument);
        writer2.close();
        // copier au fichier de persistence par dfaut:
        xmlFile = new File(defaultPersistencePath);
        writer2 = new XMLWriter(new FileOutputStream(xmlFile), format);

        writer2.write(xmlDocument);
        writer2.close();

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.doculibre.constellio.izpack.TomcatUtil.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void run(AbstractUIProcessHandler handler, String[] args) {
    if (args.length != 2) {
        System.out.println("serverPath port");
        return;/*  ww  w.  j  a v a 2  s . co  m*/
    }

    String serverPath = args[0];
    String port = args[1];
    if (port.equals("8080")) {
        // C'est celui par defaut => ne rien faire
        return;
    }

    Document xmlDocument;
    try {
        xmlDocument = new SAXReader().read(serverPath);
        Element root = xmlDocument.getRootElement();

        Iterator<Element> it = root.elementIterator("Service");
        if (!it.hasNext()) {
            System.out.println("Corrupt persistence file :" + serverPath);
            return;
        }
        Element connectors = it.next();
        for (it = connectors.elementIterator("Connector"); it.hasNext();) {
            Element connector = it.next();
            String id = connector.attributeValue("protocol");
            if (id.startsWith("HTTP")) {
                Attribute att = connector.attribute("port");
                att.setText(port);
                break;
            }
        }

        OutputFormat format = OutputFormat.createPrettyPrint();

        File xmlFile = new File(serverPath);
        XMLWriter writer2 = new XMLWriter(new FileOutputStream(xmlFile), format);

        writer2.write(xmlDocument);
        writer2.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}