Java Data Parsing---pull

Source: Internet
Author: User

Three ways to parse XML files from Android and Java:

1. Pull Analysis

2. Sax parsing

3. Dom parsing

The three have their own merits and choose the analytic mode according to the situation

1. Pull and sax are streamed parsing, meaning that they can only read from scratch, and cannot randomly access any of the nodes in the XML file like DOM parsing

2. Pull and sax consume less memory parsing and are more suitable for parsing XML files on Android

3, Dom parsing when the file is all resolved, and finally let the user arbitrary access to specific information

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------

Below you learn how pull is parsed. (The simplicity of the pull method is its greatest advantage)

 1  Public classPullparser {2      3      PublicList<worker>Pullparsers () throws Xmlpullparserexception, IOException4     { 5list<worker> list =NULL; 6Worker worker =NULL; 7         //First step: Create an XML parsing object that requires an instance of the class to be created from the factory schema 8Xmlpullparserfactory factory =xmlpullparserfactory.newinstance ();9Xmlpullparser parser =Factory.newpullparser ();Ten //Step Two: Set up the XML data source to parse OneFileInputStream FIS =NewFileInputStream ("Worker.xml"); AParser.setinput (FIS,"Utf-8"); - //Step three: Get the event type of the current XML tag -         int Event=Parser.geteventtype (); the //Fourth step: Handle different event labels with branch statements -          while(Event!=xmlpullparser.end_document) -         { -              +             Switch(Event) -             { + //Fifth step: Get the tag name and process the label information A              CaseXmlpullparser.start_document://Parse file start, create List collection Object atList =NewArraylist<worker>(); -                  Break; -              CaseXmlpullparser.start_tag: -String TagName =parser.getname (); -                 if("worker". Equals (tagName)) -                 { inWorker =NewWorker (); -                     intCount = Parser.getattributecount ();//get the number of attributes to                      for(intI=0; i<count;i++) +                     { -String Attrname =Parser.getattributename (i); theString AttrValue =Parser.getattributevalue (i); *                         if("ID". Equals (attrname)) $Worker.setid (attrValue);Panax Notoginseng                     } -                 } the                 Else if("name". Equals (tagName)) +Worker.setname (Parser.nexttext ()); AWorker.setmoney (Double.parsedouble (Parser.nexttext ())); the                  Break; +                  -              CaseXmlpullparser.end_tag://Parse file end, $                  if("worker". Equals (Parser.getname ())) $                  { - //Sixth step: Save the processed data to the JavaBean or map object -List.add (worker); theWorker =NULL; -                  }Wuyi                   Break; the             } - //Seventh Step: Get the event type of the next tag, determine if the event type is a document end event, exit if it is, and repeat IV step Wu             Event=Parser.next (); -         } About         returnlist; $     } -  -     /**60 * @param args61 * @throws ioexception * @throws xmlpullparserexception*/ -      Public Static voidMain (string[] args) throws Xmlpullparserexception, IOException { $Pullparser Pullparser =NewPullparser (); thelist<worker> list =pullparser.pullparsers (); the          the          for(Worker w:list) the         { -System. out. println (w); in         } the     } the  About}
Demo
1<?xml version="1.0"encoding="UTF-8"?>2<workers>3<worker id="AQ01">4<name>Mark</name>5</worker>6<worker id="AD02">7<name>Luch</name>8</worker>9<worker id="AD03">Ten<name>Lily</name> One</worker> A<worker id="AD04"> -<name>Lily</name> -</worker> the</workers>
XML File
1 Package Com.qianfeng.pullparser2;2 3  Public classWorker {4     5     PrivateString ID;6     PrivateString name;7 8      PublicWorker () {9 super ();Ten         //TODO auto-generated Constructor stub One     } A      PublicWorker (string ID, string name) { - super (); -          This. ID =ID; the          This. Name =name; -  -     } -      PublicString getId () { +         returnID; -     } +      Public voidsetId (String id) { A          This. ID =ID; at     } -      PublicString GetName () { -         returnname; -     } -      Public voidsetName (String name) { -          This. Name =name; in     } -  to @Override +      PublicString toString () { -         return "Worker [id="+ ID +", Name="+ name +"]"; the     } *}
Worker.class Object class

Parsing effect:

1 worker [id=aq01, Name=Mark]2 worker [Id=ad02, Name=Luch]3 worker [id= AD03, Name=Lily]4 Worker [Id=ad04, name=lily]
Console Information

Java Data Parsing---pull

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.