0

我需要解析一个内联元素可用的 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 Parser 中解析 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;
    }

}

点数.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;
        }
    }
4

1 回答 1

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
                + "]";
    }


    }
于 2012-10-16T07:30:40.760 回答