-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLWriter.java
84 lines (67 loc) · 2.22 KB
/
XMLWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Maxwell Hull, Dan Cavero, and Claire Li
May 8, 2012
Program for writing to XML.
*/
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class XMLWriter
{
private File file;
private Document doc;
private ArrayList<Song> SongList;
private PlayerFrame frame;
public XMLWriter(PlayerFrame aframe)
{
frame = aframe;
SongList = frame.getMusic().getSongs();
}
public void write() throws JDOMException, IOException
{
String current = System.getProperty("user.dir");
file = new File(current + "/src/musicplayer.xml");
Element Root = new Element("MusicLibrary");
doc = new Document(Root);
for(Song song: SongList)
{
Element Songs = new Element("Song");
Element Path = new Element("Path");
Path.setText(song.getPath());
Songs.addContent(Path);
Element Title = new Element("Title");
Title.setText(song.getTitle());
Songs.addContent(Title);
Element Artist = new Element("Artist");
Artist.setText(song.getArtist());
Songs.addContent(Artist);
Element Album = new Element("Album");
Album.setText(song.getAlbum());
Songs.addContent(Album);
Element Length = new Element("Length");
Length.setText(song.getLength());
Songs.addContent(Length);
Element Year = new Element("Year");
Year.setText(song.getYear());
Songs.addContent(Year);
Root.addContent(Songs);
}
try
{
XMLOutputter serializer = new XMLOutputter();
serializer.output(doc, new FileWriter(file));
serializer.setFormat(Format.getPrettyFormat());
}
catch (IOException e)
{
System.err.println(e);
}
System.exit(0);
}
}