最近在项目中需要使用XML记录数据,网上这方面的文章较少,记录一下
使用DOM方式
- public void append2XML(String instructions) throws ParserConfigurationException, SAXException, IOException, TransformerException{
- Long st=System.currentTimeMillis();
- String time=DateUtils.getCurrLongTime();
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-
- DocumentBuilder db = dbf.newDocumentBuilder();
-
- Document doc = db.parse(getInputStream());
-
-
- Element eModel = doc.createElement("instructionsModel");
- Element eInstructions = doc.createElement("instructions");
- Element eCreatetime = doc.createElement("createtime");
- Element eLastUseTime = doc.createElement("lastusetime");
-
- Text textInstructions = doc.createTextNode(instructions);
- Text textCreatetime = doc.createTextNode(time);
- Text textLastUseTime = doc.createTextNode(time);
- eInstructions.appendChild(textInstructions);
- eCreatetime.appendChild(textCreatetime);
- eLastUseTime.appendChild(textLastUseTime);
-
- eModel.appendChild(eInstructions);
- eModel.appendChild(eCreatetime);
- eModel.appendChild(eLastUseTime);
- Node book = doc.getElementsByTagName("instructionsList").item(0);
- book.appendChild(eModel);
-
-
-
- TransformerFactory tfs = TransformerFactory.newInstance();
-
- Transformer tf = tfs.newTransformer();
-
- tf.transform(new DOMSource(doc),new StreamResult(new FileOutputStream(fu.getSDCardRoot() + File.separator + filePath
- + File.separator +"bb.xml")));
- Long hastime=System.currentTimeMillis()-st;
- Log.i(TAG, "DOM方式耗时:"+hastime);
- }
使用PULL的方式
网上查了很久,好像大家一致认为PULL只能够解析XML,不能够对XML文件进行修改操作
所以我想了一种非常麻烦的修改方法
1.解析原XML文件,得到数据集合
2.将新数据插入到集合中去
3.重新生成XML
代码如下
1.解析XML文件
- private List<Instructions> parseDateSource(InputStream inputStream)
- throws XmlPullParserException, IOException {
- Long st=System.currentTimeMillis();
- List<Instructions> instructionsList = null;
- Instructions instructionsModel = null;
- XmlPullParser parse = Xml.newPullParser();
- parse.setInput(inputStream, "utf-8");
- int event = parse.getEventType();
- while (event != XmlPullParser.END_DOCUMENT) {
- switch (event) {
- case XmlPullParser.START_DOCUMENT:
- instructionsList = new ArrayList<Instructions>();
- break;
- case XmlPullParser.START_TAG:
- if (parse.getName().equals("instructionsModel")) {
- instructionsModel = new Instructions();
- }
- if (instructionsModel != null) {
- if (parse.getName().equals("instructions")) {
- instructionsModel.setInstructions(parse.nextText());
- } else if (parse.getName().equals("createtime")) {
- instructionsModel.setCreateTime(parse.nextText());
- } else if (parse.getName().equals("lastusetime")) {
- instructionsModel.setLastUseTime(parse.nextText());
- }
- }
- break;
- case XmlPullParser.END_TAG:
- if (parse.getName().equals("instructionsModel")) {
- instructionsList.add(instructionsModel);
- instructionsModel = null;
- }
- break;
- default:
- break;
- }
- event = parse.next();
- }
- Long hastime=System.currentTimeMillis()-st;
- Log.i(TAG, "PULL方式解析耗时:"+hastime);
- return instructionsList;
- }
2.将数据插入到集合
- private void append2xml(OutputStream out, List<Instructions> instructionsList,
- Instructions instructions2) throws IllegalArgumentException, IllegalStateException, IOException, XmlPullParserException
- {
-
- for(int i=0;i<instructionsList.size();i++){
- Instructions instructions=instructionsList.get(i);
- if(instructions.getInstructions().equals(instructions2.getInstructions())){
- instructionsList.remove(i);
- instructions2.setCreateTime(instructions.getCreateTime());
- break;
- }
- }
- instructionsList.add(0, instructions2);
- createInstructionXml(out, instructionsList);
- }
3.生成新的XML文件的方法
- private void createInstructionXml(OutputStream out,
- List<Instructions> instructionsList)
- throws IllegalArgumentException, IllegalStateException, IOException {
-
- XmlSerializer serializer = Xml.newSerializer();
-
- serializer.setOutput(out, "UTF-8");
- serializer.startDocument("UTF-8", true);
- serializer.startTag(null, "instructionsList");
- for (Instructions instructions : instructionsList) {
- serializer.startTag(null, "instructionsModel");
- serializer.startTag(null, "instructions");
- serializer.text(instructions.getInstructions());
- serializer.endTag(null, "instructions");
- serializer.startTag(null, "createtime");
- serializer.text(instructions.getCreateTime());
- serializer.endTag(null, "createtime");
- serializer.startTag(null, "lastusetime");
- serializer.text(instructions.getLastUseTime());
- serializer.endTag(null, "lastusetime");
- serializer.endTag(null, "instructionsModel");
- }
- serializer.endTag(null, "instructionsList");
- serializer.endDocument();
- out.flush();
- out.close();
- }
Android修改XML文件,布布扣,bubuko.com
Android修改XML文件
原文:http://www.cnblogs.com/daishuguang/p/3885942.html