Android Wing of mucchin | XML-related



How to read the XML of Android Apps


How to analyze load the XML in the Android app?

I think many of you are thinking the app in the Android app, that, to do something to analyze the XML file.
For example, if that, and want to use the Web services API, XML is an essential technique for analysis.
(With a Web service API is the thing that provides us with Rakuten, Amazon, etc., such as recruitment, and many companies, here is their description is omitted.)
So, I will introduce the procedure for reading XML in Android, to the analysis, I have done.


First of all, leave it to import the following two classes.
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;


But I think if when you use the Web services API, and take the way by importing an XML file in the communication HTTP, that, to obtain an InputStream instance, in the description of this time, we are reading the InputStream instance the XML file already I will assume that.
As follows: 1., To get the XmlPullParser instance.


XmlPullParser xmlPullParser = Xml.newPullParser ();


XmlPullParser, please think class and provides the ability for people to go read the following elements of the XML.
As follows: 2., To set the InputStream XML file has been loaded into the XmlPullParser.


xmlPullParser.setInput (is, "UTF-8");


※ is the first argument is the second argument InputStream instance, please specify the encoding of the XML file.


Well, ready to go to analyze this XML is now ready.
Along the rest that want to do, but go read it, here, we will be called, we expect it to read as follows.
• In the from the beginning to the end of the XML file, to obtain a list of (Value) value of the name tag.
"And the value of the name tag" is part of the following xxx.
<name> xxx </ name>
First, let's look at the sample code.




String a [] = new String [] ();
int counter = 0;
for (int e = xmlPullParser.getEventType ();! e = XmlPullParser.END_DOCUMENT; e = xmlPullParser.next ()) {
if (e == XmlPullParser.START_TAG && xmlPullParser.getName (). equals ("name")) {
a [counter] = xmlPullParser.nextText ();
counter + +
}
}


Have not written only minimal code, looks like this.
xmlPullParser is, is I do not have every element or one per line, we will read one by one to really carefully, and XML.
If the start tag, value, end tag, if you use any of the () is a mechanism go on start and end tag ⇒ ⇒ tag value XmlPullParser.next.
This can be done by either convenient or inconvenient, Which I will.
I first I was puzzled.
If you look at the for loop, and is the initial value () xmlPullParser.getEventType.
At this point, xmlPullParser just because you set the InputStream, the value of () has become the "START_DOCUMENT" means the start getEventType.
The end of the loop condition is, until the "END_DOCUMENT" means the last.
The degree of the loop, and go on to next run the () XmlPullParser.next.
Loop is called.
You can be a result, it is said, will read from the beginning to the end of the XML file.
The following is a description of the inside of the loop.
The conditions of the statement described in terms of if.
Simply put, it means that "if it is the start tag of the name tag".
I want, but it a "value" that are enclosed in opening and closing tags of the name, in its own "value" that has become the current of xmlPullParser is, the value is taken, the tag for what it is the first place is what had been surrounded by, I do not take. (GetName. () Return value becomes null)
By So, when the start tag of the name tag is turned in the current, that takes a string of the following elements, in a sentence if, use the () nextText.
This is the method called to retrieve the next element as a String.


There are other way, so, a good example is what the above does not necessarily understand.
I have been doing roughly as described above.
Incidentally, how to value but instead, take the attributes of the tag, then so is presented here.
For example, let's say something like that in the same as above, in turn, that, take a list of the id attribute of the tag name.
"And the id attribute of the tag name" is part of the following yyy.
<name id=yyy> xxx </ name>


for loops, if statements, so the same, only the contents of the statement taken if.
Do the following.


a [counter] = xmlPullParser.getAttributeValue (null, "name");


If you get attributes, use the () getAttributeValue as described above.
The first argument specifies a "name" attribute name is null, the second argument.
According to the API documentation, making it the "namespace" The first argument, second argument is "name" attribute.
I am writing a namespace if inert, the first argument is, do not and it is not null, this has to null.
About "namespace XML", I just simply do not have details, here is their description does not.
No, can not be. (Laughs)
getAttributeValue (), as the use of one, it also provides one pattern with another argument of type int.
Since this is to specify the index, if this case to replace if you attribute so the first one,


a [counter] = xmlPullParser.getAttributeValue (0);


Will be.
As the subscript of the array, you specify a value obtained by subtracting 1 from the actual number.

And, if even only method introduced above, can be obtained in the analysis of XML Web services API.