I have this assignment where I have to make an xml that has movies names and info. Then i have to make a StAX program that locates the xml file and reads it. The program should also be able to count the number of movies in the decade. Thats the part that i cant code. I am totally blank. Can someone start me off or give me a hint.
my xml is
<!ELEMENT DVDS (DVD*) >
<!ELEMENT DVD (title, performers, release_year, discs, price) >
<!ATTLIST DVD id CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT performers (performer*)>
<!ELEMENT performer (#PCDATA)>
<!ELEMENT release_year (#PCDATA)>
<!ELEMENT discs (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DVDS SYSTEM "dvd.dtd">
<DVDS>
<DVD id="1">
<title>Witness</title>
<performers>
<performer>Harrison Ford</performer>
<performer>Kelly McGillis</performer>
</performers>
<release_year>1980</release_year>//I couldn't find the movie
<discs>1</discs>
<price>7.99</price>
</DVD>
<DVD id="2">
<title>Star Wars Episode V - The Empire Strikes Back</title>
<performers>
<performer>Mark Hamill</performer>
<performer>Harrison Ford</performer>
<performer>Carrie Fisher</performer>
</performers>
<release_year>1980</release_year>
<discs>2</discs>
<price>14.95</price>
<DVD id="3">
<title>Harold and Kumar 3D Christmas</title>
<performers>
</performers>
<release_year>2011</release_year>
<discs>3</discs>
<price>19.99</price>
</DVD>
</DVDS>
and my StAX is
package staxdvdreader;
import javax.xml.stream.*;
import java.io.*;
import java.util.*;
public class StaxDVDReader {
public void read(String filepath) {
try {
FileInputStream fileInputStream = new FileInputStream(filepath);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlStreamReader =
factory.createXMLStreamReader(fileInputStream);
while (xmlStreamReader.hasNext()) {
readDVDList(xmlStreamReader);
}
xmlStreamReader.close();
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void readDVDList(XMLStreamReader reader) throws XMLStreamException {
int eventCode = reader.next();
switch (eventCode) {
case XMLStreamReader.START_ELEMENT:
String key = reader.getLocalName();
if (key.equals("DVD")) {
readDVD(reader);
}
break;
}
}
private void readDVD(XMLStreamReader reader)
throws XMLStreamException {
String name = "";
String value = "";
int nAttributes = reader.getAttributeCount();
String avalue = reader.getAttributeValue(0);
System.out.println("attribute value: " + avalue + " attribute count: " + nAttributes);
while (reader.hasNext()) {
int eventCode = reader.next();
switch (eventCode) {
case XMLStreamReader.START_ELEMENT:
name = reader.getLocalName();
break;
case XMLStreamReader.END_ELEMENT:
name = reader.getLocalName();
if (name.equals("DVD")) return;
break;
case XMLStreamReader.CHARACTERS:
value = reader.getText();
System.out.println("Element name= " + name + " value=" + value);
break;
}
}
return;
}
public static void decadeCount(){//right over here
}
public static void main(String[] args) {
StaxDVDReader dvdReader = new StaxDVDReader();
dvdReader.read("C:\\dvd.xml");
}
}