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. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. 31、android打开文件夹并列出里面的文件
  4. Bugly Android(安卓)这个错误 Cleartext HTTP traffic to androi
  5. Android(安卓)读取doc文件
  6. SmallActivity创建
  7. Android实现画虚线的控件
  8. Android(安卓)之 网络访问服务器,解析JSON数据
  9. 如何使用APK扩展文件

随机推荐

  1. 组件居中显示 安卓
  2. Android开发问题记录——“The connectio
  3. 第一行代码笔记之——Activity启动模式
  4. Android图文布局【整理】
  5. Android(安卓)开发笔记 - Android(安卓)S
  6. 设置可见和隐藏
  7. android AutoCompleteTextView
  8. android view的触摸事件坐标
  9. Android中获取时间
  10. Android(安卓)以流的方式读取服务器图片