简体   繁体   中英

Parsing XML data Using Sax Parser

I am working in android and need help parsing the below XML Data and show it on the listview. I am using Sax Parser to parse data

<NotifyList>
<Notify><Name>Raj</Name><Age>12</Age><Status>1</Status></Notify>

<Notify><Name>G</Name><Age>11</Age><Status>2</Status></Notify>

<Notify><Name>D</Name><Age>21</Age><Status>1</Status><Mobile>23232324</Mobile></Notify>

<Notify><Name>erwer</Name><Age>3</Age><Status>2</Status><Mobile>235534</Mobile></Notify>

<NotifyList>

In the above xml data you can find the 1st and 2nd row is missing with Mobile tag. I have to check whether the tag exist or not if the tag dosn't exists i need to append it to empty value in the listview like below.

Name1 Age >

Name2 Age >

Name3 Age Ph No >

Name4 Age Ph No >

Thanxs, Goutham

 ArrayList<String> parsedtxt =new ArrayList<String>();


           try {   
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();

                DefaultHandler handler = new DefaultHandler() {
                boolean partxt = false;


                @Override
                public void startElement(String uri, String localName,
                String qName, Attributes attributes)
                throws SAXException {               

                if (qName.equalsIgnoreCase("Notify")) {
                    partxt = true;
                }


                }



                @Override
                public void endElement(String uri, String localName,
                String qName) throws SAXException {
                // TODO Auto-generated method stub          
                }

                @Override
                public void characters(char[] ch, int start, int length)
                throws SAXException {
                // TODO Auto-generated method stub

                if (partxt) {
                System.out.println("Product : " + new String(ch, start, length).trim());
                parsedtxt.add(new String(ch, start, length).trim());


                }


                }
                     };
                     asset = this.getAssets();
                     in = asset.open(xmlnam+"det.xml");

                     saxParser.parse(in, handler);

                     } catch (Exception e) {

                     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM