【问题标题】:changing URL path to local xml path in android xml parser在android xml解析器中将URL路径更改为本地xml路径
【发布时间】:2011-01-13 13:27:14
【问题描述】:

大家好,,,,在下面的代码中,sn-p正在解析一个在互联网上的xml文件,使用url正在访问,但是我需要解析一个本地xml(比如存储在xml文件夹中的文件项目)请任何人都可以给我加载本地xml路径的语法来代替URL,,,(我是android的新手),,thanx提前

公共类 XMLComplaint 扩展 Activity { 私人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 = new ArrayList(); ArrayList 问题详情 = 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++;
                    }
            }});

【问题讨论】:

    标签: android xml xml-parsing


    【解决方案1】:

    您可以尝试通过以下方式获取资源的输入流:

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

    而不是:

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

    希望对你有帮助:)

    【讨论】:

    • 谢谢你的回复,没有崩溃,我没有得到输出
    • 抱歉回复晚了,崩溃意味着显示屏幕正在加载但不显示解析的内容
    猜你喜欢
    • 2010-10-03
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多