Parsing and saving xml files with pull in Android

Source: Internet
Author: User
Xml file to be parsed
<? Xml version = "1.0" encoding = "UTF-8"?> <Helps>

  

 

Package com. gfive. help; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. io. serializable; import java. util. arrayList; import java. util. list; import org. xmlpull. v1.XmlPullParser; import com. gfive. help. tools. tools; import android. content. context; import android. util. xml;/*** help class homepage object class * @ author Administrator */public class HelpBean implemen Ts Serializable {private static final long serialVersionUID = 1L; public static final String HELP = "help"; public static final String ID = "id "; public static final String NAME = "name"; public static final String PIC_PATH = "picPath"; public String id; // number public String name; // NAME public String type; // Image Type public String picPath; // picture private static final String INDEX_PATH = "/gfive/index/"; // picture on the navigation page /*** Pull parses xml file streams * @ param is * @ return */public static List <HelpBean> getHelps (InputStream is) {List <HelpBean> list = null; HelpBean help = null; xmlPullParser parser = Xml. newPullParser (); int event =-1; try {parser. setInput (is, "UTF-8"); event = parser. getEventType (); while (event! = XmlPullParser. END_DOCUMENT) {switch (event) {case XmlPullParser. START_DOCUMENT: list = new ArrayList <HelpBean> (); break; case XmlPullParser. START_TAG: if (HelpBean. HELP. equals (parser. getName () {// get the node name pointed to by the pointer help = new HelpBean ();} if (help! = Null) {if (HelpBean. ID. equals (parser. getName () {String id = parser. nextText (); help. id = id;} else if (HelpBean. NAME. equals (parser. getName () {String name = parser. nextText (); help. name = name;} else if (HelpBean. PIC_PATH.equals (parser. getName () {String picPath = parser. nextText (); help. picPath = picPath ;}} break; case XmlPullParser. END_TAG: if (HelpBean. HELP. equals (parser. getName () {list. add (El P); help = null;} break; default: break;} event = parser. next () ;}} catch (Exception e) {e. printStackTrace ();} return list;}/*** read files in the Assert directory * @ param context * @ return */public static List <HelpBean> readAssertXml (Context context) {List <HelpBean> lists = new ArrayList <HelpBean> (); InputStream is = null; try {is = context. getResources (). getAssets (). open ("help. xml "); lists. addAll (HelpBean. getHelps (is);} ca Tch (IOException e) {e. printStackTrace () ;}finally {try {if (null! = Is) is. close ();} catch (IOException e) {e. printStackTrace () ;}} return lists;}/*** read the file under the SD card * @ param context * @ return */public static List <HelpBean> readSDCardXml (Context context) {List <HelpBean> lists = new ArrayList <HelpBean> (); InputStream is = null; try {File file = new File (Tools. SDCARD_STORAGE_PATH + INDEX_PATH + "help. xml "); if (file. exists () {is = new FileInputStream (file); lists. addAll (HelpBean. ge THelps (is) ;}} catch (Exception e) {e. printStackTrace () ;}finally {try {if (null! = Is) is. close () ;}catch (IOException e) {e. printStackTrace () ;}} return lists ;}}

Save data

public static void save(List<Persion> list,OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException{ XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(os, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "persons"); for(Persion persion : list){ serializer.startTag(null, "person"); serializer.attribute(null, "id", String.valueOf(persion.getId())); serializer.startTag(null, "name"); serializer.text(persion.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(String.valueOf(persion.getAge())); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); os.flush(); os.close(); } }

  

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.