XML文件解析的机器人机器人、文件、XML

2023-09-07 00:53:41 作者:淚泪紛飛ひ湿了誰旳眸

我怎样才能解析位于系统我的本地XML文件的硬盘?

How can I parse my local XML file located in the systems hard disk?

推荐答案

如果您的文件位于/ SD卡目录,你可以使用

If your file is located in the /sdcard dir, you can use

InputStream in = new FileInputStream("/sdcard/myfile.xml");

如果它位于您的应用程序的数据目录,你可以使用

If its located in your app's data directory, you can use

File f1=new File(context.getFilesDir(), "myfile.xml");
InputStream in = new FileInputStream(f1);

如果它位于你的资产/目录中,你可以使用:

If its located inside your assets/ directory, you can use:

AssetManager assets = context.getAssets();
InputStream in = assets.open("myfile.xml");

之后,你可以使用DOM和SAX做你的XML解析

After that you can use DOM or SAX to do your XML parsing

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);