An Efficient Xml Parsing Method for android-pull

Keywords: xml Android Attribute Mobile

As we know, parsing XML is also widely used in various platforms, as is android. It often involves parsing and reading of XML. There are three main parsing methods: SAX parser, DOM parser and PULL parser.
SAX parser: The core of SAX parser is event processing mode. It mainly works around event source and event processor. ContentHandler is an important core. When an event source is encountered in the parsing process, only calling event parser can get corresponding processing. However, its advantage is that it has fast parsing speed and occupies less memory. It is very suitable for Android mobile devices.
DOM parser: The principle of DOM parsing is based on tree structure and information data set. Documents are divided into independent elements, attributes and annotations. Then, XML files are represented in memory in the form of node tree. Using objects in DOM, XML documents can be read, searched, modified, added and deleted.
PULL parser: PULL parser runs in an event-based manner similar to SAX. The difference is that in the PULL parsing process, we need to get the generated events ourselves and do the corresponding operations, instead of the way SAX triggers an event by the processor to execute our code. The PULL parser is compact, portable, fast and easy to use. It is very suitable for Android mobile devices. The PULL parser is also used to parse various XML in Android system.

Next, let's focus on a simple way to use the PULL parser:

First we put an xml file in assets:

Next, we read the ID names of several controls in xml by pull. It is important to note that when pullParser.getAttributeValue() is called, we can usually extract the attribute values of the label according to the following table of numbers, that is, pullParser.getAttributeValue(1), but if you want to get the attribute values according to the attribute names, you have to write: pullParse.getAttributeValue(). R. getAttributeValue (null, "id"). ok, let's look directly at the complete code demo:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv_text = (TextView) findViewById(R.id.tv_text);
        try {
            //Get an XML parser
            XmlPullParser pullParser = Xml.newPullParser();
            InputStream is = getAssets().open("activity_main.xml");
            pullParser.setInput(is, "utf-8");
            //Get the event type
            int eventType = pullParser.getEventType();
            //End of Document
            //Traversing through internal content
            StringBuilder stringBuilder = new StringBuilder();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String name = pullParser.getName();
                if (!TextUtils.isEmpty(name))
                    if (eventType == XmlPullParser.START_TAG) {
                        String attributeValue = pullParser.getAttributeValue(null, "id");
                        attributeValue = attributeValue.substring(attributeValue.indexOf("/") + 1, attributeValue.length());

                        stringBuilder.append("name====");
                        stringBuilder.append(name);
                        stringBuilder.append("\t\tid====");
                        stringBuilder.append(attributeValue);
                        stringBuilder.append("\n\n");
                    }
                eventType = pullParser.next();//Read the next label
            }
            tv_text.setText(stringBuilder.toString());

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

The operation results are as follows:

Posted by Okami on Tue, 26 Mar 2019 12:51:29 -0700