Example usage for org.jdom2.output Format getPrettyFormat

List of usage examples for org.jdom2.output Format getPrettyFormat

Introduction

In this page you can find the example usage for org.jdom2.output Format getPrettyFormat.

Prototype

public static Format getPrettyFormat() 

Source Link

Document

Returns a new Format object that performs whitespace beautification with 2-space indents, uses the UTF-8 encoding, doesn't expand empty elements, includes the declaration and encoding, and uses the default entity escape strategy.

Usage

From source file:XMLWriter.WriteUsersXML.java

License:Open Source License

public static boolean changeName(User usuario, String xmlSource) {
    try {//from   w  w  w  .j  a  va 2  s. c om
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        boolean find = false;

        for (Element e : rootNode.getChildren("user", ns))
            if (e.getAttributeValue("id").equals(usuario.getID())) {
                e.getChild("nombres", ns).setText(usuario.getNombres());
                e.getChild("apPat", ns).setText(usuario.getApPaterno());
                e.getChild("apMat", ns).setText(usuario.getApMaterno());
                find = true;
                break;
            }

        if (!find)
            return false;

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }

    return true;
}

From source file:XMLWriter.WriteUsersXML.java

License:Open Source License

public static boolean addCourse(String idUser, String idCourse, String xmlSource) {
    try {/* w w w . j  av  a  2  s  . co m*/
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        boolean find = false;

        for (Element e : rootNode.getChildren("user", ns))
            if (e.getAttributeValue("id").equals(idUser)) {
                find = true;
                Element idCu = new Element("idCurso", ns);
                idCu.setText(idCourse);
                e.setContent(e.indexOf(e.getChild("idCurso", ns)), idCu);
                break;
            }
        if (!find)
            return false;

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }

    return true;
}

From source file:XMLWriter.WriteUsersXML.java

License:Open Source License

public static boolean removeCourse(String idUser, String idCourse, String xmlSource) {
    try {//  w w  w .ja  v  a 2  s . c  o m
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        boolean find = false;

        for (Element e : rootNode.getChildren("user", ns))
            if (e.getAttributeValue("id").equals(idUser))
                for (Element c : e.getChildren("idCurso", ns))
                    if (c.getTextTrim().equals(idCourse)) {
                        find = e.removeContent(c);
                        break;
                    }

        if (!find)
            return false;

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }

    return true;
}

From source file:xmlwriter.XmlWriter.java

/**
 * Es usado para leer el nombre de el archivo que posteriormente ser escrito en un archivo con extension .xml
 * @param doc Documento Generado despues de la interaccion con el usuario y que ser escrito en un archivo con extension xml.
 * @return booleano que indica si encontraron errores al momento de escribir el archivo en el disco. Devuelve true si el archivo se guardo con exito y false de lo contrario.
 *//*w ww .j a  v  a2s  . co m*/
private static boolean saveDocument(Document doc) {

    try {
        String fileName = "";
        System.out.print("Nombre del archivo XML (sin la extensin): ");
        fileName = read.readLine();

        XMLOutputter xmlOutput = new XMLOutputter();
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, new FileWriter(fileName + ".xml"));
        return (true);
    } catch (IOException ex) {
        Logger.getLogger(XmlWriter.class.getName()).log(Level.SEVERE,
                "Error al escribir el archivo - saveDocument", ex);
        return (false);
    }

}