开发者

I have made a Xml parsing app. But when I am running the source code its showing a blank screen

1st activity

package com.example.parserss;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class Parserss extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);

        try {
            URL url = new URL("http://www.w3schools.com/xml/note.xml");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            xr.parse(new InputSource(url.openStream()));
            MHandler mhandler = new MHandler();
            xr.setContentHandler(mhandler);
            getValue getV = mhandler.getData();
            tv.setText(getV.toString());
        }
        catch(Exception e) {
            System.out.println("Exception Occured :" + e );
        }

       this.setContentView(tv);


    }
}

2nd activity

package com.example.parserss;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MHandler extends DefaultHandler {
    Boolean outerElement = false;
    Boolean innerElement = false;

    private getValue getvalue = new getValue();

    public getValue getData() {
        return getvalue;
    }

     public void startDocument() throws SAXException {
         getvalue = new getValue();
 }

 @Override
 public void endDocument() throws SAXException {
         // Nothing to do
 }




   public void startElement(String uri , String localName , String qName, Attributes attr) throws SAXException {


       if(localName.equals("note")) {
           outerElement = true;

       }
       else if(localName.equals("to")) {
           innerElement = true;

       }
   }
   public void endElement(String uri, String localName, String qnqme) throws SAXException {

       if(localName.equalsIgnoreCase("note")) {
           outerElement = false;
          }
       else if (localName.equals("body")) {
          开发者_如何学C innerElement = false;
       }



   }
   public void characters(char[] ch , int start, int length) throws SAXException {
       if(innerElement){
           getvalue.setStringss(new String(ch, start, length));


       }

   }
}

3rd activity

package com.example.parserss;

public class getValue {
    private String exData = null;
    public String getStringss() {
        return  exData;
    }
    public void setStringss(String exData) {
        this.exData = exData;

    }
    public String toString() {
        return "data = " + exData;
    }
}


I got the solution. Actually I was parsing the stream first and then creating the object of handler class which was wrong. First we have to create the object of handler class and then parse the stream and it worked.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜