android xml插入节点,Android对复杂xml文件的增删改查节点操作
#Android对复杂xml的增删改查操作对以下XML文件的节点进行增删改查:
#
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(ListdelList, 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;}
更多推荐
所有评论(0)