标签:xml pull
截了图居然添加不了,不知是不是51的bug呀!!! 在android系统中,很多资源文件中,很多都是xml格式,在android系统中解析这些xml的方式,是使用pul解析器进行解析的,它和sax解析一样(个人感觉要比sax简单点),也是采用事件驱动进行解析的,当pull解析器,开始解析之后,我们可以调用它的next()方法,来获取下一个解析事件(就是开始文档,结束文档,开始标签,结束标签),当处于某个元素时可以调用XmlPullParser的getAttributte()方法来获取属性的值,也可调用它的nextText()获取本节点的值。 其实以上描述,就是对整个解析步骤的一个描述,看看代码吧 package com.example.testxml; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import com.example.ParserInterface.StudentInfo; import com.example.XMLParser.PullScoreParser; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.app.Activity; public class MainActivity extends Activity implements OnClickListener{ private List<StudentInfo> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((Button) findViewById(R.id.button1)).setOnClickListener(this); ((Button) findViewById(R.id.button2)).setOnClickListener(this); } private void readXML(){ try { InputStream is = getAssets().open("XMLTest.xml"); PullScoreParser psp = new PullScoreParser(); list = psp.parseXML(is); StringBuffer sb = new StringBuffer(); for(StudentInfo stu:list){ Log.v("result", stu.toString()); sb.append(stu.toString() + "\n"); } ((TextView)findViewById(R.id.textView1)).setText(sb); } catch (Exception e) { e.printStackTrace(); } } private boolean serivalXML(){ try { File file = new File(Environment.getExternalStorageDirectory(), "XMLTest.xml");//根目录下 FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile(), false); PullScoreParser psp = new PullScoreParser(); String buf = psp.serialize(list); fos.write(buf.getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } @Override public void onClick(View arg0) { if(arg0.getId() == R.id.button1){ readXML(); }else if(arg0.getId() == R.id.button2){ if(serivalXML()){ ((TextView)findViewById(R.id.textView1)).setText("已生成xml文件,保存在SD卡根目录下"); }else{ ((TextView)findViewById(R.id.textView1)).setText("xml文件生成失败"); } } } }
package com.example.ParserInterface; import java.io.InputStream; import java.util.List; public interface ScoreParser { /** *解析输入流 */ public List<StudentInfo> parseXML(InputStream is) throws Exception; /** * 序列化StudentInfo对象集合 * 得到XML形式的字符串 */ public String serialize(List<StudentInfo> list) throws Exception; } package com.example.ParserInterface; public class StudentInfo { private String name; private String num; private double score; public StudentInfo(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public String toString(){ return "name: " + name + "num: " + num + "score: " + score; } }
package com.example.XMLParser; import java.io.InputStream; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import com.example.ParserInterface.ScoreParser; import com.example.ParserInterface.StudentInfo; import android.util.Xml; public class PullScoreParser implements ScoreParser{ /** * XML文件解析,返回字符串形式的List * XmlPullParser.START_DOCUMENT:XML文件的入口 * XmlPullParser.START_TAG:一条数据的开始标志 * XmlPullParser.END_TAG:一条数据的结束标志 * XmlPullParser.END_DOCUMENT:XML文件的结束 */ public List<StudentInfo> parseXML(InputStream is) throws Exception { List<StudentInfo> list = null; StudentInfo stu = null; XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, "utf-8"); int eventType = parser.getEventType(); while(eventType != XmlPullParser.END_DOCUMENT){//XML文件结尾 switch(eventType){ case XmlPullParser.START_DOCUMENT: list = new ArrayList<StudentInfo>(); break; case XmlPullParser.START_TAG: if(parser.getName().equals("info")){ stu = new StudentInfo(); }else if(parser.getName().equals("name")){ eventType = parser.next(); stu.setName(parser.getText().toString()); }else if(parser.getName().equals("num")){ eventType = parser.next(); stu.setNum(parser.getText().toString()); }else if(parser.getName().equals("score")){ eventType = parser.next(); stu.setScore(Double.parseDouble(parser.getText())); } break; case XmlPullParser.END_TAG: if(parser.getName().equals("info")){ list.add(stu); stu = null; } break; default: break; } /** * 之前少了下面一句,一直无法解开 */ eventType = parser.next(); } return list; } @Override public String serialize(List<StudentInfo> list) throws Exception { XmlSerializer serializer = Xml.newSerializer(); StringWriter writer = new StringWriter(); serializer.setOutput(writer); serializer.startDocument("utf-8", true); serializer.startTag("", "scores"); for(StudentInfo stu : list){ serializer.startTag("", "info"); serializer.startTag("", "name"); serializer.text(stu.getName()); serializer.endTag("", "name"); serializer.startTag("", "num"); serializer.text(stu.getNum()); serializer.endTag("", "num"); serializer.startTag("", "score"); serializer.text(stu.getScore() + ""); serializer.endTag("","score"); serializer.endTag("", "info"); } serializer.endTag("", "scores"); serializer.endDocument(); return writer.toString(); } }
<!--SD卡操作权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
本文出自 “爬过山见过海” 博客,请务必保留此出处http://670176656.blog.51cto.com/4500575/1547549
标签:xml pull
原文地址:http://670176656.blog.51cto.com/4500575/1547549