Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse local XML file in Android

Tags:

android

xml

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.

like image 812
Tamaghna M Avatar asked Sep 03 '09 09:09

Tamaghna M


People also ask

What is XML parsing in Android?

Android DOM(Document Object Model) parser is a program that parses an XML document and extracts the required information from it. This parser uses an object-based approach for creating and parsing the XML files. In General, a DOM parser loads the XML file into the Android memory to parse the XML document.

How do I read an XML string in Java?

You can parse the XML file using XPath expression, you can do XSL transformation, XSD schema validation and you can even parse whole XML file as String in just a couple of lines of code.

Which of the following are valid XML parsers in Android?

Android provides three types of XML parsers which are DOM,SAX and XMLPullParser. Among all of them android recommend XMLPullParser because it is efficient and easy to use.


2 Answers

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).

like image 50
CommonsWare Avatar answered Nov 15 '22 11:11

CommonsWare


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

like image 33
Khan Avatar answered Nov 15 '22 12:11

Khan