首页 > 代码库 > Android修改XML文件
Android修改XML文件
最近在项目中需要使用XML记录数据,网上这方面的文章较少,记录一下
使用DOM方式
[java] view plaincopy
- /**
- * 追加内容到XML文档
- * @param instructions
- * @throws ParserConfigurationException
- * @throws SAXException
- * @throws IOException
- * @throws TransformerException
- */
- public void append2XML(String instructions) throws ParserConfigurationException, SAXException, IOException, TransformerException{
- Long st=System.currentTimeMillis();
- String time=DateUtils.getCurrLongTime();
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- //通过实例构建DocumentBuilder
- DocumentBuilder db = dbf.newDocumentBuilder();
- //创建Document 解析给定的文件
- Document doc = db.parse(getInputStream());
- //doc = db.newDocument();
- //添加一个元素
- 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);
- //通过Name获取节点 第0个节点
- eModel.appendChild(eInstructions);
- eModel.appendChild(eCreatetime);
- eModel.appendChild(eLastUseTime);
- Node book = doc.getElementsByTagName("instructionsList").item(0);
- book.appendChild(eModel);
- //添加一个属性
- //Attr attr = doc.createAttribute("aaa");
- //创建工厂对象
- TransformerFactory tfs = TransformerFactory.newInstance();
- //创建Transformer对象
- Transformer tf = tfs.newTransformer();
- //将document输出到输出流中。
- 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文件
[java] view plaincopy
- /**
- * 获取XML中所有的指令信息
- *
- * @param inputStream
- * @return
- * @throws XmlPullParserException
- * @throws IOException
- */
- 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.将数据插入到集合
[java] view plaincopy
- /**
- * 将新指令添加到XML文件中
- * @param out
- * @param inputStream
- * @param instructions2
- * @throws IllegalStateException
- * @throws IllegalArgumentException
- * @throws XmlPullParserException
- * @throws IOException
- */
- 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文件的方法
[java] view plaincopy
- /***
- * 根据列表生成XML文件
- * @param out
- * @param instructionsList
- * @throws IllegalArgumentException
- * @throws IllegalStateException
- * @throws IOException
- */
- private void createInstructionXml(OutputStream out,
- List<Instructions> instructionsList)
- throws IllegalArgumentException, IllegalStateException, IOException {
- // TODO Auto-generated method stub
- 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();
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。