湛江市工程建设领域网站,泉州模板建站源码,深圳flash网站建设,济南网站建设app文章目录 1.SAX 解析器1#xff09;什么是 SAX2#xff09;SAX 工作流程初始化实现事件处理类解析 3#xff09;示例代码 2.DOM 解析器1#xff09;什么是 DOM2#xff09;DOM 工作流程初始化解析 XML 文档操作 DOM 树 3#xff09;示例代码 总结 在项目开发中#xff0… 文章目录 1.SAX 解析器1什么是 SAX2SAX 工作流程初始化实现事件处理类解析 3示例代码 2.DOM 解析器1什么是 DOM2DOM 工作流程初始化解析 XML 文档操作 DOM 树 3示例代码 总结 在项目开发中XML 是一种常见的数据交换格式。为了处理和解析 XML 文档Java 提供了两种主要的解析方式SAXSimple API for XML和 DOMDocument Object Model。 1.SAX 解析器
1什么是 SAX
SAX 是一种基于事件的 XML 解析方式。它逐行的扫描 XML 文档并在解析的过程中触发事件允许程序对文档进行响应。由于 SAX 不需要将整个文档加载到内存中因此适用于处理大型 XML 文件。
2SAX 工作流程 初始化
创建 SAXParserFactory 实例并通过它创建 SAXParser
SAXParserFactory factory SAXParserFactory.newInstance();
SAXParser saxParser factory.newSAXParser();实现事件处理类
创建一个类实现 org.xml.sax.helpers.DefaultHandler 类或其子类重写需要处理的事件方法
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;public class HandlerDemo extends DefaultHandler {Overridepublic void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {// 处理元素开始事件}Overridepublic void characters(char[] ch, int start, int length) throws SAXException {// 处理元素文本事件}Overridepublic void endElement(String uri, String localName, String qName) throws SAXException {// 处理元素结束事件}
}解析
使用 SAXParser 解析 XML 文档并将事件处理类注册到解析器中
HandlerDemo handler new HandlerDemo();
saxParser.parse(example.xml, handler);3示例代码 example.xml ?xml version1.0 encodingUTF-8?
studentnamecheney/nameage18/age
/studentDemo.java import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
public class Demo {public static void main(String[] args) throws Exception {// 1. 初始化SAXParserFactory factory SAXParserFactory.newInstance();SAXParser saxParser factory.newSAXParser();// 2. 实现事件处理类DefaultHandler handler new DefaultHandler() {Overridepublic void startElement(String uri, String localName, String qName, Attributes attributes) {System.out.println(元素开始: qName);}Overridepublic void characters(char[] ch, int start, int length) {String str new String(ch, start, length).trim();if (!.equals(str)) {System.out.println(元素文本: str);}}Overridepublic void endElement(String uri, String localName, String qName) {System.out.println(元素结束: qName);}};// 3. 解析String path D:\\workspace\\demo\\src\\main\\resources\\example.xml;saxParser.parse(new File(path), handler);}
}输出结果
元素开始: student 元素开始: name 元素文本: cheney 元素结束: name 元素开始: age 元素文本: 18 元素结束: age 元素结束: student
2.DOM 解析器
1什么是 DOM
DOM 是一种基于树结构的 XML 解析方式。它将整个 XML 文档加载到内存中并形成一个树形结构允许通过节点的方式访问和修改文档的内容。DOM 解析器适用于需要频繁随机访问 XML 数据的情况。
2DOM 工作流程 初始化
创建 DocumentBuilderFactory 实例并通过它创建 DocumentBuilder
DocumentBuilderFactory factory DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder factory.newDocumentBuilder();解析 XML 文档
使用 DocumentBuilder 解析 XML 文档得到 Document 对象
Document document docBuilder.parse(example.xml);操作 DOM 树
使用 Document 对象进行节点的增删改查操作。
// 获取根元素
Element root document.getDocumentElement();
// 获取名为 element 的所有节点
NodeList nodeList root.getElementsByTagName(student);for (int i 0; i nodeList.getLength(); i) {Node node nodeList.item(i);if (node.getNodeType() Node.ELEMENT_NODE) {Element element (Element) node;System.out.println(学生信息: element.getTextContent());}
}3示例代码 example.xml studentsstudentnamecheney/nameage18/age/student
/studentsDemo.java mport org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class Demo {public static void main(String[] args) throws Exception{// 1. 初始化DocumentBuilderFactory factory DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder factory.newDocumentBuilder();// 2. 解析String path D:\\workspace\\demo\\src\\main\\resources\\example.xml;Document document docBuilder.parse(new File(path));// 3. 操作 DOM 树Element root document.getDocumentElement();NodeList nodeList root.getElementsByTagName(student);for (int i 0; i nodeList.getLength(); i) {Node node nodeList.item(i);if (node.getNodeType() Node.ELEMENT_NODE) {Element element (Element) node;System.out.println(学生信息: element.getTextContent());}}}
}输出结果
学生信息: cheney 18
总结
解析 XML 文件的解析器有 SAX 解析器 和 DOM 解析器 两种不同方式各自有适用的场景。SAX 适用于大型 XML 文件它基于事件的方式逐行解析不需要将整个文档加载到内存。DOM 适用于需要随机访问 XML 数据的情况它将整个文档加载到内存形成树状结构允许直接操作节点。在选择解析方式时需根据具体需求和文档大小来选择适当的解析器。 文章转载自: http://www.morning.khdw.cn.gov.cn.khdw.cn http://www.morning.bktzr.cn.gov.cn.bktzr.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.gwsdt.cn.gov.cn.gwsdt.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.zwxfj.cn.gov.cn.zwxfj.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.qtzqk.cn.gov.cn.qtzqk.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.qcymf.cn.gov.cn.qcymf.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.smpb.cn.gov.cn.smpb.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.mgfnt.cn.gov.cn.mgfnt.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.nxzsd.cn.gov.cn.nxzsd.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.pffx.cn.gov.cn.pffx.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.pghfy.cn.gov.cn.pghfy.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn http://www.morning.ckfqt.cn.gov.cn.ckfqt.cn http://www.morning.wwdlg.cn.gov.cn.wwdlg.cn http://www.morning.kbdjn.cn.gov.cn.kbdjn.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.ghslr.cn.gov.cn.ghslr.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.dztp.cn.gov.cn.dztp.cn