在Android的XML解析器改变URL路径本地XML路径路径、Android、XML、URL

2023-09-07 12:48:11 作者:殇

HII每个人,,,在以下code段正在互联网上这在解析XML文件中,使用URL访问我,,但我需要解析本地XML(比如存储在XML文件夹文件在项目中),请任何一个可以给我的语法来加载URL的地方本地XML的路径,,,(我是全新到Android),, thanx提前

公共类XMLComplaint延伸活动{    私人INT窗口= 0;    私人字符串S1,S2,S3,S4,S5,S6;    串STR1,STR2,STR3,STR4,STR5,str6,STR7,STR8,STR9,str10,str11,str12,str13,str14,sidsize;    私人ArrayList的notedetails =新的ArrayList();            ArrayList的problemdetails =新的ArrayList();            ArrayList的totaldetails =新的ArrayList();

 留下私人的ImageButton,权利;私人TextView的GID进行        ListView控件notelist = NULL,problemlist = NULL,totallist = NULL;私人ArrayAdapter<串GT; totaladapter = NULL;私人列表<&的GroupInfo GT; commonlist;私人的GroupInfo GI;MyHandler的将myHandler = MyHandler的新();@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.main);    GID =(的TextView)findViewById(R.id.Gid);    左=(的ImageButton)findViewById(R.id.left);    右=(的ImageButton)findViewById(R.id.right);    totallist =(ListView控件)this.findViewById(R.id.Listview1);    尝试{        网址myurl =新的URL(http://www.compliantbox.com/docprocessor/sync.php);        SPF SAXParserFactory的= SAXParserFactory.newInstance();        的SAXParser SP = spf.newSAXParser();        XMLReader的XR = sp.getXMLReader();        xr.setContentHandler(将myHandler);        xr.parse(新的InputSource(myurl.openStream()));        commonlist = myhandler.getGroups();    }赶上(MalformedURLException的E){        e.printStackTrace();    }赶上(的ParserConfigurationException E){        e.printStackTrace();    }赶上(SAXException的E){        e.printStackTrace();    }赶上(IOException异常五){        e.printStackTrace();    }    如果(commonlist!= NULL){        GI = commonlist.get(窗口);        Gid.setText(gi.getGroupName());        notedetails.addAll(gi.getNoteNameList());        problemdetails.addAll(gi.getProblemNameList());        totaldetails.addAll(notedetails);        totaldetails.addAll(problemdetails);        totaladapter =新ArrayAdapter<串GT;(getApplicationContext(),R.layout.datalook,totaldetails);        totallist.setAdapter(totaladapter);        left.setOnClickListener(新OnClickListener(){            @覆盖            公共无效的onClick(视图v){                right.setEnabled(真);                窗口 - ;                如果(窗GT; = 0){                    notedetails.clear();                    problemdetails.clear();                    totaldetails.clear();                    GI = commonlist.get(窗口);                    Gid.setText(gi.getGroupName());                    notedetails.addAll(gi.getNoteNameList());                    problemdetails.addAll(gi.getProblemNameList());                    totaldetails.addAll(notedetails);                    totaldetails.addAll(problemdetails);                    totaladapter =新ArrayAdapter<串GT;(getApplicationContext(),R.layout.datalook,totaldetails);                    totallist.setAdapter(totaladapter);                }其他{                    left.setEnabled(假);                    窗口++;                    }            }}); 

解决方案

您可以尝试让输入流到你的资源:

  InputStream为= getResources()openRawResource(R.xml.my_xml)。....xr.parse(新的InputSource(是))); 
AndroidManifest.xml详解

而不是:

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

希望它能帮助:)

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++;
                    }
            }});

解决方案

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:)

 
精彩推荐