解析思想:
dom解析
sax解析
基于这两种解析思想市面上就有了很多的解析api
sun jaxp既有dom方式也有sax方式,并且这套解析api已经加入到j2se的规范中,意味这不需要导入任何第三方开发包就可以直接使用这种解析方式.但是这种解析方式效率低下,没什么人用.
dom4j 可以使用dom方式高效的解析xml.
pull
!!dom4j
导入开发包,通常只需要导入核心包就可以了,如果在使用的过程中提示少什么包到lib目录下在导入缺少的包即可
解析的xml文件:
<?xml version="1.0" encoding="UTF-8"?> <书架> <书 出版社="北京出版社"> <书名>Java就业培训教程</书名> <作者>张孝祥</作者> <售价>50.00元</售价> <特价>20.0元</特价> </书> <书> <书名>JavaScript网页开发</书名> <作者>张孝祥</作者> <售价>40.00元</售价> </书> </书架>
SAX解析方式:
package sax; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class SaxDemo1 { public static void main(String[] args) throws Exception { //解析器工厂 SAXParserFactory factory=SAXParserFactory.newInstance(); //sax解析器 SAXParser parser = factory.newSAXParser(); //获取读取器 XMLReader reader = parser.getXMLReader(); //注册事件 reader.setContentHandler(new MyContentHander2()); //解析xml文档 reader.parse("book.xml"); } } //读取第二本书的价格 class MyContentHander2 extends DefaultHandler{ private String eleName=""; private int count=0; @Override public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException { super.startElement(uri,localName,qName,attributes); this.eleName=qName; } @Override public void endElement(String uri,String qName) throws SAXException { super.endElement(uri,qName); eleName=""; } @Override public void characters(char[] ch,int start,int length) throws SAXException { super.characters(ch,start,length); if("书名".equals(eleName)&&++count==2){ System.out.println(new String(ch,length)); } } } class MyContentHander implements ContentHandler{ @Override public void setDocumentLocator(Locator locator) { // TODO Auto-generated method stub } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("开始读取文档...."); } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("结束读取文档...."); } @Override public void startPrefixMapping(String prefix,String uri) throws SAXException { // TODO Auto-generated method stub } @Override public void endPrefixMapping(String prefix) throws SAXException { // TODO Auto-generated method stub } @Override public void startElement(String uri,Attributes atts) throws SAXException { // TODO Auto-generated method stub System.out.println("发现开始标签,"+qName); } @Override public void endElement(String uri,String qName) throws SAXException { // TODO Auto-generated method stub System.out.println("结束考试标签,"+qName); } @Override public void characters(char[] ch,int length) throws SAXException { // TODO Auto-generated method stub System.out.println(new String(ch,length)); } @Override public void ignorableWhitespace(char[] ch,int length) throws SAXException { // TODO Auto-generated method stub } @Override public void processingInstruction(String target,String data) throws SAXException { // TODO Auto-generated method stub } @Override public void skippedEntity(String name) throws SAXException { // TODO Auto-generated method stub } }
dom4j解析:
package dom4j; import java.io.FileOutputStream; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; public class Dom4jDemo { @Test public void attr() throws Exception{ SAXReader reader=new SAXReader(); Document doc = reader.read("book.xml"); Element rootElement = doc.getRootElement(); Element attribute = rootElement.element("书").addAttribute("出版社","北京出版社"); XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write(doc); writer.close(); } @Test public void add()throws Exception{ SAXReader reader=new SAXReader(); Document doc = reader.read("book.xml"); Element rootElement = doc.getRootElement(); //凭空创建 Element Telement = DocumentHelper.createElement("特价"); Telement.setText("20.0元"); //挂载 rootElement.element("书").add(Telement); XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write(doc); writer.close(); } @Test public void delete()throws Exception{ SAXReader reader=new SAXReader(); Document doc = reader.read("book.xml"); Element rootElement = doc.getRootElement(); Element element = rootElement.element("书").element("特价"); element.getParent().remove(element); XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write(doc); writer.close(); } @Test public void update() throws Exception{ //dom4j解析器 SAXReader reader=new SAXReader(); //得到document Document doc = reader.read("book.xml"); //得到根元素 Element rootElement = doc.getRootElement(); List<Element> elements = rootElement.elements(); Element element = elements.get(0); element.element("售价").setText("50.00元"); XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write(doc); writer.close(); } @Test public void find() throws Exception{ //dom4j解析器 SAXReader reader=new SAXReader(); //得到document Document doc = reader.read("book.xml"); //得到根元素 Element rootElement = doc.getRootElement(); List<Element> elements = rootElement.elements(); Element element = elements.get(1); System.out.println(element.element("书名").getText()); } }