[英]Reading a XML file from resources
I have a XML file that I need to parse in the Android SDK. 我有一个XML文件,需要在Android SDK中进行解析。
How can I read the XML file path from resources? 如何从资源中读取XML文件路径?
The XML contains: XML包含:
<Book>
<Chapter>
<NO> 1 </NO>
<Text> My Lord </Text>
</Chapter>
<Chapter>
<NO> 1 </NO>
<Text> My Lord </Text>
</Chapter>
</Book>
Put it under your_project_root\\res\\xml\\
folder. 将其放在
your_project_root\\res\\xml\\
文件夹下。 Then you can open it with: 然后,您可以使用以下命令打开它:
Resources res = activity.getResources();
XmlResourceParser xrp = res.getXml(R.xml.your_resId);
There is an example on how to use XmlResourceParser
here: 这里有一个有关如何使用
XmlResourceParser
的示例:
http://android-er.blogspot.com/2010/04/read-xml-resources-in-android-using.html http://android-er.blogspot.com/2010/04/read-xml-resources-in-android-using.html
If you have an XML file in the raw folder in your resources then you can read it with the following code: 如果您的资源的原始文件夹中有一个XML文件,则可以使用以下代码读取它:
Context context = getApplicationContext();
InputStream istream = context.getResources().openRawResource(R.raw.test);
I hope this is useful to you. 我希望这对您有用。
Before parsing xml create one folder inside your resources and put xml file inside that. 解析xml之前,请在您的资源内创建一个文件夹,并将xml文件放入其中。 And try this code.
并尝试此代码。
try {
XmlPullParser xpp=getResources().getXml(R.xml.words);
while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType()==XmlPullParser.START_TAG) {
if (xpp.getName().equals("word")) {
items.add(xpp.getAttributeValue(0));
}
}
xpp.next();
}
}
catch (Throwable t) {
Toast
.makeText(this, "Request failed: "+t.toString(), Toast.LENGTH_LONG)
.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.