インライン要素が利用可能なXMLを解析する必要があります。例:
<states>
<state name ="Alaska" colour="#ff0000" >
<point lat="70.0187" lng="-141.0205"/>
<point lat="70.1292" lng="-141.7291"/>
<point lat="70.4515" lng="-144.8163"/>
<point lat="70.7471" lng="-148.4583"/>
<point lat="70.7923" lng="-151.1609"/>
</state>
<state name ="Alabama" colour="#ff0000" >
<point lat="35.0041" lng="-88.1955"/>
<point lat="34.9918" lng="-85.6068"/>
<point lat="32.8404" lng="-85.1756"/>
<point lat="32.2593" lng="-84.8927"/>
</state>
</states>
すべての値を表示できますが、それらを配列リストに追加しようとすると、追加されません。以下は、SAXパーサーでXMLコードを解析しているコードです。
// Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
tempVal1 = "";
tempVal2 = "";
Log.e("SAX Details", "localName: " + localName + " qName: " + qName
+ " name: " + attributes.getValue(0));
if (qName.equalsIgnoreCase("state")) {
// create a new instance of state
tempState = new States();
tempVal1 = attributes.getValue(0);
tempVal2 = attributes.getValue(1);
Log.e("SAX StateDetails", "localName: " + localName + " qName: "
+ qName + " name: " + attributes.getValue(0) + " color: "
+ attributes.getValue(1));
} else if (qName.equalsIgnoreCase("point")) {
// create a new instance of point
tempPoint = new Points();
tempVal1 = attributes.getValue(0);
tempVal2 = attributes.getValue(1);
Log.e("SAX pointDetails", "localName: " + localName + " qName: "
+ qName + " lat: " + attributes.getValue(0) + " lng: "
+ attributes.getValue(1));
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
Log.e("SAX Details", "localName: " + localName + " qName" + qName);
if (qName.equalsIgnoreCase("state")) {
// add it to the list
tempState.setStateName(tempVal1); // getting Exception
tempState.setColor(Integer.parseInt(tempVal2));// getting Exception
states.add(tempState);
} else if (qName.equalsIgnoreCase("point")) {
// add it to the list
tempPoint.setLatitude(Long.parseLong(tempVal1));// getting Exception
tempPoint.setLongitude(Long.parseLong(tempVal2));// getting Exception
points.add(tempPoint);
}
tempState.setPoints(points);// getting Exception
Log.e("States", "" + states.size());
Log.e("points", "" + points.size());
}
arrayListには何も追加されていません。setterメソッドとgetterメソッドが実装されているサンプルコードは次のとおりです。States.java
public class States {
private String stateName;
private int color;
private ArrayList<Points> points;
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public ArrayList<Points> getPoints() {
return points;
}
public void setPoints(ArrayList<Points> points) {
this.points = points;
}
}
Points.java
public class Points {
private long latitude;
private long longitude;
public long getLatitude() {
return latitude;
}
public void setLatitude(long latitude) {
this.latitude = latitude;
}
public long getLongitude() {
return longitude;
}
public void setLongitude(long longitude) {
this.longitude = longitude;
}
public String getPointsDetails() {
String result = latitude + ": " + longitude;
return result;
}
}
これらの要素をArrayListに追加する方法を教えてください。
更新されたコード:
public class SAXXMLHandler extends DefaultHandler {
public static ArrayList<States> states;
public static ArrayList<Points> points;
public String tempVal1;
public String tempVal2;
public String tempVal3;
public String tempVal4;
public States tempState;
public Points tempPoint;
public SAXXMLHandler() {
states = new ArrayList<States>();
points = new ArrayList<Points>();
}
// Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
tempVal1 = "";
tempVal2 = "";
tempVal3 = "";
tempVal4 = "";
tempState = new States();
tempPoint = new Points();
if (qName.equalsIgnoreCase("state")) {
// create a new instance of state
tempVal1 = attributes.getValue("name");
tempVal2 = attributes.getValue("colour");
tempState.setStateName(tempVal1);
tempState.setColor(tempVal2);
states.add(tempState);
} else if (qName.equalsIgnoreCase("point")) {
// create a new instance of point
tempVal3 = attributes.getValue("lat");
tempVal4 = attributes.getValue("lng");
tempPoint.setLatitude(Double.parseDouble(tempVal3));
tempPoint.setLongitude(Double.parseDouble(tempVal4));
points.add(tempPoint);
}
}
static int statesSize = 0;
public void endElement(String uri, String localName, String qName)
throws SAXException {
Log.e("NAmes", "" + localName + " : " + qName);
if (qName.equalsIgnoreCase("state")) {
// add it to the list
statesSize = states.size();
if (statesSize == states.size()) {
tempState.setPoints(points);
}
//here the size of points is displayed w.r.t states i.e., 5 and 4
Log.e("Points size", "" + tempState.getPoints().size());
} else if (qName.equalsIgnoreCase("point")) {
// add it to the list
//here the size of points is displayed as zero.
Log.e("Points size", "" + tempState.getPoints().size());
}
if (statesSize == states.size()) {
points.clear();
}
}
public ArrayList<States> getStates() {
return states;
}
}