开发者

Show null value in the emmulator after Parsing the tag through sax parser in android?

开发者 https://www.devze.com 2023-03-24 00:22 出处:网络
I am making an small application in android of webservices through Sax parser my link is (http://www.anddev.org/images/tut/basic/parsingxml/example.xml)I am able to display the value of ****<**inne

I am making an small application in android of webservices through Sax parser my link is (http://www.anddev.org/images/tut/basic/parsingxml/example.xml)I am able to display the value of ****<**innertag>,******But can't able to display the value of in theTextView

Here is my sorcs code

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;





public class ExampleHandler extends DefaultHandler{



        // ===========================================================

        // Fields

        // ===========================================================



        private boolean in_outertag = false;

        private boolean in_innertag = false;

        private boolean in_mytag = false;



        private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();



        // ===========================================================

        // Getter & Setter

        // ===========================================================



        public ParsedExampleDataSet getParsedData() {

                return this.myParsedExampleDataSet;

        }



        // ===========================================================

        // Methods

        // ===========================================================

        @Override

        public void startDocument() throws SAXException {

                this.myParsedExampleDataSet = new ParsedExampleDataSet();

        }



        @Override

        public void endDocument() throws SAXException {

                // Nothing to do

        }



        /** Gets be called on opening tags like:

         * <tag>

         * Can provide attribute(s), when xml was like:

         * <tag attribute="attributeValue">*/

       @Override

        public void startElement(String namespaceURI, String localName,

                        String qName, Attributes atts) throws SAXException {

                if (localName.equals("outertag")) {

                        this.in_outertag = true;

                }else if (localName.equals("innertag")) {

                        //this.in_innertag = true;
                    String attrValue = atts.getValue("sampleattribute");
                     myParsedExampleDataSet.setExtractedString(attrValue);

                }else if (localName.equals("mytag")) {

                        //this.in_mytag = true;
                    String attrValue = atts.getValue("mytag");
                 myParsedExampleDataSet.setE开发者_开发百科xtractedString1(attrValue);

                }else if (localName.equals("tagwithnumber")) {

                        // Extract an Attribute

                        String attrValue = atts.getValue("thenumber");

                        int i = Integer.parseInt(attrValue);

                        myParsedExampleDataSet.setExtractedInt(i);

                }

        }



        /** Gets be called on closing tags like:

        * </tag> */

        @Override

        public void endElement(String namespaceURI, String localName, String qName)

                        throws SAXException {

                if (localName.equals("outertag")) {

                        this.in_outertag = false;

                }else if (localName.equals("innertag")) {

                       // this.in_innertag = false;

                }else if (localName.equals("mytag")) {

                        //this.in_mytag = false;

                }else if (localName.equals("tagwithnumber")) {

                        // Nothing to do here

                }

        }



        /** Gets be called on the following structure:

         * <tag>characters</tag> */

        @Override

    public void characters(char ch[], int start, int length) {

                if(this.in_mytag){

               myParsedExampleDataSet.setExtractedString1(new String(ch, start, length));

        }

    }

}![enter image description here][1]

Show null value in the emmulator after Parsing the tag through sax parser in android?


You have this.in_mytag = true commented out in startElement(). Therefore the code block in the characters() function that sets ExtractedString1 isn't executing because in_mytag is false.

One other thing when handling the start of mytag: String attrValue = atts.getValue("mytag"); is unnecessary. It should be handled in the characters() function (I suspect you simply had it for debugging purposes).

0

精彩评论

暂无评论...
验证码 换一张
取 消