Android parses xml using SAX

Source: Internet
Author: User

I. Theoretical preparation SAX uses an event-driven mechanism to parse XML documents. When the SAX Parser discovers events such as document start, element start, text, element end, and document end, an event is sent out, and the developer can write the event listener to process these events to obtain information in the XML document. The DOM standard is easy to use, but it needs to read the entire XML document at a time. During the running of the program, the entire DOM tree is resident in the memory, resulting in excessive system overhead. The SAX parsing method occupies a small amount of memory, and the processing speed is faster. Since DOM reads all the XML documents into the memory at a time, each element of the XML document can be randomly accessed. SAX adopts the sequential mode to read XML documents in sequence, so it cannot randomly access any elements of XML documents. 2. The project structure image box contains the Code required by this instance. Iii. instance implementation <? Xml version = "1.0" encoding = "UTF-8"?> <Persons> <person id = "1"> <name> Jack </name> <age> 24 </age> </person> <person id = "2"> <name> Tom </name> <age> 25 </age> </person> <person id = "3"> <name> Bob </name> <age> 22 </age> </person> </persons> ps: I don't know why. Publishing in xml format will cause problems, and java will not cause problems ~~ I don't know what's going on with anger. The Test class cannot be inserted. It shows that it is half angry .. Let's take a look at package xml; import java. io. IOException; import java. io. inputStream; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import java. util. list; public class Test {static String URL_PATH = "http: // localhost: 8080/TestGet/person. xml "; public static InputStream getInputStream () throws IOException {InputStream inputStream = null; HttpURLConnection httpURL Connection = null; try {URL url = new URL (URL_PATH); if (url! = Null) {httpURLConnection = (HttpURLConnection) url. openConnection (); // sets the timeout value for connecting to the network. setConnectTimeout (3000); httpURLConnection. setDoInput (true); // sets the http request to use the get method to request httpURLConnection. setRequestMethod ("GET"); int responseCode = httpURLConnection. getResponseCode (); if (responseCode = 200) {// obtain an input stream from the server inputStream = httpURLConnection. getInputStream () ;}} catch (Malforme DURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} return inputStream;} public static void main (String [] args) throws IOException, Exception {// TODO Auto-generated method stub if (getInputStream () = null) {System. out. println ("failed to read"); return;} List <Person> person = SaxService. readXML (getInputStream (); System. out. println (person. size (); for (int I = 0; I <person. size (); I + +) System. out. println ("id:" + person. get (I ). getId () + "age:" + person. get (I ). getAge () + "name:" + person. get (I ). getName () ;}} result: + View Code 4. legacy problem 1. what is the difference between Name and localName in sax parsing? I think this is because a buddy uses localName and person to judge whether the result is correct. Why ??? 2. In endElement, if sets "person" to "null" to prevent repeated triggering of this event. Then, what does it mean to set "currentTag" to "null? It is difficult to understand the following situations...

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.