
一、单个对象生成xml
生成以下xml,该怎么生成呢?
<?xml version='1.0' enCoding='UTF-8' standalone='yes' ?><account> <ID>1</ID> <password>123456</password> <name>传说之美</name> <createDate>2015-02-02 11:50:42</createDate></account>
先定义一个account类,属性有ID、name、password、createDate。
public class Account { private String ID; private String password; private String name; private String createDate; public Account() { super(); } public Account(String ID,String password,String name,String createDate) { super(); this.ID = ID; this.password = password; this.name = name; this.createDate = createDate; } public String getID() { return ID; } public voID setID(String ID) { this.ID = ID; } public String getpassword() { return password; } public voID setPassword(String password) { this.password = password; } public String getname() { return name; } public voID setname(String name) { this.name = name; } public String getCreateDate() { return createDate; } public voID setCreateDate(String createDate) { this.createDate = createDate; } @OverrIDe public String toString() { return "Account [ID=" + ID + ",password=" + password + ",name=" + name + ",createDate=" + createDate + "]\n\n"; }}定义好这个类,就可以利用XmlSerializer用于写xml数据了。写个方法,把生成的xml保存在xmlparser_account.xml文件。
/** * 单个对象生成xml * @param account */ private static voID XmlfileCreator(Account account) { file newxmlfile = new file(Environment.getExternalStorageDirectory() + "/xmlparser_account.xml"); try { if (!newxmlfile.exists()) newxmlfile.createNewfile(); } catch (IOException e) { Log.e("IOException","exception in createNewfile() method"); } fileOutputStream fileos = null; try { fileos = new fileOutputStream(newxmlfile); } catch (fileNotFoundException e) { Log.e("fileNotFoundException","can't create fileOutputStream"); } // XmlSerializer用于写xml数据 XmlSerializer serializer = Xml.newSerializer(); try { // XmlSerializer 用 UTF-8 编码 serializer.setoutput(fileos,"UTF-8"); serializer.startdocument(null,Boolean.valueOf(true)); serializer.setFeature("http://xmlpull.org/v1/doc/features.HTML#indent-output",true); serializer.startTag(null,"account"); // xml-tree,由startTag开始,endTag结束 serializer.startTag(null,"ID"); serializer.text(account.getID()); serializer.endTag(null,"ID"); serializer.startTag(null,"password"); serializer.text(account.getpassword()); serializer.endTag(null,"password"); serializer.startTag(null,"name"); serializer.text(account.getname()); serializer.endTag(null,"name"); serializer.startTag(null,"createDate"); serializer.text(account.getCreateDate()); serializer.endTag(null,"createDate"); serializer.endTag(null,"account"); serializer.enddocument(); // 写xml数据到fileOutputStream serializer.flush(); // 关闭fileos,释放资源 fileos.close(); } catch (Exception e) { Log.e("Exception","error occurred while creating xml file"); } }生成account对象,单个对象生成xml
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Account account = new Account("1","123456","传说之美",sdf.format(new Date())); XmlfileCreator(account);查看保存的文件
二、解析单个对象组成的xml为单个对象
把生成的xmlparser_account.xml文件放在res/xml/下,将这个xml解析为Account对象。这里用XmlResourceParser,XmlResourceParser继承了xmlpullparse的类。
Pull解析和sax解析类似,都采用事件驱动进行解析的,当pull解析器,开始解析后,调用它的next()方法,获取下一个解析事件(包括4个解析事件:开始文档,结束文档,开始标签,结束标签),这里单单说一下Pull解析。
/** * 解析单个对象组成的xml和xml组 * @return */ private List<Account> getListData() { List<Account> accountList = new ArrayList<Account>(); XmlResourceParser xrp = getResources().getXml(R.xml.xmlparser_account); try { // 直到文档的结尾处 Account account = null; while (xrp.getEventType() != XmlResourceParser.END_document) { String tagname = xrp.getname(); if (xrp.getEventType() == XmlResourceParser.START_document){ } // 如果遇到了开始标签 if (xrp.getEventType() == XmlResourceParser.START_TAG) { Log.i("",tagname); if(tagname.equals("account")){ account = new Account(); } else if (account != null) { if (tagname.equals("ID")) { String ID = xrp.nextText();// 通过属性名来获取属性值 account.setID(ID); } else if (tagname.equals("password")) { String password = xrp.nextText();// 通过属性索引来获取属性值 account.setPassword(password); } else if (tagname.equals("name")) { String name = xrp.nextText(); account.setname(name); } else if (tagname.equals("createDate")) { String createDate = xrp.nextText(); account.setCreateDate(createDate); } } } if (xrp.getEventType() == XmlResourceParser.END_TAG) { if (tagname.equals("account") && account !=null) { accountList.add(account); account = null; } } xrp.next();// 获取解析下一个事件 } } catch (XmlPullParserException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } return accountList; }直接打印结果看看
Log.i("",getListData().toString());log如下
三、单个对象组成的xml组
类似这样
<?xml version='1.0' enCoding='UTF-8' standalone='yes' ?><accounts> <account> <ID>2</ID> <password>123456</password> <name>传说</name> <createDate>2015-02-02 02:54:41</createDate> </account> <account> <ID>3</ID> <password>567890</password> <name>之美</name> <createDate>2015-02-02 02:54:41</createDate> </account></accounts>
生成单个对象组 组成的xml组跟单个对象xml基本差不多,写成了一个方法,把生成的xml保存在xmlparser_accounts.xml文件。
/** * 生成单个对象的xml数组 * * @param data */ private static voID XmlfileCreator(List<Account> data) { file newxmlfile = new file(Environment.getExternalStorageDirectory() + "/xmlparser_accounts.xml"); try { if (!newxmlfile.exists()) newxmlfile.createNewfile(); } catch (IOException e) { Log.e("IOException","can't create fileOutputStream"); } XmlSerializer serializer = Xml.newSerializer(); try { serializer.setoutput(fileos,true); serializer.startTag(null,"accounts"); for (Account account : data) { serializer.startTag(null,"account"); serializer.startTag(null,"ID"); serializer.text(account.getID()); serializer.endTag(null,"ID"); serializer.startTag(null,"password"); serializer.text(account.getpassword()); serializer.endTag(null,"password"); serializer.startTag(null,"name"); serializer.text(account.getname()); serializer.endTag(null,"name"); serializer.startTag(null,"createDate"); serializer.text(account.getCreateDate()); serializer.endTag(null,"createDate"); serializer.endTag(null,"account"); } serializer.endTag(null,"accounts"); serializer.enddocument(); serializer.flush(); fileos.close(); } catch (Exception e) { Log.e("Exception","error occurred while creating xml file"); } }简单地用几行代码生成
Account account1 = new Account("2","传说",sdf.format(new Date())); Account account2 = new Account("3","567890","之美",sdf.format(new Date())); List<Account> accountList = new ArrayList<Account>(); accountList.add(account1); accountList.add(account2); XmlfileCreator(accountList);生成的文件如下
四、解析单个对象组成的xml组
跟 二、解析单个对象组成的xml为单个对象 一样 ,请查看二
五、生成具有attribute的单个对象组成的xml组
类似如下,account里面还包含一个attribute值如何生成,其实很简单,在 三、单个对象组成的xml组 基础上修改一点就可以了
<?xml version='1.0' enCoding='UTF-8' standalone='yes' ?><accounts> <account ID="2"> <password>123456</password> <name>传说</name> <createDate>2015-02-02 04:50:45</createDate> </account> <account ID="3"> <password>567890</password> <name>之美</name> <createDate>2015-02-02 04:50:45</createDate> </account></accounts>
修改地方为
for (Account account : data) { serializer.startTag(null,"account"); serializer.attribute(null,"ID",account.getID());// serializer.startTag(null,"ID");// serializer.text(account.getID());// serializer.endTag(null,"account"); }六、解析具有attribute的单个对象组成的xml组
解析同理,跟四、解析单个对象组成的xml组 差不多,修改ID部分解析即可
// 如果遇到了开始标签 if (xrp.getEventType() == XmlResourceParser.START_TAG) { Log.i("",tagname); if(tagname.equals("account")){ account = new Account(); String ID = xrp.getAttributeValue(null,"ID"); account.setID(ID); } else if (account != null) { if (tagname.equals("ID")) {// String ID = xrp.nextText();// account.setID(ID); } else if (tagname.equals("password")) { String password = xrp.nextText(); account.setPassword(password); } else if (tagname.equals("name")) { String name = xrp.nextText(); account.setname(name); } else if (tagname.equals("createDate")) { String createDate = xrp.nextText(); account.setCreateDate(createDate); } } }本文原创链接:http://www.cnblogs.com/liqw/p/4267461.html
下载地址:http://download.csdn.net/detail/lqw770737185/8417583
总结以上是内存溢出为你收集整理的Android 生成和Pull解析xml全部内容,希望文章能够帮你解决Android 生成和Pull解析xml所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)