Android general XML parsing method

1. Why do you need to write a general XML parsing method.

When different XML nodes need to be parsed. You may match different nodes during XML parsing, and the node names are written dead. In this case, you need different parsing methods to parse different nodes. Of course, this is the simplest and stupidest way. In order to reduce the code and write the code better, you need to consider designing a general XML parsing method.

2. Analytical thinking.

Generally, XML parsing results are best placed in an entity class object. In that case, it is very convenient for you to use (of course, it is more OO). You can also choose other methods to save the parsing results, but personally, I think this method is better. What do you need to do in the parsing process? This is the key to parsing. In fact, it is to set the result to be parsed to the attribute (member variable) of the object. Considering this, you must know what attributes the object has. Then add a method to the entity class (in fact, there is a certain specification) to obtain the attribute. After knowing the attribute names, the next step is, of course, to set the values of these attributes. Because the properties of different entity classes are different, the setting value adopts the reflection mechanism. This is the general idea. The specific code will be described later.

3. Parse the format type of XML.

Text only writes parsing in two XML formats. For other formats, you can refer to the ideas of this article.

① Only the content in the node: such as

① According to the design idea, you need an entity class, but the entity class has a certain specification (for parsing). Therefore, these specifications also need to implement some unified methods, so there is an abstract class: baseobj.

④ The key is how to parse.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>