List of usage examples for org.jdom2.output Format getPrettyFormat
public static Format getPrettyFormat()
From source file:XMLWriter.WriteCommentsXML.java
License:Open Source License
public static boolean rateComment(String id, Integer calif, String xmlSource) { try {//w w w. j a v a2 s . c om Document doc = builder.build(xmlSource); Element rootNode = doc.getRootElement(); List<Element> lista = rootNode.getChildren("comentario", ns); boolean find = false; for (Element e : lista) { if (e.getAttributeValue("id").equals(id)) { find = true; e.getChild("calificacion", ns).setText(calif.toString()); 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 {/* w w w . j a va 2 s . 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 ww . java 2 s . c om 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 void addCourse(Course curso, String xmlSource) { try {//from w w w. j av a 2 s . co m Document doc = builder.build(xmlSource); Element rootNode = doc.getRootElement(); Element c = new Element("curso", ns); c.setAttribute("id", curso.getID()); Element idProfesor = new Element("idProfesor", ns); idProfesor.setText(curso.getIDProfesor()); Element nombre = new Element("nombre", ns); nombre.setText(curso.getNombre()); c.addContent(idProfesor); c.addContent(nombre); rootNode.addContent(c); 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.WriteCoursesXML.java
License:Open Source License
public static boolean deleteCourse(String idCurso, String xmlSource) { try {//from w w w. j a va2 s .c o 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 addCode(String idCourse, String idCode, String xmlSource) { try {/*from w ww .j a va 2 s. 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)) { find = true; Element idCo = new Element("idCodigo", ns); idCo.setText(idCode); e.addContent(e.indexOf(e.getChild("nombre", ns)), idCo); 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 {/* w ww. j a v a2 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 addUser(String idCourse, String idUser, String xmlSource) { try {//from ww w . j a v a 2 s . 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)) { find = true; Element idUs = new Element("idAlumno", ns); idUs.setText(idUser); e.addContent(2, idUs); 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 {//from w ww. j av 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("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 addUser(User us, String xmlSource) { try {// w w w . j a v a 2 s . c o m Document doc = builder.build(xmlSource); Element rootNode = doc.getRootElement(); Element user = new Element("user", ns); user.setAttribute("id", us.getID()); for (Element e : rootNode.getChildren("user", ns)) if (e.getAttributeValue("id").equals(us.getID())) return false; Element usuario = new Element("usuario", ns); usuario.setText(us.getUsuario()); Element password = new Element("password", ns); password.setText(us.getPassword()); Element nombres = new Element("nombres", ns); nombres.setText(us.getNombres()); Element apPat = new Element("apPat", ns); apPat.setText(us.getApPaterno()); Element apMat = new Element("apMat", ns); apMat.setText(us.getApMaterno()); Element category = new Element("category", ns); category.setText(us.getCategory()); List<Element> idCursos = new ArrayList<Element>(); for (String id : us.getIDCursos()) { Element idCurso = new Element("idCurso", ns); idCurso.setText(id); idCursos.add(idCurso); } user.addContent(usuario); user.addContent(password); user.addContent(nombres); user.addContent(apPat); user.addContent(apMat); user.addContent(category); user.addContent(idCursos); rootNode.addContent(user); 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; }