简体   繁体   English

在Android中解析本地xml文件

[英]Parsing local xml file in android

Hii every one, am brand new to android,i have a doubt can any one help me 大家好,我是android的新手,我怀疑有人可以帮我吗

In this following link 在以下链接中

http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/ http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/

there is a sax parser in which xml file is taken from the internet, using a URL path How to change that url to local path in which xml is stored in raw folder of the project,,can any one give me the syntax ,,thanx in advance 有一个萨克斯解析器,其中使用URL路径从Internet上获取xml文件如何将url更改为将xml存储在项目的原始文件夹中的本地路径,任何人都可以给我语法,谢谢提前

This is what I did, 这就是我所做的

InputStream is = res.openRawResource(R.raw.fileName);
xr.parse(new InputSource(is));
try {
//          InputStream is = getResources().getAssets().open("yourfilename.xml");
            InputStream is =getAssets().open("yourfilename.xml");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);

            xr.parse(new InputSource(is));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

In the link you provided, replace the line 47 在您提供的链接中,替换第47行

xr.parse(new InputSource(sourceUrl.openStream()));

with

xr.parse(getResources().getAssets().open(fileName));

and place your xml file in /res/raw folder 并将您的xml文件放在/ res / raw文件夹中

links: Asset Manager Docs and Resources Manager Docs 链接: 资产管理器文档资源管理器文档

In Android you can store data in the assets folder, which can be entered by your code. 在Android中,您可以将数据存储在资产文件夹中,可以通过代码输入。 To address your file use 解决您的文件使用

file:///android_asset/yourFile.xml

I haven´t tried this yet, but I hope it will work 我还没有尝试过,但是我希望它能起作用

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

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