import java.io.FileOutputStream; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; 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 Dom4jDemo1 { @Test public void dom4j1() throws Exception{ //1.创建解析器 SAXReader reader=new SAXReader(); //2.解析xml获取代表整个文档的dom对象 Document dom=reader.read("book.xml"); //3.获取跟元素 Element root= dom.getRootElement(); //4.逐级获取,拿到想要的书名,然后打印出来 String text=root.element("书").element("书名").getText(); System.out.println(text); } @Test public void dom4j2() throws Exception{ //1.创建解析器 SAXReader reader=new SAXReader(); //2.获取代表整个文档的dom对象 Document dom=reader.read("book.xml"); //3.获取根元素 Element root=dom.getRootElement(); //4.获取书名集 List<Element> list=root.elements("书"); String bookName=list.get(1).element("书名").getText(); System.out.println(bookName); } @Test public void add() throws Exception{ //获取解析器 SAXReader reader=new SAXReader(); //解析xml获取dom Document dom=reader.read("book.xml"); //获取根元素 Element root=dom.getRootElement(); //<特价>20.00</特价> Element price2Ele=DocumentHelper.createElement("特价"); price2Ele.setText("20.00美元"); //把<特价>20.00</特价>挂载到书下 root.element("书").add(price2Ele); //写回到xml中 // Writer write=new OutputStreamWriter(new FileOutputStream("book.xml"),"UTF-8"); // dom.write(write); // write.flush(); // write.close(); XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write(dom); writer.flush(); writer.close(); } @Test public void update()throws Exception{ //获取解析器 SAXReader reader=new SAXReader(); //解析xml获取dom Document dom=reader.read("book.xml"); //获取跟元素 Element root=dom.getRootElement(); //修改值 root.element("书").element("特价").setText("2.0元"); //更新xml XMLWriter writer=new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write(dom); writer.close(); } @Test public void delete()throws Exception{ //获取解析器 SAXReader reader=new SAXReader(); //解析xml获取dom Document dom=reader.read("book.xml"); //获取跟元素 Element root=dom.getRootElement(); //删除特价 Element price2=root.element("书").element("特价"); price2.getParent().remove(price2); //更新xml XMLWriter writer=new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); } @Test public void add2()throws Exception{ //获取解析器 SAXReader reader=new SAXReader(); //解析xml获取dom Document dom=reader.read("book.xml"); //获取根元素 Element root=dom.getRootElement(); //凭空创建特价 Element price2Ele=DocumentHelper.createElement("特价"); price2Ele.setText("3.0元"); //插入到指定的位置 List<Element> list=root.element("书").elements(); list.add(1,price2Ele); //跟新xml XMLWriter writer=new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write(dom); writer.close(); } @Test public void attr()throws Exception{ //获取解析器 SAXReader reader=new SAXReader(); //解析xml获取dom Document dom=reader.read("book.xml"); //获取跟元素 Element root=dom.getRootElement(); //加属性 //创建属性 出版社="xxx出版社"; Element bookEle=root.element("书"); Attribute attr=DocumentHelper.createAttribute(bookEle,"出版社","xxx出版社"); //挂载到父标签上 bookEle.add(attr); bookEle.addAttribute("出版社","xxx出版社"); //查询属性 Attribute attrr=bookEle.attribute("出版社"); String name=attrr.getName(); String value=attrr.getValue(); System.out.println(name+"..."+value); //修改属性 bookEle.setAttributeValue("出版社","xxx出版社"); //删除属性 Attribute attrr1=bookEle.attribute("出版社"); bookEle.remove(attrr1); //更新xml XMLWriter writer=new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write(dom); writer.close(); } }
上面的代码需要第三方的jar包dom4j.jar
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.junit.Test; 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; public class SaxDemo1 { //解析方式一 @Test public void sax1() throws Exception{ //1.创建解析工厂 SAXParserFactory factory=SAXParserFactory.newInstance(); //2.生产解析器 SAXParser parser=factory.newSAXParser(); //3.获取读取器 XMLReader reader=parser.getXMLReader(); //4.设置事件处理器 我采用匿名内部类实现 reader.setContentHandler(new ContentHandler(){ boolean flag=false; int count=0; @Override public void startElement(String uri,String localName,String name,Attributes atts) throws SAXException { // TODO Auto-generated method stub //System.out.println("xml文档开始解析了...."); if("书名".equals(name)){ flag=true; count++; } } @Override public void characters(char[] ch,int start,int length) throws SAXException { // TODO Auto-generated method stub if(flag && count==2){ System.out.println(new String(ch,start,length)); } } @Override public void endElement(String uri,String name) throws SAXException { // TODO Auto-generated method stub //System.out.println("xml文档解析完成了"); if("书名".equals(name)){ flag=false; } } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub } @Override public void endPrefixMapping(String prefix) throws SAXException { // TODO Auto-generated method stub } @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 setDocumentLocator(Locator locator) { // TODO Auto-generated method stub } @Override public void skippedEntity(String name) throws SAXException { // TODO Auto-generated method stub } @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub } @Override public void startPrefixMapping(String prefix,String uri) throws SAXException { // TODO Auto-generated method stub } }); //5.解析xml reader.parse("book.xml"); } }
配置文件如下:
book.xml
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名>Java就业培训教程</书名> <特价>3.0元</特价> <作者>张孝祥</作者> <售价>39.00元</售价> </书> <书> <书名>JavaScript网页开发</书名> <作者>张孝祥</作者> <售价>28.00元</售价> </书> </书架>原文链接:https://www.f2er.com/xml/299140.html