Example usage for org.jdom2 Element removeContent

List of usage examples for org.jdom2 Element removeContent

Introduction

In this page you can find the example usage for org.jdom2 Element removeContent.

Prototype

@Override
    public Content removeContent(final int index) 

Source Link

Usage

From source file:XMLWriter.WriteCodesXML.java

License:Open Source License

public static void writeCode(Code code, String xmlSource) {
    try {/*w w  w .j a  va  2s  .c om*/
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        Element codigo = new Element("codigo", ns);
        codigo.setAttribute("id", code.getID());

        for (Element e : rootNode.getChildren("codigo", ns))
            if (e.getAttributeValue("id").equals(code.getID())) {
                rootNode.removeContent(e);
                break;
            }

        Element idProf = new Element("idProfesor", ns);
        idProf.setText(code.getIDProfesor());
        Element nombre = new Element("nombre", ns);
        nombre.setText(code.getNombre());
        Element lenguaje = new Element("lenguaje", ns);
        lenguaje.setText(code.getLenguaje());
        Element resaltar = new Element("resaltar", ns);
        resaltar.setText(code.getResaltar());
        List<Element> lineas = new ArrayList<Element>();
        for (String l : code.getLineas()) {
            Element linea = new Element("linea", ns);
            linea.setText(l);
            lineas.add(linea);
        }
        List<Element> comentarios = new ArrayList<Element>();
        if (code.getIDComentarios().size() > 0) {
            for (String c : code.getIDComentarios()) {
                Element comentario = new Element("idComentario", ns);
                comentario.setText(c);
                comentarios.add(comentario);
            }
        }

        codigo.addContent(idProf);
        codigo.addContent(nombre);
        codigo.addContent(lenguaje);
        codigo.addContent(resaltar);
        codigo.addContent(lineas);
        if (comentarios.size() > 0)
            codigo.addContent(comentarios);

        rootNode.addContent(codigo);

        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());
    }
}

From source file:XMLWriter.WriteCodesXML.java

License:Open Source License

public static boolean deleteCode(Code code, String xmlSource) {
    try {/*  www. j a va  2 s.  c  om*/
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        boolean find = false;

        for (Element e : rootNode.getChildren("codigo", ns))
            if (e.getAttributeValue("id").equals(code.getID())) {
                find = rootNode.removeContent(e);
                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.WriteCommentsXML.java

License:Open Source License

public static boolean removeComments(List<String> idComments, String xmlSource) {
    if (idComments.isEmpty())
        return true;

    try {/*from   www .j  a v  a2s .co  m*/
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        List<Element> lista = rootNode.getChildren("comentario", ns);
        List<Element> rem = new ArrayList<Element>();
        boolean find = false, error = false;

        for (Element e : lista) {
            if (idComments.contains(e.getAttributeValue("id"))) {
                find = true;
                rem.add(e);
            }
        }
        for (Element e : rem) {
            error = (error || !rootNode.removeContent(e));
        }
        if (!find || error)
            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.WriteCommentsXML.java

License:Open Source License

public static boolean removeComments(String idUser, List<String> idComments, String xmlSource) {
    try {//from w w w.  j  a v a2  s. c  o  m
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        List<Element> lista = rootNode.getChildren("comentario", ns);
        boolean error = false;

        for (Element e : lista)
            if (e.getChildTextTrim("idUsuario", ns).equals(idUser)
                    && idComments.contains(e.getAttributeValue("id")))
                error = (error || !rootNode.removeContent(e));
        if (error)
            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.WriteCoursesXML.java

License:Open Source License

public static boolean deleteCourse(String idCurso, String xmlSource) {
    try {/*from   w w  w.j  a va2s . co  m*/
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        boolean find = false;

        for (Element e : rootNode.getChildren("curso", ns))
            if (e.getAttributeValue("id").equals(idCurso)) {
                find = rootNode.removeContent(e);
                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.WriteCoursesXML.java

License:Open Source License

public static boolean removeCode(String idCourse, String idCode, String xmlSource) {
    try {//ww w . j a v a  2  s.c  om
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        List<Element> lista = rootNode.getChildren("curso", ns);
        boolean find = false;

        for (Element e : lista)
            if (e.getAttributeValue("id").equals(idCourse)) {
                for (Element c : e.getChildren("idCodigo", ns))
                    if (c.getTextTrim().equals(idCode)) {
                        find = e.removeContent(c);
                        break;
                    }
                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.WriteCoursesXML.java

License:Open Source License

public static boolean removeUser(String idCourse, String idUser, String xmlSource) {
    try {//  w ww  .j  a  v  a2s .  c o m
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        List<Element> lista = rootNode.getChildren("curso", ns);
        boolean find = false;

        for (Element e : lista)
            if (e.getAttributeValue("id").equals(idCourse)) {
                for (Element c : e.getChildren("idAlumno", ns))
                    if (c.getTextTrim().equals(idUser)) {
                        find = e.removeContent(c);
                        break;
                    }
                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 {/*from   w w w  .ja va  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;
}