好了,前两篇是转载的别人的dom解析xml,也是因为自己不熟悉dom的适应,算是学习了。下面是我自己处理的xml文件。
package com.xiaozhu.xzdz.debug.util;
import android.text.TextUtils;
import com.xiaozhu.xzdz.debug.model.ApiMsgInfo;
import com.xiaozhu.xzdz.newapp.utils.AppUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* @Description: 本地xml文件的读写(这里只有api请求信息)
* @Author: maoshenbo
* @Date: 2017/12/13 19:51
* @Version: 1.0
*/
public class APIXMLParseUtil {
private final String REQUEST_PATH = AppUtils.getInstance().getAppFilePath() + "/debug_api.xml";
private final String ELEMENT_API_MSG = "message";
private final String ELEMENT_TIME = "time";
private final String ELEMENT_REQUEST = "request";
private final String ELEMENT_RESPONSE = "response";
private final String ATTRIBUTE_TAG = "tag";
private static APIXMLParseUtil instance = null;
public static synchronized APIXMLParseUtil getInstance() {
if (instance == null) {
instance = new APIXMLParseUtil();
}
return instance;
}
/**
* 写xml文件到本地
*
* @return 返回document对象
*/
private Document getAccountDocument() {
DocumentBuilderFactory buf = DocumentBuilderFactory.newInstance();
File accountsFile = new File(REQUEST_PATH);
try {
DocumentBuilder db = buf.newDocumentBuilder();
if (!accountsFile.exists()) {
boolean b = accountsFile.createNewFile();
if (b) {
String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<api>\n" +
"</api>";
try {
FileOutputStream fos = new FileOutputStream(accountsFile, true);
PrintStream ps = new PrintStream(fos);
ps.print(xmlString);
ps.flush();
ps.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
return db.parse(accountsFile);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXParseException e) {
e.printStackTrace();
if (accountsFile.exists()) {
accountsFile.delete();
}
} catch (SAXException e) {
e.printStackTrace();
if (accountsFile.exists()) {
accountsFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 添加信息
*
* @param msg 信息
*/
public synchronized void addRequest(String msg) {
Document doc = getAccountDocument();
if (doc == null) return;
Node rootNode = doc.getFirstChild();
if (rootNode == null) return;
Element requestElement = doc.createElement(ELEMENT_REQUEST);
requestElement.appendChild(doc.createTextNode(msg));
rootNode.appendChild(requestElement);
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer former = factory.newTransformer();
former.transform(new DOMSource(doc), new StreamResult(new File(REQUEST_PATH)));
deleteMsg(doc);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
/**
* 添加信息
*
* @param msg 信息
*/
public synchronized void addMessage(String tag, String time, String msg) {
if (TextUtils.isEmpty(tag) || TextUtils.isEmpty(time) || TextUtils.isEmpty(msg)) return;
Document doc = getAccountDocument();
if (doc == null) return;
Node rootNode = doc.getFirstChild();
if (rootNode == null) return;
// 创建节点
Element messageElement = doc.createElement(ELEMENT_API_MSG);
Element timeElement = doc.createElement(ELEMENT_TIME);
Element requestElement = doc.createElement(ELEMENT_REQUEST);
Element responseElement = doc.createElement(ELEMENT_RESPONSE);
//创建节点数据
Text timeText = doc.createTextNode(tag);
Text requestText = doc.createTextNode(msg);
Text responseText = doc.createTextNode("response");
//写入属性
messageElement.setAttribute(ATTRIBUTE_TAG, tag);
//添加节点
messageElement.appendChild(timeElement);
messageElement.appendChild(requestElement);
messageElement.appendChild(responseElement);
//添加数据
timeElement.appendChild(timeText);
requestElement.appendChild(requestText);
responseElement.appendChild(responseText);
rootNode.appendChild(messageElement);
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer former = factory.newTransformer();
former.transform(new DOMSource(doc), new StreamResult(new File(REQUEST_PATH)));
deleteMsg(doc);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
/**
* 添加response信息
*
* @param tag 标识
* @param response response
*/
private synchronized void addResponse(String tag, String response) {
if (TextUtils.isEmpty(tag) || TextUtils.isEmpty(response)) return;
Document doc = getAccountDocument();
if (doc == null) return;
Node rootNode = doc.getFirstChild();
if (rootNode == null) return;
NodeList messageNodeList = rootNode.getChildNodes();
for (int i = 0; i < messageNodeList.getLength(); i++) {
Node messageNode = messageNodeList.item(i);
if (messageNode.getNodeType() == Node.ELEMENT_NODE) {
Element messageElement = (Element) messageNode;
if (messageElement.getAttribute(ATTRIBUTE_TAG).equals(tag)) {
NodeList childNodeList = messageElement.getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) {
Node childNode = childNodeList.item(j);
if (childNode.getNodeName().equals(ELEMENT_RESPONSE)) {
childNode.setTextContent(response);
}
}
}
}
}
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer former = factory.newTransformer();
former.transform(new DOMSource(doc), new StreamResult(new File(REQUEST_PATH)));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
/**
* 获取登录账号数据
*
* @return 返回map账号信息
*/
public List<String> getMessageList() {
List<String> msgList = new ArrayList<>();
if (getAccountDocument() != null) {
Node rootNote = getAccountDocument().getFirstChild();
NodeList list = rootNote.getChildNodes();
for (int temp = 0; temp < list.getLength(); temp++) {
Node node = list.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
if (ELEMENT_REQUEST.equals(eElement.getNodeName())) {
msgList.add(eElement.getTextContent());
}
}
}
}
return msgList;
}
/**
* 解析包含多层子节点的element
*
* @return 信息列表
*/
public List<ApiMsgInfo> getMessageList2() {
List<ApiMsgInfo> msgInfoList = new ArrayList<>();
if (getAccountDocument() != null) {
Node rootNote = getAccountDocument().getFirstChild();
NodeList messageNodeList = rootNote.getChildNodes();
for (int i = 0; i < messageNodeList.getLength(); i++) {
Node messageNode = messageNodeList.item(i);
if (messageNode.getNodeType() == Node.ELEMENT_NODE) {
Element messageElement = (Element) messageNode;
ApiMsgInfo msgInfo = new ApiMsgInfo();
msgInfo.tag = messageElement.getAttribute(ATTRIBUTE_TAG);
NodeList childNodeList = messageElement.getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) {
Node childNode = childNodeList.item(j);
if (childNode.getNodeName().equals(ELEMENT_TIME)) {
msgInfo.time = childNode.getTextContent();
}
if (childNode.getNodeName().equals(ELEMENT_REQUEST)) {
msgInfo.request = childNode.getTextContent();
}
if (childNode.getNodeName().equals(ELEMENT_RESPONSE)) {
msgInfo.response = childNode.getTextContent();
}
}
msgInfoList.add(msgInfo);
}
}
}
return msgInfoList;
}
/**
* 删除及结点
* @param doc 文档
*/
private synchronized void deleteMsg(Document doc) {
final int MAX_LENGTH = 20;
if (doc == null) return;
Node rootNote = doc.getFirstChild();
if (rootNote == null) return;
NodeList childNodes = rootNote.getChildNodes();
if (childNodes.getLength() > MAX_LENGTH) {
Node node = childNodes.item(0);
rootNote.removeChild(node);
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer former = factory.newTransformer();
former.transform(new DOMSource(doc), new StreamResult(new File(REQUEST_PATH)));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
}
里面包含了dom增删改查xml