SAX 解析器采用了基于事件的模型,它在解析 XML 文档的时候可以触发一系列的事件,当发现给定的tag的时候,它可以激活一个回调方法,告诉该方法制定的标签已经找到。SAX 对内存的要求通常会比较低,因为它让开发人员自己来决定所要处理的tag。特别是当开发人员只需要处理文档中所包含的部分数据时,SAX 这种扩展能力得到了更好的体现。但用 SAX 解析器的时候编码工作会比较困难,而且很难同时访问同一个文档中的多处不同数据。
JAXP:
JAXP开发包是J2SE的一部分,它由javax.xml、org.w3c.dom、org.xml.sax包及其子包组成
在javax.xml.parsers包中,定义了几个工厂类,程序员调用这些工厂类,可以得到xml文档的DOM
或SAX的解析器,从而实现对XML文档的解析。
使用JAXP进行DOM解析:
javax.xml.parsers包中的DocumentBuilderFactory用于创建DOM模式的解析器对象, DocumentBuilderFactory是一个抽象工厂类,它不能直接实例化,但该类提供了一个newInstance 方法,这个方法会根据本地平台默认安装的解析器,自动创建一个工厂的对象并返回。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/sql.xml");//传入所要解析的XML文件所在路径此对象与javascript中对象几乎一样,对应的方法也一样,有
Element中定义了getAttribute(String name)/getTagName()/setAttribute()等方法。
示例:
package cn.itcast.xml; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; //使用dom方式对xml文档进行CRUD public class Demo3 { //读取<书名>C++教程</书名> @Test public void read1() throws Exception { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/book.xml"); NodeList list=document.getElementsByTagName("书名"); Node node=list.item(1); String content=node.getTextContent(); System.out.println(content);//C++教程 } //得到文档中所有标签 @Test public void read2() throws Exception { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/book.xml"); NodeList list=document.getElementsByTagName("书名"); Node root=document.getElementsByTagName("书架").item(0); list(root); } private void list(Node node) { Node child; if (node instanceof Element) System.out.println(node.getNodeName()); NodeList nodelist=node.getChildNodes(); for (int i=0;i<nodelist.getLength();i++) { child=nodelist.item(i); list(child); } } //得到文档中标签属性<书名 name="xxxx">java web就业</书名> @Test public void read3() throws Exception { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/book.xml"); NodeList list=document.getElementsByTagName("书名"); Node node=list.item(0); if(node.hasAttributes()){ NamedNodeMap nodemap=node.getAttributes(); for(int i=0;i<nodemap.getLength();i++) { Node nd=nodemap.item(i); String strname=nd.getNodeName(); String strval=nd.getNodeValue(); System.out.println(strname+":"+strval);//name:dddd } } Element node2=(Element)list.item(0); String str3=node2.getAttribute("name"); System.out.println("__"+str3);//__dddd } //<售价>10</售价> @Test public void add() throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/book.xml"); //创建节点 Element price=document.createElement("售价"); price.setTextContent("59.0元"); //把创建的节点放到第一本书上 document.getElementsByTagName("书").item(0).appendChild(price); //把跟新后的内容写回文档 Transformer transformer=TransformerFactory.newInstance().newTransformer(); DOMSource source=new DOMSource(document); FileOutputStream outstream =new FileOutputStream(new File("src/outbook.xml")); StreamResult reslut=new StreamResult(outstream); transformer.transform(source, reslut); outstream.close(); } //向文档中指定位置上添加节点 <售价>10</售价> @Test public void add2() throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/book.xml"); //创建节点 Element price=document.createElement("售价"); price.setTextContent("59.0元"); //得到参考节点 Element refNode=(Element)document.getElementsByTagName("售价").item(0); //得到挂崽的节点 Element book=(Element)document.getElementsByTagName("书").item(0); //把创建的节点放到第一本书上 document.getElementsByTagName("书").item(0).appendChild(price); // 往book节点指定位置插值 book.insertBefore(price, refNode); Transformer transformer=TransformerFactory.newInstance().newTransformer(); DOMSource source=new DOMSource(document); FileOutputStream outstream =new FileOutputStream(new File("src/outbook2.xml")); StreamResult reslut=new StreamResult(outstream); transformer.transform(source, reslut); outstream.close(); } //向文档节点 添加属性 <售价>10</售价> @Test public void addAtt() throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/book.xml"); //得到参考节点 Element refNode=(Element)document.getElementsByTagName("售价").item(0); refNode.setAttribute("addAtrr","new cha ru value"); Transformer transformer=TransformerFactory.newInstance().newTransformer(); DOMSource source=new DOMSource(document); FileOutputStream outstream =new FileOutputStream(new File("src/outbook3.xml")); StreamResult reslut=new StreamResult(outstream); transformer.transform(source, reslut); outstream.close(); } //删除 <售价>10</售价> @Test public void delete() throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/book.xml"); //得到要删除的节点 Element refNode=(Element)document.getElementsByTagName("售价").item(0); refNode.getParentNode().removeChild(refNode); Transformer transformer=TransformerFactory.newInstance().newTransformer(); DOMSource source=new DOMSource(document); FileOutputStream outstream =new FileOutputStream(new File("src/outbook3.xml")); StreamResult reslut=new StreamResult(outstream); transformer.transform(source, reslut); outstream.close(); } //更新 售价 @Test public void update() throws Exception{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/book.xml"); //得到要更新的节点 Element refNode=(Element)document.getElementsByTagName("售价").item(0); refNode.setTextContent("1000"); Transformer transformer=TransformerFactory.newInstance().newTransformer(); DOMSource source=new DOMSource(document); FileOutputStream outstream =new FileOutputStream(new File("src/outbook3.xml")); StreamResult reslut=new StreamResult(outstream); transformer.transform(source, reslut); outstream.close(); } }
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名 name="dddd">java web就业</书名> <作者>张孝祥</作者> <售价>40</售价> </书> <书> <书名>C++教程</书名> <作者>自己</作者> <售价>50</售价> </书> </书架>