XML解析方式:
XML解析器
Crimson、Xerces 、Aelfred2
XML解析开发包
Jaxp、Jdom、dom4j
Jaxp
JAXP 开发包是J2SE的一部分,它由javax.xml、org.w3c.dom 、org.xml.sax 包及其子包组成
在 javax.xml.parsers 包中,定义了几个工厂类,程序员调用这些工厂类,可以得到对xml文档进行解析的 DOM 或 SAX 的解析器对象。
使用JAXP进行DOM解析
javax.xml.parsers 包中的DocumentBuilderFactory用于创建DOM模式的解析器对象 , DocumentBuilderFactory是一个抽象工厂类,它不能直接实例化,但该类提供了一个newInstance方法 ,这个方法会根据本地平台默认安装的解析器,自动创建一个工厂的对象并返回。
获得JAXP中的DOM解析器
DOM编程
DOM模型(document object model)
DOM方式解析XML文件
xml文件
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名 id="1">Java就业培训教程</书名> <作者>redarmy</作者> <售价>39.00元</售价> </书> <书> <书名>JavaScript网页开发</书名> <作者>redarmy</作者> <售价>28.00元</售价> </书> </书架>
解析xml文件
package net.csdn; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; 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.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class DomDemo { @Test //遍历 public void read() throws ParserConfigurationException, SAXException, IOException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document document = db.parse("src/book.xml"); Node root = document.getElementsByTagName("书架").item(0); list(root); } private void list(Node node) { // TODO Auto-generated method stub if(node instanceof Element){ System.out.println(node.getNodeName()); } NodeList list = node.getChildNodes(); for(int i=0; i<list.getLength(); i++){ Node child = list.item(i); list(child); } } //读属性 @Test public void read1() throws ParserConfigurationException, SAXException, IOException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document document = db.parse("src/book.xml"); Element node = (Element) document.getElementsByTagName("书名").item(0); System.out.println(node.getAttribute("id")); } //添加节点 @Test public void insert() throws ParserConfigurationException, SAXException, IOException, TransformerException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document document = db.parse("src/book.xml"); //创建节点 Element element = document.createElement("优惠价"); element.setTextContent("29元"); //加入到第一本书 Element book = (Element) document.getElementsByTagName("书").item(0); book.appendChild(element); //把添加的书名写到文件中 TransformerFactory tff = TransformerFactory.newInstance(); Transformer ff = tff.newTransformer(); ff.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //在指定位置添加 @Test public void insert1() throws SAXException, IOException, ParserConfigurationException, TransformerException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document document = db.parse("src/book.xml"); //创建节点 Element element = document.createElement("优惠价"); element.setTextContent("29元"); //获取参考节点 Element price = (Element)document.getElementsByTagName("售价").item(0); //加入到第一本书 Element book = (Element) document.getElementsByTagName("书").item(0); book.insertBefore(element, price); //把添加的书名写到文件中 TransformerFactory tff = TransformerFactory.newInstance(); Transformer ff = tff.newTransformer(); ff.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //删除数据 @Test public void delect() throws ParserConfigurationException, SAXException, IOException, TransformerException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document document = db.parse("src/book.xml"); //获取要删除节点 Element element = (Element)document.getElementsByTagName("优惠价").item(0); //获取要删除节点的字节 Element parent = (Element) element.getParentNode(); parent.removeChild(element); //把添加的书名写到文件中 TransformerFactory tff = TransformerFactory.newInstance(); Transformer ff = tff.newTransformer(); ff.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //删除数据 @Test public void delect1() throws ParserConfigurationException, SAXException, IOException, TransformerException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document document = db.parse("src/book.xml"); //获取要删除节点 Element element = (Element)document.getElementsByTagName("售价").item(0); element.getParentNode().getParentNode().removeChild(element.getParentNode()); //把添加的书名写到文件中 TransformerFactory tff = TransformerFactory.newInstance(); Transformer ff = tff.newTransformer(); ff.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //修改数据 @Test public void update() throws ParserConfigurationException, SAXException, IOException, TransformerException{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document document = db.parse("src/book.xml"); //获取要更新节点 Element element = (Element)document.getElementsByTagName("售价").item(0); element.setTextContent("38元"); //把添加的书名写到文件中 TransformerFactory tff = TransformerFactory.newInstance(); Transformer ff = tff.newTransformer(); ff.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } }
遍历所有节点
读属性
添加节点
在指定位置添加节点
删除某一数据
删除一个表签
修改数据
SAX解析
在使用 DOM 解析 XML 文档时,需要读取整个 XML 文档,在内存中构架代表整个 DOM 树的Doucment对象,从而再对XML文档进行操作。此种情况下,如果 XML 文档特别大,就会消耗计算机的大量内存,并且容易导致内存溢出。
SAX解析允许在读取文档的时候,即对文档进行处理,而不必等到整个文档装载完才会文档进行操作。
SAX采用事件处理的方式解析XML文件,利用 SAX 解析 XML 文档,涉及两个部分:解析器和事件处理器:
package net.csdn; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class SaxDemo { /** * @param args */ public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { //创建工厂 SAXParserFactory spf = SAXParserFactory.newInstance(); //创建sax解析器 SAXParser sp = spf.newSAXParser(); //获取读取器 XMLReader xmlReader = sp.getXMLReader(); //设置事件处理器 xmlReader.setContentHandler(new BookContenHandler1()); // xmlReader.parse("src/book.xml"); } } class BookContenHandler1 extends DefaultHandler{ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("<"+qName+">"); } @Override public void characters(char[] ch, int start, int length) throws SAXException { System.out.println(new String(ch,start,length)); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("<"+qName+">"); } }
<书架>
<书>
<书名>
Java就业培训教程
<书名>
<作者>
redarmy
<作者>
<售价>
39.00元
<售价>
<书>
<书>
<书名>
JavaScript网页开发
<书名>
<作者>
redarmy
<作者>
<售价>
28.00元
<售价>
<书>
<书架>