4 import java.io.FileWriter;
5 import java.io.IOException;
7 import org.jdom2.Document;
8 import org.jdom2.Element;
9 import org.jdom2.JDOMException;
10 import org.jdom2.input.SAXBuilder;
11 import org.jdom2.output.Format;
12 import org.jdom2.output.XMLOutputter;
15 public class xmlOperation {
18 static private String pathName=global.listaEsercizi;
20 public void create() {
24 Element company = new Element("esercizi");
25 Document doc = new Document(company);
26 doc.setRootElement(company);
28 // new XMLOutputter().output(doc, System.out);
29 XMLOutputter xmlOutput = new XMLOutputter();
32 xmlOutput.setFormat(Format.getPrettyFormat());
33 xmlOutput.output(doc, new FileWriter(pathName));
35 System.out.println("File Saved!");
36 } catch (IOException io) {
37 System.out.println(io.getMessage());
43 public void add(String name)
47 SAXBuilder builder = new SAXBuilder();
48 File xmlFile = new File(pathName);
50 Document doc = (Document) builder.build(xmlFile);
51 Element rootNode = doc.getRootElement();
54 // add new age element
55 Element newEs = new Element("esercizio").setText(name).setAttribute("md5",MD5.digest(name));
56 rootNode.addContent(newEs);
58 XMLOutputter xmlOutput = new XMLOutputter();
61 xmlOutput.setFormat(Format.getPrettyFormat());
62 xmlOutput.output(doc, new FileWriter(pathName));
64 // xmlOutput.output(doc, System.out);
66 System.out.println("File updated!");
67 } catch (IOException io) {
69 } catch (JDOMException e) {
74 public String getMD5(String name)
78 SAXBuilder builder = new SAXBuilder();
79 File xmlFile = new File(pathName);
81 Document doc = (Document) builder.build(xmlFile);
82 Element rootNode = doc.getRootElement();
84 for(Element esercizio:rootNode.getChildren())
86 if(esercizio.getText().contentEquals(name))
88 return esercizio.getAttribute("md5").toString();
91 } catch (IOException io) {
93 } catch (JDOMException e) {
99 public void remove(String name)
103 SAXBuilder builder = new SAXBuilder();
104 File xmlFile = new File(pathName);
106 Document doc = (Document) builder.build(xmlFile);
107 Element rootNode = doc.getRootElement();
109 for(Element esercizio : rootNode.getChildren())
111 if(esercizio.getText().contentEquals(name))
118 rootNode.removeContent(tmp);
119 XMLOutputter xmlOutput = new XMLOutputter();
122 xmlOutput.setFormat(Format.getPrettyFormat());
123 xmlOutput.output(doc, new FileWriter(pathName));
125 // xmlOutput.output(doc, System.out);
127 System.out.println("File updated!");
128 } catch (IOException io) {
129 io.printStackTrace();
130 } catch (JDOMException e) {