4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.security.NoSuchAlgorithmException;
8 import org.jdom2.Document;
9 import org.jdom2.Element;
10 import org.jdom2.JDOMException;
11 import org.jdom2.input.SAXBuilder;
12 import org.jdom2.output.Format;
13 import org.jdom2.output.XMLOutputter;
16 public class xmlOperation {
19 static private String pathName=global.listaEsercizi;
21 public void create() {
25 Element company = new Element("esercizi");
26 Document doc = new Document(company);
27 doc.setRootElement(company);
29 // new XMLOutputter().output(doc, System.out);
30 XMLOutputter xmlOutput = new XMLOutputter();
33 xmlOutput.setFormat(Format.getPrettyFormat());
34 xmlOutput.output(doc, new FileWriter(pathName));
36 System.out.println("File Saved!");
37 } catch (IOException io) {
38 System.out.println(io.getMessage());
44 public void add(String name)
48 SAXBuilder builder = new SAXBuilder();
49 File xmlFile = new File(pathName);
51 Document doc = (Document) builder.build(xmlFile);
52 Element rootNode = doc.getRootElement();
55 // add new age element
56 Element newEs = new Element("esercizio").setText(name).setAttribute("md5",MD5.digest(name));
57 rootNode.addContent(newEs);
59 XMLOutputter xmlOutput = new XMLOutputter();
62 xmlOutput.setFormat(Format.getPrettyFormat());
63 xmlOutput.output(doc, new FileWriter(pathName));
65 // xmlOutput.output(doc, System.out);
67 System.out.println("File updated!");
68 } catch (IOException io) {
70 } catch (JDOMException e) {
75 public String getMD5(String name)
79 SAXBuilder builder = new SAXBuilder();
80 File xmlFile = new File(pathName);
82 Document doc = (Document) builder.build(xmlFile);
83 Element rootNode = doc.getRootElement();
85 for(Element esercizio:rootNode.getChildren())
87 if(esercizio.getText().contentEquals(name))
89 return esercizio.getAttribute("md5").toString();
92 } catch (IOException io) {
94 } catch (JDOMException e) {
100 public void remove(String name)
104 SAXBuilder builder = new SAXBuilder();
105 File xmlFile = new File(pathName);
107 Document doc = (Document) builder.build(xmlFile);
108 Element rootNode = doc.getRootElement();
110 for(Element esercizio:rootNode.getChildren())
112 if(esercizio.getText().contentEquals(name))
114 System.out.println("trovato");
115 rootNode.removeContent(esercizio);
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) {