Android - XML Parser
XML stands for Extensible Markup Language.XML is a very famous arrangement and generally used for allocation information on the
internet. This chapter analyze how to parse the XML file and quotation necessary advice from it. Android afford three types of XML
parsers which are DOM,SAX and XMLPullParser. Surrounded by all of them android advocate XMLPullParser because it is active
and easy to use. So we are functioning to use XMLPullParser for parsing XML.
The first step is to analyze the garden in the XML data in which you are absorbed in. For example. In the XML given below we absorbed
in getting condition only.
in getting condition 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>
<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 contains of many ingredients. Here is the table decide the components of an XML file and their confession.
Prolog
An XML file kick off with a prolog. The first line that accomodate the information about a file is prolog
Text
Apart from tags and events, and xml file also contains simple text. Similar as GB is a text in the country tag.
Attributes
Attributes are the additional properties of a tag such as value e.t.c
Events
An XML file has many actions. Event could be like this. Document starts , Document ends, Tag start , Tag end and Text e.t.c
Learn all the XML related concepts with android training in Chandigarh. Now, Let us discuss about the XML - Parsing.
XML - Parsing
XMLPullParser object in procedure to constitute that we will first create XmlPullParserFactory object and then call its
newPullParser() method to build XMLPullParser. Its syntax is given below −
newPullParser() method to build XMLPullParser. Its syntax is given below −
private XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
private XmlPullParser myparser = xmlFactoryObject.newPullParser();
private XmlPullParser myparser = xmlFactoryObject.newPullParser();
The next step affect determine the file for XmlPullParser that contains XML. It could be a case or could be a current.
In our case it is a stream.Its syntax is given below −
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 get along of accident, Name, Text, AttributesValue e.t.c. So XMLPullParser
has a disparts function for define each of the basic of XML file.
has a disparts function for define each of the basic of XML file.
Here we have given the syntax 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();
}
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 appearance the type of causality that happens. e.g: Document start , tag start e.t.c.
The method getName arrival the name of the tag and after all we are only interested in temperature , so we just
check in limited allegation that if we got a climate tag , we call the approach getAttributeValue to arrival us
the value of temperature tag.
The method getName arrival the name of the tag and after all we are only interested in temperature , so we just
check in limited allegation that if we got a climate tag , we call the approach getAttributeValue to arrival us
the value of temperature tag.
Apart from the these approach, there are other ways to provide the class for improved parsing XML files.
Other methods are listed below −
Other methods are listed below −
getAttributeCount()
This method just arrival the number of attributes of the current start tag
getAttributeName(int index)
This method returns the name of the aspect described by the index value
getColumnNumber()
This method returns the Returns the current column number, starting from 0.
getDepth()
This method returns Returns the current depth of the element.
getLineNumber()
Returns the current line number, starting from 1.
getNamespace()
This method returns the namespace URI of the modern element.
getPrefix()
This method returns the prefix of the current element
getName()
This method returns the name of the tag
getText()
This method returns the text for that particular element
isWhitespace()
This method analysis even if the latest TEXT event accommodate only whitetops characters.
Android widget
A widget is a small gadget or authority of your android application arranged on the home screen.
Widgets can be very accessible as they gives permission to put your favourite applications on
your home cover in order to rapidly approach them. You have doubtless seen some accepted
widgets, such as music widget, weather widget, clock widget e.t.c
Widgets can be very accessible as they gives permission to put your favourite applications on
your home cover in order to rapidly approach them. You have doubtless seen some accepted
widgets, such as music widget, weather widget, clock widget e.t.c
Widgets could be of many types such as information widgets, collection widgets, control widgets
and hybrid widgets. Android provides us a complete framework to develop our own widgets.
and hybrid widgets. Android provides us a complete framework to develop our own widgets.
Widget - XML file
To build an application widget, Starting you required is AppWidgetProviderInfo object, which you
will agree a independent widget XML file. Right click on your program and create a new folder
called xml. Now you have to right click on the new created folder and create a XML file.
The effectiveness of the XML file will be set to AppWidgetProvider.
will agree a independent widget XML file. Right click on your program and create a new folder
called xml. Now you have to right click on the new created folder and create a XML file.
The effectiveness of the XML file will be set to AppWidgetProvider.
Widget - Layout file
Now you have to define the layout of your widget in your default XML file. You can drag
components to generate auto xml.
components to generate auto xml.
Widget - Java file
After defining layout, now create a new JAVA file or use existing one, and extend it with AppWidgetProvider
class and reverse its update method as follows.With this we have to define the object of two classes
which are RemoteViews and Pending Intent. Learn more about android parser at Android Training in Chandigarh.
class and reverse its update method as follows.With this we have to define the object of two classes
which are RemoteViews and Pending Intent. Learn more about android parser at Android Training in Chandigarh.
Syntax is −
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
onDeleted(Context context, int[] appWidgetIds)
This is called when an instance of AppWidgetProvider is deleted.
onDisabled(Context context)
This is called when the last instance of AppWidgetProvider is deleted
onEnabled(Context context)
This is called when an instance of AppWidgetProvider is created.
onReceive(Context context, Intent intent)
It is used to dispatch calls to the various methods of the class
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.