#

Android对复杂xml的增删改查操作对以下XML文件的节点进行增删改查:<?xml

version="1.0" encoding="UTF-8"?>1.0

time="2017-06-01 12:34:56">aa

time="2017-06-01 12:34:56">bb

time="2017-06-01 12:34:56">cc

-------------------1.添加元素://添加元素,其中PlayBean是数据源,str判断是playlist,还是medialistpublic void addElement(String filename, PlayBean playBean, String str) {Element root = document.getDocumentElement();NodeList stuNodeList = root.getElementsByTagName(str);for (int i = 0; i < stuNodeList.getLength(); i++) {Element stuElement = (Element) stuNodeList.item(i);NodeList stuInforList = stuElement.getChildNodes();for (int j = 0; j < stuInforList.getLength(); j++) {Node stuInforNode = stuInforList.item(j);if (stuInforNode.getNodeName().equals("day")) {NodeList dayN = stuInforNode.getChildNodes();String qTime = playBean.getQueryDate();if (!qTime.startsWith("d")) {qTime = "d" + qTime;}Element key = document.createElement(qTime);Text nameText = document.createTextNode(playBean.getListname());Attr time = document.createAttribute("time");time.setValue(playBean.getUpdatetime());key.setAttributeNode(time);key.appendChild(nameText);stuInforNode.appendChild(key);Log.i("Playdate中" + str + "--" + playBean.getQueryDate(), "添加成功");}}}UpdateXmlFile(document);}2.删除元素:public void deleteElement(List

delList, String str, List

playList) {Element root = document.getDocumentElement();NodeList stuNodeList = root.getElementsByTagName(str);for (int m = 0; m < delList.size(); m++) { PlayBean playBean = delList.get(m); for (int i = 0; i < stuNodeList.getLength(); i++) {

Element stuElement = (Element) stuNodeList.item(i); NodeList stuInforList = stuElement.getChildNodes(); for (int j = 0; j < stuInforList.getLength();

j++) { Node stuInforNode = stuInforList.item(j); if (stuInforNode.getNodeName().equals("day")) {

NodeList dayN = stuInforNode.getChildNodes(); String qTime = playBean.getQueryDate(); if (!qTime.startsWith("d")) {

qTime = "d" + qTime; } for (int k = 0; k < dayN.getLength(); k++) { Node

subnode = dayN.item(k); if (subnode.getNodeType() == Node.ELEMENT_NODE && subnode.hasAttributes()) { String key = subnode.getNodeName();

if (key.equals(qTime)) { subnode.getParentNode().removeChild(subnode); Log.i("Playdate中" + str + "--" + playBean.getQueryDate(), "删除成功");

playList.remove(playBean); UpdateXmlFile(document); } }

} } } } }

}3.修改元素:public void alertLineXml(String filename, PlayBean playBean, String str) {Element root = document.getDocumentElement();NodeList stuNodeList = root.getElementsByTagName(str);for (int i = 0; i < stuNodeList.getLength(); i++) {Element stuElement = (Element) stuNodeList.item(i);NodeList stuInforList = stuElement.getChildNodes();for (int j = 0; j < stuInforList.getLength(); j++) {Node stuInforNode = stuInforList.item(j);if (stuInforNode.getNodeName().equals("day")) {NodeList dayN = stuInforNode.getChildNodes();for (int k = 0; k < dayN.getLength(); k++) {Node subnode = dayN.item(k);if (subnode.getNodeType() == Node.ELEMENT_NODE && subnode.hasAttributes()) {String value = subnode.getFirstChild().getNodeValue();String key = subnode.getNodeName();String time = subnode.getAttributes().getNamedItem("time").getNodeValue();Element ele = (Element) dayN.item(k);if (!ele.getAttribute("time").equals(playBean.getUpdatetime())) {ele.setAttribute("time", playBean.getUpdatetime());Log.i("Playdate中" + str + "--" + key, time + "->" + playBean.getUpdatetime() + "参数修改成功");}if (!value.equals(playBean.getListname())) {subnode.setTextContent(playBean.getListname());Log.i("Playdate中" + str + "--" + key, value + "->" + playBean.getListname() + "参数修改成功");}}}}}}UpdateXmlFile(document);}4.加载文件public static Document load(String filename) {Document document = null;try {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();File file = new File(filename);document = builder.parse(file);document.normalize();} catch (Exception ex) {ex.printStackTrace();}return document;}public static boolean UpdateXmlFile(Document document) {boolean flag = true;try {/** 将document中的内容写入文件中 */TransformerFactory tFactory = TransformerFactory.newInstance();Transformer transformer = tFactory.newTransformer();// 设置各种输出属性transformer.setOutputProperty("encoding", "UTF-8");//

transformer.setOutputProperty(OutputKeys.INDENT,"yes");/** 编码 */DOMSource source = new DOMSource(document);StreamResult result = new StreamResult(new File(Const.PLAYDATE_PATH));transformer.transform(source, result);} catch (Exception ex) {flag = false;ex.printStackTrace();}return flag;}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐