Lesson 79. XmlPullParser. XML parse


In this lesson:

– XML ​​parsing using XmlPullParser

XmlPullParser is an XML parser that can be used to parse an XML document. The principle of his work is that it runs through the entire document, focusing on its elements. But it runs not by itself, but by the method of next. We always call the next method and use the getEventType method to check which element the parser has stopped.

The main elements of the document that catches the parser:

START_DOCUMENT – start of document

START_TAG – start of tag

TEXT – element content

END_TAG – End of tag

END_DOCUMENT – End of document

Let’s write a program that will take an xml file and parse it into tags and attributes.

Let’s create a project:

Project name: P0791_ XmlPullParser
Build Target: Android 2.3.3
Application name: XmlPullParser
Package name: ru.startandroid.develop.p0791xmlpullparser
Create Activity: MainActivity

In the folder really create a folder xml, And create a file in it data.xml:



	
		Samsung
		Galaxy
		18000
		3
		
			black
			white
		
	

This is a Samsung Galaxy phone description file. Its price, screen specifications and possible body colors are indicated. The data is fictitious and may not coincide with reality 🙂

MainActivity.java:

package ru.startandroid.develop.p0791xmlpullparser;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

public class MainActivity extends Activity {

  final String LOG_TAG = "myLogs";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String tmp = "";

    try {
      XmlPullParser xpp = prepareXpp();

      while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
        switch (xpp.getEventType()) {
        // начало документа
        case XmlPullParser.START_DOCUMENT:
          Log.d(LOG_TAG, "START_DOCUMENT");
          break;
        // начало тэга
        case XmlPullParser.START_TAG:
          Log.d(LOG_TAG, "START_TAG: name = " + xpp.getName()
              + ", depth = " + xpp.getDepth() + ", attrCount = "
              + xpp.getAttributeCount());
          tmp = "";
          for (int i = 0; i < xpp.getAttributeCount(); i++) {
            tmp = tmp + xpp.getAttributeName(i) + " = "
                + xpp.getAttributeValue(i) + ", ";
          }
          if (!TextUtils.isEmpty(tmp))
            Log.d(LOG_TAG, "Attributes: " + tmp);
          break;
        // конец тэга
        case XmlPullParser.END_TAG:
          Log.d(LOG_TAG, "END_TAG: name = " + xpp.getName());
          break;
        // содержимое тэга
        case XmlPullParser.TEXT:
          Log.d(LOG_TAG, "text = " + xpp.getText());
          break;

        default:
          break;
        }
        // следующий элемент
        xpp.next();
      }
      Log.d(LOG_TAG, "END_DOCUMENT");

    } catch (XmlPullParserException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  XmlPullParser prepareXpp() {
    return getResources().getXml(R.xml.data);
  }
} 

IN onCreate we get XmlPullParser using the prepareXpp method and starting to parse it. Then in the while loop, we run the document run until we reach the end - END_DOCUMENT. Running is provided by the method next at the end of the while loop. In the switch we check which element the parser stopped.

START_DOCUMENT - the beginning of the document

START_TAG - tag start. We print the tag name, its level in the tag tree (depth) and the number of attributes. The next line displays the names and attribute values, if any.

END_TAG - end of tag. We only print the name.

TEXT - tag content

In the method prepareXpp we are preparing XmlPullParser. To do this, extract the data from the folder res / xml. This is similar to drawing rows or pictures - first we get access to resources (getResources), then call the method corresponding to the resource. In our case, this is the getXml method. But it does not return an xml string, but a ready XmlPullParser.

We will save everything and launch the application.

We look at the log:

START_DOCUMENT
START_DOCUMENT
START_TAG: name = data, depth = 1, attrCount = 0
START_TAG: name = phone, depth = 2, attrCount = 0
START_TAG: name = company, depth = 3, attrCount = 0
text = Samsung
END_TAG: name = company
START_TAG: name = model, depth = 3, attrCount = 0
text = Galaxy
END_TAG: name = model
START_TAG: name = price, depth = 3, attrCount = 0
text = 18000
END_TAG: name = price
START_TAG: name = screen, depth = 3, attrCount = 2
Attributes: multitouch = yes, resolution = 320x480,
text = 3
END_TAG: name = screen
START_TAG: name = colors, depth = 3, attrCount = 0
START_TAG: name = color, depth = 4, attrCount = 0
text = black
END_TAG: name = color
START_TAG: name = color, depth = 4, attrCount = 0
text = white
END_TAG: name = color
END_TAG: name = colors
END_TAG: name = phone
END_TAG: name = data
END_DOCUMENT
START_DOCUMENT
START_DOCUMENT
START_TAG: name = data, depth = 1, attrCount = 0
START_TAG: name = phone, depth = 2, attrCount = 0
START_TAG: name = company, depth = 3, attrCount = 0
text = Samsung
END_TAG: name = company
START_TAG: name = model, depth = 3, attrCount = 0
text = Galaxy
END_TAG: name = model
START_TAG: name = price, depth = 3, attrCount = 0
text = 18000
END_TAG: name = price
START_TAG: name = screen, depth = 3, attrCount = 2
Attributes: multitouch = yes, resolution = 320x480,
text = 3
END_TAG: name = screen
START_TAG: name = colors, depth = 3, attrCount = 0
START_TAG: name = color, depth = 4, attrCount = 0
text = black
END_TAG: name = color
START_TAG: name = color, depth = 4, attrCount = 0
text = white
END_TAG: name = color
END_TAG: name = colors
END_TAG: name = phone
END_TAG: name = data
END_DOCUMENT

START_DOCUMENT works twice for unknown reasons. The following can be observed as the parser stops at the beginning of each tag and gives us information about it: name, level (depth), number of attributes, names and attribute names, text. It also stops at the end of the tag and we output a name. At the end, the parser says the document is complete END_DOCUMENT.

If you do not have xml in the file but received it from somewhere, then XmlPullParser should be created in another way. Let's rewrite the prepareXpp method:

  XmlPullParser prepareXpp() throws XmlPullParserException {
    // получаем фабрику
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    // включаем поддержку namespace (по умолчанию выключена)
    factory.setNamespaceAware(true);
    // создаем парсер
    XmlPullParser xpp = factory.newPullParser();
    // даем парсеру на вход Reader
    xpp.setInput(new StringReader(
        "Samsung"));
    return xpp;
  }

Here we create the parser ourselves with the help of the factory, include namespace support (in our case it is not necessary, just in case I show) and give the parser to the input a stream from the xml-string (shortened data.xml).

We'll save everything and run it. We look at the log:

START_DOCUMENT
START_TAG: name = data, depth = 1, attrCount = 0
START_TAG: name = phone, depth = 2, attrCount = 0
START_TAG: name = company, depth = 3, attrCount = 0
text = Samsung
END_TAG: name = company
END_TAG: name = phone
END_TAG: name = data
END_DOCUMENT

START_DOCUMENT has already worked here once, as it should. Well, further down are the data elements of the document.




Discuss in the forum [83 replies]

Leave a Comment