1.解析思想
DOM Document object model
加载整个xml文件到内存中,形成文档对象,对文档对象进行操作;(W3C推荐)
SAX Simple API for XML
数据量太大 采用SAX 一边解析 一边释放内存 推模式解析
STAX The Stream API for XML
流解析方式 拉模式解析 JDK 6 引入
JAXP sun提供DOM SAX STAX
DOM4J DOM
XML PULL 安卓内置 解析STAX
2.
//Dom解析 读取
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = domFactory.newDocumentBuilder();
File file = new File(BaseEnv.getInstance().getEnvRoot() + "/books.xml");
Document dc = documentBuilder.parse(file);
NodeList nodeList = dc.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Log.d("debug",
node.getNodeName() +
node.getNodeType() + node.getFirstChild().getTextContent()
);
}
//Dom解析 写
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(dc);
StreamResult streamResult = new StreamResult(new File(BaseEnv.getInstance().getEnvRoot() + "/books1.xml"));
transformer.transform(domSource, streamResult);
}
Node 保函文本里的所有内容 比如一个换行 一个注释
//SAX解析 读取 无法回写
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
File file = new File(BaseEnv.getInstance().getEnvRoot() + "/books.xml");
saxParser.parse(file, new DefaultHandler());
//STAX 自己去拉取
//逐行读取
XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = xmlPullParserFactory.newPullParser();
File file = new File(BaseEnv.getInstance().getEnvRoot() + "/books.xml");
FileInputStream fileInputStream = new FileInputStream(file);
pullParser.setInput(fileInputStream, "utf-8");
int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
eventType = pullParser.next();
}
//输出xml文件
XmlSerializer xmlSerializer = xmlPullParserFactory.newSerializer();
xmlSerializer.setOutput(new FileOutputStream(new File(BaseEnv.getInstance().getEnvRoot() + "books2.xml")), "utf-8");
xmlSerializer.startDocument("utf-8",true);
xmlSerializer.startTag("","books");
xmlSerializer.text("books more");
xmlSerializer.endTag("","books");
DOM Document object model
加载整个xml文件到内存中,形成文档对象,对文档对象进行操作;(W3C推荐)
SAX Simple API for XML
数据量太大 采用SAX 一边解析 一边释放内存 推模式解析
STAX The Stream API for XML
流解析方式 拉模式解析 JDK 6 引入
JAXP sun提供DOM SAX STAX
DOM4J DOM
XML PULL 安卓内置 解析STAX
2.
//Dom解析 读取
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = domFactory.newDocumentBuilder();
File file = new File(BaseEnv.getInstance().getEnvRoot() + "/books.xml");
Document dc = documentBuilder.parse(file);
NodeList nodeList = dc.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Log.d("debug",
node.getNodeName() +
node.getNodeType() + node.getFirstChild().getTextContent()
);
}
//Dom解析 写
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(dc);
StreamResult streamResult = new StreamResult(new File(BaseEnv.getInstance().getEnvRoot() + "/books1.xml"));
transformer.transform(domSource, streamResult);
}
Node 保函文本里的所有内容 比如一个换行 一个注释
//SAX解析 读取 无法回写
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
File file = new File(BaseEnv.getInstance().getEnvRoot() + "/books.xml");
saxParser.parse(file, new DefaultHandler());
//STAX 自己去拉取
//逐行读取
XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = xmlPullParserFactory.newPullParser();
File file = new File(BaseEnv.getInstance().getEnvRoot() + "/books.xml");
FileInputStream fileInputStream = new FileInputStream(file);
pullParser.setInput(fileInputStream, "utf-8");
int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
} else if (eventType == XmlPullParser.TEXT) {
}eventType = pullParser.next();
}
//输出xml文件
XmlSerializer xmlSerializer = xmlPullParserFactory.newSerializer();
xmlSerializer.setOutput(new FileOutputStream(new File(BaseEnv.getInstance().getEnvRoot() + "books2.xml")), "utf-8");
xmlSerializer.startDocument("utf-8",true);
xmlSerializer.startTag("","books");
xmlSerializer.text("books more");
xmlSerializer.endTag("","books");
xmlSerializer.endDocument();
2016.8.25.23:35