5

How can I parse my local XML file located in my android project (inside res/XML folder) and want to get the values in that file.

2

2 Answers 2

10

To access XML resources stored in res/xml, call getResources().getXml() from any Activity or other Context. You need to supply to getXml() the ID of the XML to load (R.xml.myfile).

Sign up to request clarification or add additional context in comments.

1 Comment

The getXml() method automatically returns you an XmlResourceParser implementation instance, so you can directly pass it to XmlPullParser and start parsing your data.
4

to read your xml add code as shown below

XmlResourceParser myxml = mContext.getResources().getXml(R.xml.MyXml);
//MyXml.xml is name of our xml in newly created xml folder, mContext is the current context
// Alternatively use: XmlResourceParser myxml = getContext().getResources().getXml(R.xml.MyXml);

myxml.next();//Get next parse event
int eventType = myxml.getEventType(); //Get current xml event i.e., START_DOCUMENT etc.

and to get content of code add code shown below

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.