android操作xml封装后的类,包括创建xml和读xml。

public class XmlParserUtil{//创建xml文件 public static void createXmlFile(final String xmlPath){ File xmlFile = new File(xmlPath); FileOutputStream fileOPStream = null; try{ fileOPStream = new FileOutputStream(xmlFile); }catch (FileNotFoundException e) { Log.e("FileNotFoundException", "can't create FileOutputStream"); } XmlSerializer serializer = Xml.newSerializer(); try{ serializer.setOutput(fileOPStream,"UTF-8"); serializer.startDocument(null, true); serializer.startTag(null, "books"); for(int i = 0; i < 5; i ++){ serializer.startTag(null, "book"); serializer.startTag(null, "bookname"); serializer.text("Android教程" + i); serializer.endTag(null, "bookname"); serializer.startTag(null, "bookauthor"); serializer.text("Frankie" + i); serializer.endTag(null, "bookauthor"); serializer.endTag(null, "book"); } serializer.endTag(null, "books"); serializer.endDocument(); serializer.flush(); fileOPStream.close(); } catch (Exception e) { Log.e("XmlParserUtil","error occurred while creating xml file"); } Toast.makeText(getApplicationContext(), "创建xml文件成功!", Toast.LENGTH_SHORT).show(); } /** dom解析xml文件 * xmlPath xml的路径*/public static void domParseXML(final String xmlPath){ File file = new File(xmlPath); if(!file.exists()||file.isDirectory()){Log.e("domParseXML", "file not exists");return;}DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } Document doc = null; try { doc = db.parse(file); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } Element root = doc.getDocumentElement(); NodeList books = root.getElementsByTagName("book"); String res = "本结果是通过dom解析:" + "\n"; for(int i = 0; i < books.getLength();i++){ Element book = (Element)books.item(i); Element bookname = (Element)book.getElementsByTagName("bookname").item(0); Element bookauthor = (Element)book.getElementsByTagName("bookauthor").item(0); res += "书名: " + bookname.getFirstChild().getNodeValue() + " " + "作者: " + bookauthor.getFirstChild().getNodeValue() + "\n"; } } /** xmlPullParser解析xml文件 * xmlPath xml的路径*/public static void xmlPullParseXML(final String xmlPath){ String res = "本结果是通过XmlPullParse解析:" + "\n"; try{ XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); try{xmlPullParser.setInput(new StringReader(bufferedReaderFile(xmlPath)));}catch (Exception e) {Log.e("xmlPullParseXML", e.toString());}int eventType = xmlPullParser.getEventType(); try{ while (eventType != XmlPullParser.END_DOCUMENT){ String nodeName = xmlPullParser.getName(); switch (eventType){ case XmlPullParser.START_TAG: if("bookname".equals(nodeName)){ res += "书名: " + xmlPullParser.nextText() + " "; }else if("bookauthor".equals(nodeName)){ res += "作者: " + xmlPullParser.nextText() + "\n"; } break; default: break; } eventType = xmlPullParser.next(); } } catch (IOException e){ e.printStackTrace(); } } catch (XmlPullParserException e){ e.printStackTrace(); } } //从sd卡中读取xml文件的内容private String bufferedReaderFile(final String path) throws IOException{File file=new File(path);if(!file.exists()||file.isDirectory())throw new FileNotFoundException();BufferedReader br=new BufferedReader(new FileReader(file));String temp=null;StringBuffer sb=new StringBuffer();temp=br.readLine();while(temp!=null){sb.append(temp+" ");temp=br.readLine();}br.close();return sb.toString();}}


更多相关文章

  1. Android代码实现APK文件的安装与卸载
  2. android获取文件目录
  3. Android(安卓)文件实现断点上传
  4. android 使用VideoView加载raw目录内视频资源
  5. Flutter实现android应用内版本更新功能
  6. MTK Android(安卓)Driver :camera
  7. android用ViewPager实现欢迎界面
  8. Android(安卓)存储方式之文件存储
  9. android开发环境配置

随机推荐

  1. 关于php中对象传值方式的详解
  2. php源码加密方法详解
  3. php数组转json
  4. php中substr_compare()区分大小写吗
  5. php实现分页的原理及步骤
  6. 【哈希密码】PHP比md5更安全的加密方式
  7. PHP面向对象简易验证码类
  8. php反射机制用法详解
  9. 通过实例详细讲解PHP垃圾回收机制
  10. php对象转数组的函数