ArrayList中没有添加XML元素

时间:2012-10-16 07:19:43

标签: java android xml-parsing

我需要解析内联元素可用的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>

我能够显示所有值,但是当我尝试将它们添加到数组列表时,它不会添加它们。下面是我解析XML的代码 SAX Parser中的代码:

// 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;
        }
    }

1 个答案:

答案 0 :(得分:1)

如果您获得numberformatexception,则将纬度和经度更新为double并将颜色更改为String

class XMLParsingSAX extends DefaultHandler {


    public static void main(String[] args) {
        new XMLParsingSAX().parseDocument();
    }

    private void parseDocument() {

        // get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {

            // get a new instance of parser
            SAXParser sp = spf.newSAXParser();

            // parse the file and also register this class for call backs
            sp.parse("NewFile.xml", this);

        } catch (SAXException se) {
            se.printStackTrace();
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (IOException ie) {
            ie.printStackTrace();
        }

        System.out.println(states.toString());
        System.out.println(states.get(0).getPoints().toString());
        System.out.println(states.get(1).getPoints().toString());
    }

    Points tempPoint;
    String tempVal1;
    String tempVal2;

    States tempState;

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // reset
        tempVal1 = "";
        tempVal2 = "";
        System.out.println("localName: " + localName + " qName: " + qName
                + " name: " + attributes.getValue(0));
        if (qName.equalsIgnoreCase("state")) {
            // create a new instance of state
            tempState = new States();
            points = new ArrayList<Points>();
            tempState.setStateName(attributes.getValue(0));
            tempState.setColor(Integer.parseInt(attributes.getValue(1)));
            tempVal1 = attributes.getValue(0);
            tempVal2 = attributes.getValue(1);
            System.out.println("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);
            System.out.println("localName: " + localName + " qName: "
                    + qName + " lat: " + attributes.getValue(0) + " lng: "
                    + attributes.getValue(1));
        }

    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        System.out.println("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        
        System.out.println("" + states.size());
        System.out.println("" + points.size());

    }


       ArrayList<States> states = new ArrayList<States>();
       ArrayList<Points> points = new ArrayList<Points>();

}


 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;
    }

    @Override
    public String toString() {
        return "States [stateName=" + stateName + ", color=" + color + "]";
    }


}


 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;
    }

    @Override
    public String toString() {
        return "Points [latitude=" + latitude + ", longitude=" + longitude
                + "]";
    }


    }