Android Programming – XML Parsing in Android Questions and Answers

Android Programming Long Questions and AnswersHere in this section of Android Programming Long Questions and Answers,We have listed out some of the important Long Questions with Answers on XML Parsing which will help students to answer it correctly in their University Written Exam.

 

 
Q-1 What is Parsing? Explain XML Parsing with suitable example

 

Extensible Markup Language (XML) is a set of rules for encoding documents in machine-readable form.

XML is a popular format for sharing data on the internet.

Websites that frequently update their content, such as news sites or blogs, often provide an XML feed so that external programs can keep abreast of content changes.

Uploading and parsing XML data is a common task for network-connected apps.

We will see how to parse XML documents and use their data

XmlPullParser, which is an efficient and maintainable way to parse XML on Android. Historically Android has had two implementations of this interface:

KXmlParser via XmlPullParserFactory.newPullParser() ‘

ExpatPullParser, via Xml.newPullParser()

 

XML Parsing

XML stands for Extensible Mark-up Language.

XML is a very popular format and commonly used for sharing data on the internet.

This chapter explains how to parse the XML file and extract necessary information from it.

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.

So we are going to use XMLPullParser for parsing XML.

The first step is to identify the fields in the XML data in which you are interested in.

E.g., in the XML given below we interested in getting temperature only.

<?xml version="1.0"?>

<current>

<city id="2643743" name="London">

<coord lon="-0.12574" lat="51.50853"/>

<country>GB</country>

<sun rise="2013-10-08T06:13:56" set="2013-10-08T17:21:45"/>

</city>

<temperature value="289.54" min="289.15" max="290.15" unit="kelvin"/><humidity value="77" unit="%"/>

<pressure value="1025" unit="hPa"/>

</current>

 

 

XML – Elements

An xml file consist of many components. Here is the table defining the components of an XML file and their description.

Prolog – An XML file starts with a prolog. The first line that contains the information about a file is prolog

Events – An XML file has many events. Event could be like this. Document starts, Document ends, Tag start, Tag end and Text

Text – Apart from tags and events, and xml file also contains simple text. Such as GB is a text in the country tag

Attributes – Attributes are the additional properties of a tag such as value.

We will create XMLPullParser object , but in order to create that we will first create XmlPullParserFactory object and then call its newPullParser() method to create XMLPullParser. Its syntax is given below –

private XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();

private XmlPullParser myparser = xmlFactoryObject.newPullParser();

 

The next step involves specifying the file for XmlPullParser that contains XML. It could be a file or could be a Stream. In our case it is a stream. Its syntax is given below:

myparser.setInput(stream, null);

 

The last step is to parse the XML. An XML file consist of events, Name, Text, AttributesValue etc., So XMLPullParser has a separate function for parsing each of the component of XML file. Its syntax is given below –

int event = myParser.getEventType();

while (event != XmlPullParser.END_DOCUMENT) {

String name=myParser.getName();

switch (event){

case XmlPullParser.START_TAG:

break;

case XmlPullParser.END_TAG:

if(name.equals("temperature")){

temperature = myParser.getAttributeValue(null,"value");

}

break;

}

event = myParser.next();

}

 

 

The method getEventType returns the type of event that happens. e.g: Document start, tag start. The method getName returns the name of the tag and since we are only interested in temperature, so we just check in conditional statement that if we got a temperature tag, we call the method getAttributeValue to return us the value of temperature tag.

 

Sample XML Parsing

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity extends Activity {

TextView tv1;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv1=(TextView)findViewById(R.id.textView1);

try {

InputStream is = getAssets().open("file.xml");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(is);

Element element=doc.getDocumentElement();

element.normalize();

NodeList nList = doc.getElementsByTagName("employee");

for (int i=0; i<nList.getLength(); i++) {

Node node = nList.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {

Element element2 = (Element) node;

tv1.setText(tv1.getText()+"\nName : " + getValue("name",element2)+"\n");

tv1.setText(tv1.getText()+"Surname : " + getValue("surname",element2)+"\n");

tv1.setText(tv1.getText()+"-----------------------");

}

}

} catch (Exception e) {e.printStackTrace();}

}

private static String getValue(String tag, Element element) {

NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();

Node node = nodeList.item(0);

return node.getNodeValue();

}

}