Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8685703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:38:35+00:00 2026-06-12T22:38:35+00:00

I need to parse an XML where inline elements are available. For eg: <states>

  • 0

I need to parse an XML where inline elements are available. For eg:

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

I am able to display all values, but when I am trying to add them to array list then it is not adding them. Below is the code where I am parsing the XML
Code in 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());

    }

Nothing is getting added in the arrayList
Here is the sample code where setter and getter methods are implemented:
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;
}

}

Please help me how can I add these elements in the ArrayList.

Updated Code:

    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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:38:35+00:00Added an answer on June 12, 2026 at 10:38 pm

    if you are getting numberformatexception, then update latitude and longitude to double and color to 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
                    + "]";
        }
    
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse some XML containing inline elements. The XML look, for example,
I need to parse a continuous stream of well-formed XML elements, to which I
I need to parse a XML String so I can use some of the
I don't need to parse the XML, or to decode it into an ActionScript
i need to parse this php xml response in android: <?phpxml version=1.0 encoding=utf-8?> <SmsResponse>
I need to parse a bunch of incoming xml documents, they all have the
I need to parse a value from an xml and place it to image's
I have an XML file which I need to parse using PHP and send
Given the XML below, I need to parse it and output names of all
I need to parse XML with JAXB which describes structure. E.g. it can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.