SAX question
hi all,
i have an xm document with the data of a cd.
this is the file:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE cd SYSTEM "cd.dtd" >
<cd>
<artist>U2</artist>
<title>How To Dismantle An Atomic Bomb</title>
<genre>Rock</genre>
<song>
<title>Vertigo</title>
<length>3:13</length>
</song>
<song>
<title>Miracle Drug</title>
<length>3:54</length>
</song>
<song>
<title>Sometimes You Can't Make It On Your Own</title>
<length>5:05</length>
</song>
<song>
<title>Love And Peace Or Else</title>
<length>4:48</length>
</song>
<song>
<title>City Of Blinding Lights</title>
<length>5:46</length>
</song>
<song>
<title>All Because Of You</title>
<length>3:34</length>
</song>
<song>
<title>A Man And A Woman</title>
<length>4:27</length>
</song>
<song>
<title>Crumbs From Your Table</title>
<length>4:59</length>
</song>
<song>
<title>One Step Closer</title>
<length>3:47</length>
</song>
<song>
<title>Original Of The Species</title>
<length>4:34</length>
</song>
<song>
<title>Yahweh</title>
<length>4:22</length>
</song>
</cd>
now i must write a program that prints the songs that are longer than 3:30.
my code partly works, it prints the lenghts that are loner than 3:30.
but i don't know how to print the title with it.
here is the code:
public class TryOut563 extends SAXException {
public void init(String uri) throws SAXException, IOException {
XMLReader reader = XMLReaderFactory.createXMLReader();
PrintHandler ph = new PrintHandler();
reader.setContentHandler(ph);
InputSource inputSource = new InputSource(uri);
reader.parse(inputSource);
}
public static void main(String[] args) throws SAXException, IOException {
TryOut563 app = new TryOut563();
app.init("cd.xml");
}
}
class PrintHandler extends DefaultHandler {
private StringBuffer buffer;
private String string;
public void characters(char[] ch, int start, int length) {
if (buffer != null) {
buffer.append(ch, start, length);
}
}
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (localName.equals("song")) {
if (localName.equals("title")) {
buffer = new StringBuffer();
}
//buffer = new StringBuffer();
}
}
public void endElement(String uri, String localName, String qName) {
/*if (localName.equals("length")) {
string = buffer.toString();
//testen of length > 3:30
int deel1 = Integer.parseInt(string.substring(0,1));
//System.out.println(deel1);
int deel2 = Integer.parseInt(string.substring(2,4));
//System.out.println(deel2);
if (deel1 >= 3 && deel2 >= 30) {
System.out.println(string);
}
}*/
if (localName.equals("song")) {
if (localName.equals("title")) {
string = buffer.toString();
System.out.println(string);
}
//string = buffer.toString();
//System.out.println(string);
}
}
}
i really hope somebody can help me with this.
grts

