简体   繁体   English

我的xml解析器(SAX解析器)仅解析URI源。 如何使用SAXparser解析项目中存在的文件?

[英]my xml parser(SAX parser) parses only URI source. How can I parse a file which exists in my project with SAXparser?

This is the code which can only parse URI sources. 这是只能解析URI源的代码。

    package com.example.xmlparserdeneme;

import java.io.IOException;
import java.util.ArrayList;

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

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

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

    ArrayList<String> titles = new ArrayList<String>();
    Context ctx = this;
    CustomAdapter adapter = null;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ctx = this;
        new parseXML().execute();

        Log.i("info2", titles.toString());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class parseXML extends AsyncTask<String, Void, String> {

        MySaxHandler handler;

        @Override
        protected String doInBackground(String... params) {

            // ((Activity) ctx).setContentView(R.layout.activity_main);

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = null;
            try {
                parser = spf.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }

            handler = new MySaxHandler();

            try {

                parser.parse("http://sosyalmedya.co/mobile-feed/", handler);
                // parser.parse("file:///android_asset/DirenisDB.txt", handler);
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Log.i("info", titles.toString());

            // Toast.makeText(ctx, "POST", Toast.LENGTH_LONG).show();
            // setAdapter();

            return "a";

        }

        @Override
        protected void onPostExecute(String result) {

            lv = (ListView) findViewById(R.id.listView);
            lv.setAdapter(new CustomAdapter(titles, ctx));
        }

        public ArrayList<String> getArray() {
            return titles;
        }

        public class MySaxHandler extends DefaultHandler {

            StringBuffer chars;

            public void startElement(String uri, String localName,
                    String qName, Attributes atts) {
                chars = new StringBuffer();

            }

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

                if (localName.equalsIgnoreCase("key"))
                    titles.add(chars.toString());

            }

            public void characters(char ch[], int start, int length) {
                chars.append(new String(ch, start, length));
            }

        }

    }
}

I've spent almost 6 hours on it but I still couldn't figure it out. 我已经花了将近6个小时,但还是不知道。 I just want it to parse this(a file) parser.parse("file:///android_asset/DirenisDB.txt", handler); 我只希望它解析这个(文件) parser.parse("file:///android_asset/DirenisDB.txt", handler); instead of this(URI) parser.parse("http://sosyalmedya.co/mobile-feed/", handler); 而不是此(URI) parser.parse("http://sosyalmedya.co/mobile-feed/", handler); .

I'd be appreciated if you can solve my problem. 如果您能解决我的问题,我将不胜感激。

the json code which is going to be parsed must be in the layout/res/Raw folder. 要解析的json代码必须位于layout / res / Raw文件夹中。 and this is the code to open the resource. 这是打开资源的代码。

db = parse(getResources().openRawResource(R.raw.database));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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