Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate and parse data in Xml format

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to generate and parse data in Xml format", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn how to generate and parse data in Xml format.

1. Generation and parsing of Xml format data

Using xml as the carrier of data exchange is a very important function in Android. For example, weather forecast data, SMS backup data and interrogation data can all be based on xml.

Is transmitted over the network.

The format of XML is written and displayed in the form of notes, which is clear at a glance and easy to read and identify, as shown below:

Zhang San 110001male

Generation of XML

If you use java code to implement such a string format, you can use StringBuilder to assemble: StringBuilder sb = new StringBuilder ()

/ / Save data to file sb.append ("); sb.append (name); sb.append ("); sb.append ("); sb.append (number); sb.append ("); sb.append ("); sb.append (sex); sb.append ("); sb.append (")

Although the above code can also generate xml

File, but cannot handle special characters, such as if the text message contains a "" symbol, then xml

The parser cannot do the correct parsing. Therefore, the premise of use is that you are sure that the data content does not have special characters.

And Android provides us with a special API:XmlSerializer for generating XML data, the api

The handling of special characters has been implemented internally, as follows:

Try {/ / use Android api object-oriented to generate xml files. / / 1. Get the serializer XmlSerializer serializer = Xml.newSerializer () of the xml file; / / 2. Specify some initial parameters of the serializer File file = new File (getFilesDir (), name + ".xml"); FileOutputStream os = new FileOutputStream (file); serializer.setOutput (os, "utf-8"); / / 3. Write xml file .serializer.startDocument ("utf-8", true); / write opening serializer.startTag (null, "student"); / / start tag serializer.startTag (null, "name"); serializer.text (name); / / text tag serializer.endTag (null, "name"); / / end tag serializer.startTag (null, "number"); serializer.text (number); serializer.endTag (null, "number"); serializer.startTag (null, "sex"); serializer.text (sex) Serializer.endTag (null, "sex"); serializer.endTag (null, "student"); serializer.endDocument (); / write ending os.close (); Toast.makeText (this, "data saved successfully", 0). Show ();} catch (Exception e) {e.printStackTrace (); Toast.makeText (this, "failed to save data", 0). Show ();}

Analysis of XML

1. DOM parsing

Is an object-based API that stores all the contents of the XML file in memory as a document tree, and then allows you to use DOMAPI to traverse the XML tree and retrieve the desired data, so that the file can be manipulated in the form of nodes according to the structure of the tree. Because DOM needs to store the entire XML file in memory in the form of a document tree, which consumes a lot of memory, I don't mind parsing in this way under Android.

2. SAX parsing

The XML document is scanned line by line, and the parsing handler is triggered when a tag is encountered, parsing the XML in an event-handling manner. It can read the document at the same time to deal with XML, do not have to wait until the end of document loading, relatively fast, and do not need to load the entire document into memory, so there is no memory problem, you can parse super-large XML. However, SAX parsing can only be used to read XML data, and cannot be added, deleted or modified.

3. PULL parsing is similar to SAX parsing, which is also based on event handling. The PULL parser is an open source Java project that can be used either for Android applications or with JavaEE programs. Android has integrated an PULL parser, so the most common parsing method in android is PULL parsing.

Comparison of SAX and PULL parsing: how the Pull parser works compared to SAX

Parsers are similar in that they are event-driven. It provides similar events, such as start element and end element events, which you can use parser.next () to go to the next element and trigger the corresponding event. Events will be sent as numeric codes, so you can use a switch

Deal with events of interest. When the element begins to parse, call the parser.nextText () method to get the next Text

The value of the type element.

The SAX parser works by automatically pushing events into the event handler for processing, so you cannot control the active termination of event processing; while Pull

The way the parser works is to allow your application code to actively retrieve events from the parser, and because you are actively acquiring events, you can end parsing without getting events after the required conditions are met.

The code for parsing XML files using PULL under Android is as follows:

The xml file of try {/ / student information exists / / 1. Get a xml parser XmlPullParser parser = Xml.newPullParser (); / / 2. Set the initialization parameters of the parser FileInputStream inputStream = new FileInputStream (file); parser.setInput (inputStream, "utf-8"); / / 3. Parse the xml file int type= parser.getEventType (); / / get the type of the first event .System.out.println ("type:" + type); StringBuilder sb = new StringBuilder (); / / keep traversing each node while (typewriter XmlPullParser.Endpoint document) {if (type==XmlPullParser.START_TAG) {/ / start node / / judge node name if ("name" .equals (parser.getName () {String nameStr = parser.nextText () when the event type is not the end of the document. System.out.println ("name:" + nameStr "); sb.append (" name: "+ nameStr+"\ n ");} else if (" number ".equals (parser.getName () {String numberStr = parser.nextText (); System.out.println (" student number: "+ numberStr); sb.append (" student number: "+ numberStr+"\ n ");} else if (" sex ".equals (parser.getName () {String sexStr = parser.nextText (); System.out.println (" gender: "+ sexStr) Sb.append ("gender:" + sexStr+ "\ n");} type = parser.next (); / / get the next event type System.out.println ("type:" + type);} inputStream.close (); / / memory leak caused by wasting resources! Tv_result.setText (sb.toString ());} catch (Exception e) {e.printStackTrace (); Toast.makeText (this, "failed to parse student information", 0). Show ();} above is all the content of the article "how to generate and parse data in Xml format". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report