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>
3 个解决方案
#1
18
Put it under your_project_root\res\xml\
folder. Then you can open it with:
把它放在你的_project_root\res xml\文件夹下。然后你可以用:
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
#2
4
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.
我希望这对你有用。
#3
1
Before parsing xml create one folder inside your resources and put xml file inside that. And try this code.
在解析xml之前,在资源中创建一个文件夹,并将xml文件放在其中。这段代码。
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();
}
#1
18
Put it under your_project_root\res\xml\
folder. Then you can open it with:
把它放在你的_project_root\res xml\文件夹下。然后你可以用:
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
#2
4
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.
我希望这对你有用。
#3
1
Before parsing xml create one folder inside your resources and put xml file inside that. And try this code.
在解析xml之前,在资源中创建一个文件夹,并将xml文件放在其中。这段代码。
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();
}