changing URL path to local xml path in android xml parser

吃可爱长大的小学妹 提交于 2019-12-23 05:27:50

问题


hii every one ,,,in the following code snippet am parsing a xml file which in on internet, using url am accessing,,But i need to parse a local xml( say file which is stored in xml folder in the project)please can any one give me the syntax to load the path of local xml in the place of URL,,, (i am brand new to android),,thanx in advance

public class XMLComplaint extends Activity { private int window=0; private String s1,s2,s3,s4,s5,s6; String str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14,sidsize; private ArrayList notedetails = new ArrayList(); ArrayList problemdetails = new ArrayList(); ArrayList totaldetails = new ArrayList();

private ImageButton left,right;
private TextView Gid;
        ListView notelist = null,problemlist=null,totallist=null;
private ArrayAdapter<String> totaladapter = null;
private List<GroupInfo> commonlist ;

private GroupInfo gi ;
MyHandler myhandler=new MyHandler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Gid=(TextView)findViewById(R.id.Gid);
    left=(ImageButton)findViewById(R.id.left);
    right=(ImageButton)findViewById(R.id.right);

    totallist = (ListView) this.findViewById(R.id.Listview1);

    try {
        URL myurl=new URL("http://www.compliantbox.com/docprocessor/sync.php");
        SAXParserFactory spf=SAXParserFactory.newInstance();
        SAXParser sp=spf.newSAXParser();
        XMLReader xr=sp.getXMLReader();

        xr.setContentHandler(myhandler);
        xr.parse(new InputSource(myurl.openStream()));

        commonlist=myhandler.getGroups();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if(commonlist != null){
        gi= commonlist.get(window);
        Gid.setText(gi.getGroupName());
        notedetails.addAll(gi.getNoteNameList());
        problemdetails.addAll(gi.getProblemNameList());
        totaldetails.addAll(notedetails);
        totaldetails.addAll(problemdetails);
        totaladapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.datalook,totaldetails);
        totallist.setAdapter(totaladapter);

        left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                right.setEnabled(true);
                window--;
                if(window>=0){
                    notedetails.clear();
                    problemdetails.clear();
                    totaldetails.clear();
                    gi= commonlist.get(window);
                    Gid.setText(gi.getGroupName());
                    notedetails.addAll(gi.getNoteNameList());
                    problemdetails.addAll(gi.getProblemNameList());
                    totaldetails.addAll(notedetails);
                    totaldetails.addAll(problemdetails);
                    totaladapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.datalook,totaldetails);
                    totallist.setAdapter(totaladapter);
                }else{
                    left.setEnabled(false);
                    window++;
                    }
            }});

回答1:


you can try to get the input stream to your resource by:

InputStream is = getResources().openRawResource(R.xml.my_xml);
....
xr.parse(new InputSource(is)));

Instead of :

 URL myurl=new URL("http://www.compliantbox.com/docprocessor/sync.php");
 ...
 xr.parse(new InputSource(myurl.openStream()));

Hope it helps:)



来源:https://stackoverflow.com/questions/4680543/changing-url-path-to-local-xml-path-in-android-xml-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!