Skip to main content

Android基础 - XML解析之dom解析

Android基础 - XML解析之dom解析

Android基础 - XML解析之dom解析

解析原理

读取内容的情况下

DocumentBuilderFactory->DocumentBulder->Document->NodeList->Node->content

更改内容的情况下

TransformerFactory->Transformer->transform(Source, Result)

实例代码如下

package com.gowhich.xmlparser;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DomXmlParserTest {
	
	@Test
	public void domXmlParser() throws Exception {
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
		Document document = documentBuilder.parse("book.xml");
		NodeList nodeList = document.getElementsByTagName("书名");
		Node node = nodeList.item(1);
		String content = node.getTextContent();
		System.out.println(content);
	}
	
	@Test
	public void domXmlModifyParser() throws Exception {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document document = documentBuilder.parse("book.xml");
		NodeList nodeList = document.getElementsByTagName("书名");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i);
			String content = node.getTextContent();
			System.out.println(content);
		}
		
		Node node = nodeList.item(1);
		node.setTextContent("书名2-改");
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		Source xmlSource = new DOMSource(document);
		Result outputTarget = new StreamResult("book.xml");
		transformer.transform(xmlSource , outputTarget);
		
	}
}

dom解析优缺点

优点:因为分配了一个树形结构,很容易的进行增加、删除、更改操作

缺点:如果要解析的文件过大,一次性在内存中分配一个树形结构,造成内存溢出

版权声明

版权声明

张大鹏 创作并维护的 Walkerfree 博客采用 创作共用保留署名-非商业-禁止演绎4.0国际许可证。本文首发于 Walkerfree 博客(https://www.walkerfree.com/),版权所有,侵权必究。本文永久链接:https://www.walkerfree.com/article/278