public class LevelHandler extends DefaultHandler{
// ===========================================================
// Fields
// ===========================================================
private int mIndex = 0;
private boolean in_element = false;
private boolean in_id = false;
private boolean in_name = false;
private boolean in_coodrX = false;
private boolean in_coordY = false;
private boolean in_cellX = false;
private boolean in_cellY = false;
private ArrayList<ParsedObjectDataSet> myParsedObjectDataList;
// ===========================================================
// Getter & Setter
// ===========================================================
public ArrayList<ParsedObjectDataSet> getParsedData() {
return this.myParsedObjectDataList;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
myParsedObjectDataList = new ArrayList<ParsedObjectDataSet>();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("ELEMENT")){
myParsedObjectDataList.add(new ParsedObjectDataSet());
} else
if (localName.equals("ID")){
in_id = true;
} else
if (localName.equals("NAME")){
in_name = true;
} else
if (localName.equals("COORD_X")){
in_coodrX = true;
} else
if (localName.equals("COORD_Y")){
in_coordY = true;
} else
if (localName.equals("CELLS_X")){
in_cellX = true;
} else
if (localName.equals("CELLS_Y")){
in_cellY = true;
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("ELEMENT")){
myParsedObjectDataList.add(new ParsedObjectDataSet());
mIndex++;
} else
if (localName.equals("ID")){
in_id = false;
} else
if (localName.equals("NAME")){
in_name = false;
} else
if (localName.equals("COORD_X")){
in_coodrX = false;
} else
if (localName.equals("COORD_Y")){
in_coordY = false;
} else
if (localName.equals("CELLS_X")){
in_cellX = false;
} else
if (localName.equals("CELLS_Y")){
in_cellY = false;
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
String textBetween = new String(ch,start,length);
if (in_id) {
myParsedObjectDataList.get(mIndex).setID(Integer.parseInt(textBetween));
} else
if (in_name) {
myParsedObjectDataList.get(mIndex).setName(textBetween);
} else
if (in_coodrX) {
myParsedObjectDataList.get(mIndex).setCoord_X(Integer.parseInt(textBetween));
} else
if (in_coordY) {
myParsedObjectDataList.get(mIndex).setCoord_Y(Integer.parseInt(textBetween));
} else
if (in_cellX) {
myParsedObjectDataList.get(mIndex).setCell_X(Integer.parseInt(textBetween));
} else
if (in_cellY) {
myParsedObjectDataList.get(mIndex).setCell_Y(Integer.parseInt(textBetween));
}
}
}