下载必要的jar包:
activation.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
jaxen-1.1.1.jar
jdom-1.0.jar
一、DOM
解析器读入整个文档,然后构建一个驻留内存的树结构,使用 DOM 接口来操作这个树结构。
优点:整个文档树在内存中,便于操作;支持删除、修改、重新排列等多种功能;访问效率高。
缺点:将整个文档调入内存(包括无用的节点),浪费时间和空间;使用场合:一旦解析了文档还需多次访问这些数据;硬件资源充足(内存、CPU)
- package xmlParse;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Calendar;
- import java.util.Locale;
- import java.util.TimeZone;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import javax.xml.transform.Result;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import org.w3c.dom.DOMException;
- 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;
- /**
- * java自带的dom解析xml文件
- *
- * @author abc
- *
- */
- public class TestDom {
- public static void main(String[] args) {
- builXmlByDom();
- parseXmlByDom();
- }
- /**
- * 生成xml信息
- */
- public static void builXmlByDom() {
- long begintime = System.currentTimeMillis();
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- try {
- //构建Document
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc=db.newDocument();
- Element root=doc.createElement("students");
- root.setAttribute("class", "一班");
- root.setAttribute("count", "3");
- Element stu=doc.createElement("student");
- Element name=doc.createElement("name");
- name.appendChild(doc.createTextNode("小明"));
- Element age=doc.createElement("age");
- age.appendChild(doc.createTextNode("10"));
- stu.appendChild(name);
- stu.appendChild(age);
- root.appendChild(stu);
- stu=doc.createElement("student");
- stu.setAttribute("position","班长");
- name=doc.createElement("name");
- name.appendChild(doc.createTextNode("小王"));
- age=doc.createElement("age");
- age.appendChild(doc.createTextNode("11"));
- stu.appendChild(name);
- stu.appendChild(age);
- root.appendChild(stu);
- stu=doc.createElement("student");
- name=doc.createElement("name");
- name.appendChild(doc.createTextNode("小兵"));
- age=doc.createElement("age");
- age.appendChild(doc.createTextNode("12"));
- stu.appendChild(name);
- stu.appendChild(age);
- root.appendChild(stu);
- doc.appendChild(root);
- //将被变换的Document对象封装到一个DOMSource对象中
- DOMSource xmlSource=new DOMSource(doc);
- //使用 Transformer对象将一个Document节点变换为一个XML文件
- TransformerFactory transFactory=TransformerFactory. newInstance();
- Transformer transformer=transFactory.newTransformer();
- //创建Result
- File file=new File("students.xml");
- FileOutputStream fos;
- fos = new FileOutputStream(file);
- StreamResult result=new StreamResult(fos);
- transformer.transform(xmlSource, result);
- if(fos!=null){
- fos.close();
- }
- }catch (ParserConfigurationException e) {
- e.printStackTrace();
- }catch (TransformerConfigurationException e) {
- e.printStackTrace();
- }catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (TransformerException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("dom生成时间(毫秒)"
- + (System.currentTimeMillis() - begintime));
- }
- /**
- * 解析xml信息
- */
- public static void parseXmlByDom() {
- long begintime = System.currentTimeMillis();;
- try {
- DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
- DocumentBuilder builder=dbf.newDocumentBuilder();
- File file=new File("students.xml");
- Document doc=builder.parse(file);
- NodeList students=doc.getFirstChild().getChildNodes();
- Node student=null;
- Node node=null;
- for(int i=0,len=students.getLength();i<len;i++){
- student=students.item(i);
- NodeList childs=student.getChildNodes();
- System.out.println("第"+(i+1)+"个学生");
- for(int j=0,size=childs.getLength();j<size;j++){
- node=childs.item(j);
- if("name".equals(node.getNodeName())){
- System.out.println(node.getNodeName()+"---"+node.getTextContent());
- }
- if("age".equals(node.getNodeName())){
- System.out.println(node.getNodeName()+"---"+node.getTextContent());
- }
- }
- }
- } catch (DOMException e) {
- e.printStackTrace();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("dom解析时间(毫秒)"
- + (System.currentTimeMillis() - begintime));
- }
- }
二、SAX
特点: 1、边读边解析,应用于大型XML文档
2、只支持读
3、访问效率低
4、顺序访问
- package xmlParse;
- import java.io.File;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- public class TestSAXParse {
- /**
- * @param args
- */
- public static void main(String[] args) {
- sax();
- }
- public static void sax() {
- long begintime = System.currentTimeMillis();
- File f = new File("students.xml");
- SAXParserFactory sf = SAXParserFactory.newInstance();
- try {
- SAXParser sp = sf.newSAXParser();
- SAXHandler handler = new SAXHandler();
- sp.parse(f, handler);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("sax解析时间(毫秒)"
- + (System.currentTimeMillis() - begintime));
- }
- }
- SAXHandler.java
- package xmlParse;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import java.util.Stack;
- public class SAXHandler extends DefaultHandler {
- Stack tags = null;
- @Override
- public void startDocument() throws SAXException {
- tags=new Stack();
- }
- @Override
- public void endDocument() throws SAXException {
- while(!tags.isEmpty()){
- System.out.println(tags.peek());
- tags.pop();
- }
- tags=null;
- }
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if("students".equals(qName)){
- System.out.println(attributes.getValue("class")+"--人数-"+attributes.getValue("count"));
- }
- tags.push(qName);//压入栈
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- tags.pop();//取出栈顶元素
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- String tag=(String) tags.peek();//查看栈顶元素,但不移除
- if("name".equals(tag)){
- System.out.println("name==="+new String(ch,start,length));
- }
- if("age".equals(tag)){
- System.out.println("age==="+new String(ch,start,length));
- }
- }
- }
三、JDOM
JDOM
优点:①是基于树的处理XML的Java API,把树加载在内存中
②没有向下兼容的限制,因此比DOM简单
③速度快,缺陷少
④具有SAX的Java规则
缺点:①不能处理大于内存的文档
②JDOM表示XML文档逻辑模型。不能保证每个字节真正变换。
③针对实例文档不提供DTD与模式的任何实际模型。
④不支持与DOM中相应遍历包
最适合于:JDOM具有树的便利,也有SAX的JAVA规则。在需要平衡时使用
- JDOM
- package xmlParse;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.List;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- import org.jdom.output.XMLOutputter;
- public class TestJDOM {
- /**
- * @param args
- */
- public static void main(String[] args) {
- buildXmlByJDOM();
- parseXmlByJDOM();
- }
- public static String buildXmlByJDOM(){
- Document doc=new Document();
- Element root=new Element("students");
- root.setAttribute("count", "3");
- root.setAttribute("class", "一班");
- doc.setRootElement(root);
- root.addContent(new Element("student").addContent(new Element("name").setText("小明"))
- .addContent(new Element("age").setText("10")));
- root.addContent(new Element("student").addContent(new Element("name").setText("小汪"))
- .addContent(new Element("age").setText("11")));
- root.addContent(new Element("student").addContent(new Element("name").setText("小兵"))
- .addContent(new Element("age").setText("12")));
- ByteArrayOutputStream out=new ByteArrayOutputStream();
- XMLOutputter putter=new XMLOutputter();
- try {
- putter.output(doc, out);
- putter.output(doc, new FileOutputStream("students.xml"));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return out.toString();
- }
- public static void parseXmlByJDOM(){
- long begintime=System.currentTimeMillis();
- File f=new File("students.xml");
- SAXBuilder builder=new SAXBuilder();
- try {
- Document doc=builder.build(new FileInputStream(f));
- Element root=doc.getRootElement();
- System.out.println(root.getAttributeValue("class")+"-人数:--"+root.getAttributeValue("count"));
- List list=root.getChildren("student");
- for(int i=0;i<list.size();i++){
- Element ele=(Element)list.get(i);
- System.out.println("第"+(i+1)+"个学生");
- System.out.println(ele.getChildText("name")+"---"+ele.getChildText("age"));
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (JDOMException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("sax解析时间(毫秒)"+(System.currentTimeMillis()-begintime));
- }
- }
四、DOM4J
DOM4J 是一个非常非常优秀的java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件。如今你可以看到越来越多的 Java 软件都在使用 DOM4J 来读写 XML,特别值得一提的是连 Sun 的 JAXM 也在用 DOM4J。
DOM4J package xmlParse; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class TestDom4J { /** * @param args */ public static void main(String[] args) { buildXmlByDOM4J(); paserXmlByDOM4J(); } public static void buildXmlByDOM4J(){ Document doc=DocumentHelper.createDocument(); doc.setXMLEncoding("UTF-8"); Element root=doc.addElement("students"); root.addAttribute("class", "一班").addAttribute("count", "3"); Element student=root.addElement("student"); student.addElement("name").setText("小明"); student.addElement("age").setText("10"); student=root.addElement("student").addAttribute("position", "班长"); student.addElement("name").setText("小汪"); student.addElement("age").setText("11"); student=root.addElement("student"); student.addElement("name").setText("小兵"); student.addElement("age").setText("12"); String xmlStr=doc.asXML(); try { OutputFormat format=OutputFormat.createPrettyPrint(); XMLWriter writer=new XMLWriter(new FileWriter(new File("students.xml")),format); writer.setEscapeText(false); writer.write(xmlStr); writer.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } public static void paserXmlByDOM4J(){ long begintime=System.currentTimeMillis(); SAXReader reader=new SAXReader(); try { Document doc=reader.read(new FileInputStream(new File("students.xml"))); Element root=doc.getRootElement(); System.out.println(root.attributeValue("class")+"--人数--"+root.attributeValue("count")); /* List<Node> list=root.selectNodes("student");//需要引入jaxen-1.1.1.jar for(int i=0,len=list.size();i<len;i++){ Node node=(Node)list.get(i); System.out.println(node.selectSingleNode("name").getText()+"---"+node.selectSingleNode("age").getStringValue()); }*/ Iterator it=root.elementIterator(); Element ele; while(it.hasNext()){ ele=(Element)it.next(); //System.out.println(ele.selectSingleNode("name").getText()+"---"+ele.selectSingleNode("age").getText()); System.out.println(ele.elementText("name")+"---"+ele.elementText("age")); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } System.out.println("sax解析时间(毫秒)"+(System.currentTimeMillis()-begintime)); } }
dom4解析xml的另一个代码案例:
package com.java.team; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class ParseXml { public void read() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/test.xml")); Element root = document.getRootElement(); //将解析出来的allresource下的resourceitem放在list中 List list = root.elements("resourceitem"); //创建source存放每一个resourceitem中资源 List<XmlBean> source = new ArrayList<XmlBean>(); //将resourceitem中的各项解析出来,通过XmlBean存放到source中 for(Iterator i = list.iterator();i.hasNext();) { Element resourceitem = (Element) i.next(); String id = resourceitem.element("id").getText(); String title = resourceitem.element("title").getText(); String keywords = resourceitem.element("keywords").getText(); String kind = resourceitem.element("kind").getText(); String describe = resourceitem.element("describe").getText(); String date = resourceitem.element("date").getText(); String url = resourceitem.element("url").getText(); String author = resourceitem.element("author").getText(); String publisher = resourceitem.element("publisher").getText(); XmlBean bean = new XmlBean(); bean.setId(id); bean.setTitle(title); bean.setKeywords(keywords); bean.setKind(kind); bean.setDescribe(describe); bean.setDate(date); bean.setUrl(url); bean.setAuthor(author); bean.setPublisher(publisher); source.add(bean); } } } XmlBean类如下: [java] view plain copy package com.java.team; public class XmlBean { private String id; private String title; private String keywords; private String kind; private String describe; private String date; private String url; private String author; public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } private String publisher; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } xml文件部分如下: [html] view plain copy <?xml version="1.0" encoding="UTF-8"?> <allresource host="192.168.16.111" remote="192.168.16.111"> <resourcenum>499</resourcenum> <resourceitem> <id>2</id> <title>《实数》复习d.doc</title> <keywords>实数复习,期中复习</keywords> <kind>doc</kind> <describe>期中复习训练题</describe> <date>2008-6-18 20:50:01</date> <url>http://192.168.16.111:8080/resources/《实数》复习d.doc</url> <author>胡克林</author> <publisher>历城二中</publisher> </resourceitem> <resourceitem> <id>3</id> <title>《轴对称图形》教学设计</title> <keywords>轴对称图形,平面几何图形</keywords> <kind>doc</kind> <describe>认识轴对称图形,知道轴对称图形的含义,能够找出轴对称图形的对称轴</describe> <date>2008-6-18 20:55:10</date> <url>http://192.168.16.111:8080/resources/《轴对称图形》教学设计.doc</url> <author>胡克林</author> <publisher>历城二中</publisher> </resourceitem>