0

在这个 sax 解析代码中,我得到了加载数据的屏幕,但 xml 文件没有被解析.....任何人都可以尽快帮助这个,因为我必须提交 thos 应用程序......

我的代码..

  public class Xmlfile extends Activity {
// For Single Data
private List<String> item = new ArrayList<String>();
public ArrayList<StudentDetails> data = new ArrayList<StudentDetails>();

Xmlfile x;


private boolean in_name;
private boolean in_formatted_address;
private boolean in_lat;
private boolean in_lng;
StudentDetails d;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

    x = this;
    new StudentTask().execute();
}

public class StudentTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = ProgressDialog.show(x, "Please Wait", "Loading Data");

        pd.setCancelable(true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {

            URL u = new URL(
                    "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Vijayawada&sensor=true&key=AIzaSyC-bRTW12s9l3iZ57wPZGrH8kRMpQPeF5s");
            InputSource i = new InputSource(u.openStream());

            SAXParserFactory sx = SAXParserFactory.newInstance();
            SAXParser msx = sx.newSAXParser();

            XMLReader reader = msx.getXMLReader();

            MyXmlHandler myRSSHandler = new MyXmlHandler();

            reader.setContentHandler(myRSSHandler);
            // Reading xml file from raw folder.
            // InputStream istream = null;
            // istream =
            // getApplicationContext().getResources().openRawResource(R.raw.studentdetails);

            // InputSource myInputSource = new
            // InputSource(rssUrl.openStream());

            // myXMLReader.parse(myInputSource);
            reader.parse(new InputSource(u.openStream()));

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);



        // setListAdapter(new
        // ArrayAdapter<String>(x,android.R.layout.simple_list_item_1,
        // item));
        //          StudentDetails d = new StudentDetails();
               //           data.add(d);
        ListView l=(ListView)findViewById(R.id.list);
         Demo demo=new Demo();
        l.setAdapter(demo);
        pd.dismiss();
    }

}

// 1)Implementing Handler to get data from xml file
public class MyXmlHandler extends DefaultHandler {
    private boolean in_name = false;
    private boolean in_formatted_address = false;
    private boolean in_lat = false;
    private boolean in_lng = false;

    // private boolean in_clas = false;
    // private boolean in_phno = false;
    // private boolean in_email = false;

    String name;
    String formatted_address;
    public String lat, lng;

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
    }

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

        if (localName.equals("name")) {
            d=new StudentDetails();

            this.in_name = true;
        } else if (localName.equals("formatted_address")) {

            this.in_formatted_address = true;

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

            this.in_lat = true;
        }

        else if (localName.equals("lng")) {

            this.in_lng = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (this.in_name) {

            name = new String(ch, start, length);
            d.setName(name);
            //Log.v("name========", name + item.size());

        }

        else if (this.in_formatted_address) {

            formatted_address = new String(ch, start, length);
            d.setFormatted_address(formatted_address);

        } else if (this.in_lat) {

            lat = new String(ch, start, length);

            d.setLat(lat);

        }

        else if (this.in_lng) {

            lng = new String(ch, start, length);
            d.setLng(lng);


        }

    }


public void endElement(String uri, String localName, String qName)
        throws SAXException {
    // TODO Auto-generated method stub
    if (localName.equals("name")) {


        this.in_name = false;

    } else if (localName.equals("formatted_address")) {
        this.in_formatted_address = false;

    }

    else if (localName.equals("lat")) {
        this.in_lat = false;

    } else if (localName.equals("lng")) {
        this.in_lng = false;
        data.add(d);
    }

}
}
public class Demo extends BaseAdapter

{
    Activity context;
    private List<String> s;

    public Demo() {
        super();
        this.context = context;
        this.s = s;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        // return LISTITEMS.size();
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;


        if (v == null) {
            LayoutInflater vi = LayoutInflater.from(parent.getContext());
            v = vi.inflate(R.layout.activitymain, null);
        }

        final StudentDetails studentdetails=data.get(position);
        // ImageView img=(ImageView)v.findViewById(R.id.img);
        TextView name = (TextView) v.findViewById(R.id.name);
        TextView formatted_address = (TextView) v
                .findViewById(R.id.formatted_address);
        TextView lat = (TextView) v.findViewById(R.id.lat);
        TextView lng = (TextView) v.findViewById(R.id.lng);

        // c.moveToPosition(position);

        // img.setImageResource(Integer.parseInt(c.getString(0)));
        name.setText(data.get(position).getName());
        formatted_address
                .setText(data.get(position).getFormatted_address());
        lat.setText(data.get(position).getLat());
        lng.setText(data.get(position).getLng());

        return v;
    }

}
      }

吸气剂和吸气剂....

   public String name,formatted_address,lat,lng;


public StudentDetails(String n,String f,String la,String ln)
{
    this.name=n;
    this.formatted_address=f;
    this.lat=la;
    this.lng=ln;

}
  public StudentDetails(){

    }

public String getName() {
    return name;
}
public void setName(String name) {
       this.name = name;
   }

public String getFormatted_address() {
    return formatted_address;
}

public void setFormatted_address(String Formatted_address) {
       this.formatted_address = formatted_address;
   }
public String getLat() {
    return lat;
}
public void setLat(String Lat) {
       this.lat = lat;
   }

public String getLng() {
    return lng;
}


public void setLng(String Lng) {
       this.lng = lng;
   }

       }
4

1 回答 1

0

尝试将您的方法更改为characters(char[] ch, int start, int length)

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (this.in_name) {
            name += new String(ch, start, length);

            /* What does list 'item' actually do in your code? */
            item.add(name);
            Log.v("name========", name + item.size());

        } else if (this.in_formatted_address) {
            formatted_address += new String(ch, start, length);
            item.add(formatted_address);

        } else if (this.in_lat) {
            lat += new String(ch, start, length);
            item.add(lat);

        } else if (this.in_lng) {

            lng += new String(ch, start, length);
            item.add(lng);
        }
    }

以及为什么在endElement(String uri, String localName, String qName)方法中将所有字段设置为 null 之前将它们分配给 StudentDetails 类,我想应该是:

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

        if (localName.equals("name")) {
            StudentDetails s = new StudentDetails(name,formatted_address, lat, lng);    
            data.add(s);

            String lng = "";
            String name = "";
            String formatted_address = "";
            String lat = "";                      
            this.in_name = false;

        } else if (localName.equals("formatted_address")) {
            this.in_formatted_address = false;
        } else if (localName.equals("lat")) {
            this.in_lat = false;
        } else if (localName.equals("lng")) {
            this.in_lng = false;
        }
    }
于 2013-02-01T11:26:56.713 回答