Android builds an XML parsing framework, androidxml

Source: Internet
Author: User

Android builds an XML parsing framework, androidxml

In our work, we often get data from the server and parse it. The server returns two types of data: json and xml. Json can be parsed using excellent open-source frameworks such as gson or fastjson, which saves a lot of trouble. Generally, we only need to design the bean and then call a single sentence to parse it successfully. To parse xml data, we have to compare one tag and one tag, which is also very troublesome to modify. For example, we do not need data on a tag now, we have to delete the line in the Code. If our requirements change and we need to add data with a tag, we can only add the corresponding code to the code, this is very troublesome. In this article, we will introduce how to design our own xml parsing framework, so that it can be parsed successfully by calling a code just like gson.

First, analyze the xml data format returned by the server, and then design it. My data is obtained from the aggregated data.

1.We use the xml pull parsing method. The parsing process is from the first starting tag order to the last ending tag. First, determine the tags on which data we need. There is data on reason and title. We want to get it. Then there is a bunch of items. Each item contains some data, which we want to get. 2.Analysis structure: items are stored in a list at first glance. The tags in each item are the same, but the data is different. Therefore, we need to design a ListBean, it is used to save the data in the item and put it in an ArrayList. In this way, all items can be saved. Let's look at reason and title again. These two labels should also be stored in a bean. we are designing a Bean's entire xml data structure into a bean and a list structure: bean: reason and title list: All items 3.Let's design a final object class, a bean and a list where T is a generic type, which is passed in the object class of item, that is, the ListBean in this example. 4.In the parsing process, the first tag is parsed backward. We must let parser know when the outer Bean is, and when it is the internal ListBean, when to add ListBean to the list. In fact, it is very simple. When parser parses to <root>, we let the Bean be instantiated. When it is parsed to </root>, the Bean will be saved to ResultBeanAndList. When parser resolves to <item>, We instantiate ListBean. when parsed to </list>, add ListBean to ResultBeanAndList. 5.We use a custom Request based on Volley to retrieve xml data. For details about how to customize a Request, see the Android network framework-Volley (5) Use Volley to send a custom Request. 6.The data obtained from xml is as follows: the data in the final result is summarized as follows: 1. to avoid confusion, the annotations in the Code use the names of entity classes in this example. In fact, we define generics. Entity classes can be passed into any class, based on our actual needs, we can design the entity classes we need. For example, if we do not need the title data, we can directly Delete the title attribute in the Bean. If we add an unrelated attribute to the Bean, such as Public String abc; we do not need to worry about errors either, through Field field = listClazz. getField (tagName); If the xml file does not contain the <abc> label, it will jump directly to the catch file and then be executed backward without any errors. 2. in this example, the xml file is a bean and a list structure. If it is a bean and two lists structure, we need to design a parsing method based on this idea, if there is only one bean structure, it is simpler. When we encounter a structure in our work, we will design it and add it to XmlUtils. In this way, we have formed our own xml parsing framework. In the future, we will have an xml file with the same structure and directly design the entity class.

Welcome to reprint, reprint please indicate the source: Ali lamp of the CSDN blog: http://blog.csdn.net/nugongahou110



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.