XML编程:利用java程序去增删改查(CRUD)xml中的数据
解析思想:
dom解析
sax解析
基于这两种解析思想市面上就有了非常多的解析api
sun jaxp既有dom方式也有sax方式,而且这套解析api已经增加到j2se的规范中,意味这不须要导入不论什么第三方开发包就能够直接使用这样的解析方式.可是这样的解析方式效率低下,没什么人用.
dom4j 能够使用dom方式高效的解析xml.
pull
!!dom4j
SAX解析方式:
dom4j解析:
解析思想:
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 localName, String qName) throws SAXException { super.endElement(uri, localName, 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,start,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, String localName, String qName, Attributes atts) throws SAXException { // TODO Auto-generated method stub System.out.println("发现開始标签,"+qName); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub System.out.println("结束考试标签,"+qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub System.out.println(new String(ch, start, length)); } @Override public void ignorableWhitespace(char[] ch, int start, 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()); } }