android中写XML时,需要用到XmlSerializer类
解析XML时,则需要用到XmlPullParser类
1.XmlSerializer类介绍
通过Xml.newSerializer()来获取XmlSerializer
XmlSerializer常用方法如下所示:
void setOutput(OutputStream os, String encoding); //设置要写入的XML的文件位置,以及encoding编码格式 //比如: //File file = new File(Environment.getExternalStorageDirectory(), "text.xml"); //xmlSerializer.setOutput(new fileOutputStream(file), "utf-8"); void startDocument(String encoding, Boolean standalone); //设置文档开头描述,比如:" <?xml version="1.0" encoding="utf-8" standalone="yes"?>" // standalone:表示该XML是否是独立的,默认为true,如果填为false,则表示该XML被外部文件DTD约束 startTag(String namespace, String name); //写入开始元素标签 endTag(String namespace, String name); //写入结束元素标签 attribute(String namespace, String name, String value); //往元素标签里写属性(name="value"),比如<name id="123">
text(String text); //往元素里添加内容,比如: <name id="123">张三</name>
示例代码如下:
private void writePersonXml(){ XmlSerializer serializer = Xml.newSerializer(); try { serializer.setOutput(openFileOutput("persons.xml", MODE_PRIVATE), "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null,"persons"); //创建两个成员 serializer.startTag(null,"person"); serializer.attribute(null, "id", "1"); //添加编号 serializer.startTag(null,"姓名"); serializer.text("张三"); serializer.endTag(null,"姓名"); serializer.startTag(null,"age"); serializer.text("17"); serializer.endTag(null,"age"); serializer.endTag(null,"person"); serializer.startTag(null,"person"); serializer.attribute(null, "id", "2"); //添加编号 serializer.startTag(null,"姓名"); serializer.text("李四"); serializer.endTag(null,"姓名"); serializer.startTag(null,"age"); serializer.text("19"); serializer.endTag(null,"age"); serializer.endTag(null,"person"); serializer.endTag(null,"persons"); serializer.endDocument(); //结束文档,并将内容写入文件 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
运行后,生成persons.xml:
接下来,我们便通过XmlPullParser来解析persons.xml
2.XmlPullParser介绍
通过Xml. newPullParser()来获取XmlPullParser;
XmlPullParser常用方法如下:
void setInput(InputStream inputStream, String inputEncoding); //设置要解析的文件以及编码 int getEventType(); //获取当前事件类型,返回的类型有START_DOCUMENT(文档开头)、START_TAG (元素开头)、END_TAG(元素结束)、TEXT (内容)、 END_DOCUMENT(文档结束) int next(); //获取下个事件类型. 返回的类型和getEventType()一样. getName(); //获取当前元素名 getTextCharacters(); //获取当前内容(当getEventType ()==TEXT时通过这个来获取元素内容) nextText(); //获取下个内容(当getEventType ()==START_TAG时通过这个来获取元素内容)
示例代码如下:
private void parsePersonsXml(){ XmlPullParser pullParser = Xml.newPullParser(); try { pullParser.setInput(this.openFileInput("persons.xml"), "utf-8"); String name = null; String age = null; String id = null; int eventType = pullParser.getEventType(); while(eventType!=pullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: if("person".equals( pullParser.getName())){ id = pullParser.getAttributeValue(null, "id"); //获取编号,并初始化内容 name =null; age = null; }else if("姓名".equals(pullParser.getName())){ name = pullParser.nextText(); //获取姓名 }else if("age".equals(pullParser.getName())){ age = pullParser.nextText(); //获取年龄 } break; case XmlPullParser.END_TAG: if("person".equals( pullParser.getName())){ System.out.println("person : id="+id+" 姓名="+name+" 年龄="+age); //打印信息 } break; } eventType = pullParser.next(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
运行打印: