打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
四大xml解析器测试代码

DOMMain.java


  1. package wkx;  
  2.   
  3. import java.io.File;  
  4.   
  5. import javax.xml.parsers.DocumentBuilder;  
  6. import javax.xml.parsers.DocumentBuilderFactory;  
  7.   
  8. import org.w3c.dom.Document;  
  9. import org.w3c.dom.NodeList;  
  10.   
  11. public class DOMMain {  
  12.   
  13.     public static void parser() {  
  14.         long lasting = System.currentTimeMillis();  
  15.         try {  
  16.   
  17.             File f = new File("C:/Users/Jack_Wong/Desktop/demo.xml");  
  18.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  19.             DocumentBuilder builder = factory.newDocumentBuilder();  
  20.             Document doc = builder.parse(f);  
  21.               
  22.             NodeList list = doc.getElementsByTagName("node");  
  23.             for (int i = 0; i < list.getLength(); i++) {  
  24.                 doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();  
  25.                 doc.getElementsByTagName("space").item(i).getFirstChild().getNodeValue();  
  26.                 /*System.out.println("name"+i+":" 
  27.                         + doc.getElementsByTagName("name").item(i) 
  28.                                 .getFirstChild().getNodeValue()); 
  29.                 System.out.println("space"+i+":" 
  30.                         + doc.getElementsByTagName("space").item(i) 
  31.                                 .getFirstChild().getNodeValue());*/  
  32.             }  
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         System.out.println("DOM RUNTIME:"  
  37.                 + (System.currentTimeMillis() - lasting) + " MS");  
  38.     }  
  39.   
  40.     public static void main(String[] args) {  
  41.         parser();  
  42.     }  
  43.   
  44. }  


JDOMMain.java



  1. package wkx;  
  2.   
  3. import java.io.File;  
  4. import java.util.List;  
  5.   
  6. import org.jdom2.Document;  
  7. import org.jdom2.Element;  
  8. import org.jdom2.input.SAXBuilder;  
  9.   
  10. public class JDOMMain {  
  11.   
  12.     public static void parser() {  
  13.   
  14.         long lasting = System.currentTimeMillis();  
  15.         try {  
  16.             SAXBuilder builder = new SAXBuilder();  
  17.             Document doc = builder.build(new File(  
  18.                     "C:/Users/Jack_Wong/Desktop/demo.xml"));  
  19.             Element foo = doc.getRootElement();  
  20.             List<Element> allChildren = foo.getChildren();  
  21.             for (int i = 0; i < allChildren.size(); i++) {  
  22.                 ((Element) allChildren.get(i)).getChild("name").getText();  
  23.                 ((Element) allChildren.get(i)).getChild("space").getText();  
  24.                 /*System.out.println("name" 
  25.                         + i 
  26.                         + ":" 
  27.                         + ((Element) allChildren.get(i)).getChild("name") 
  28.                                 .getText()); 
  29.                 System.out.println("space" 
  30.                         + i 
  31.                         + ":" 
  32.                         + ((Element) allChildren.get(i)).getChild("space") 
  33.                                 .getText());*/  
  34.             }  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.         System.out.println("JDOM RUNTIME:"  
  39.                 + (System.currentTimeMillis() - lasting) + " MS");  
  40.     }  
  41.   
  42.     public static void main(String[] args) {  
  43.   
  44.         parser();  
  45.   
  46.     }  
  47.   
  48. }  




SAXMain.java




  1. package wkx;  
  2.   
  3. import javax.xml.parsers.SAXParser;  
  4. import javax.xml.parsers.SAXParserFactory;  
  5.   
  6. import org.xml.sax.InputSource;  
  7. import org.xml.sax.helpers.DefaultHandler;  
  8.   
  9. public class SAXMain extends DefaultHandler {  
  10.   
  11.     public static void parser() {  
  12.   
  13.         long lasting = System.currentTimeMillis();  
  14.   
  15.         try {  
  16.   
  17.             SAXParserFactory sf = SAXParserFactory.newInstance();  
  18.             SAXParser sp = sf.newSAXParser();  
  19.             SAXMain reader = new SAXMain();  
  20.             sp.parse(new InputSource("C:/Users/Jack_Wong/Desktop/demo.xml"),reader);  
  21.               
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         System.out.println("SAX RUNTIME:" + (System.currentTimeMillis() - lasting) + " MS");  
  26.   
  27.     }  
  28.   
  29.     public static void main(String[] args) {  
  30.         parser();  
  31.     }  
  32.   
  33. }  



DOM4JMain.java



  1. package wkx;  
  2.   
  3. import java.io.File;  
  4. import java.util.Iterator;  
  5.   
  6. import org.dom4j.Document;  
  7. import org.dom4j.Element;  
  8. import org.dom4j.io.SAXReader;  
  9.   
  10. public class DOM4JMain {  
  11.     public static void parser() {  
  12.         long lasting = System.currentTimeMillis();  
  13.         try {  
  14.             File f = new File("C:/Users/Jack_Wong/Desktop/demo.xml");  
  15.             SAXReader reader = new SAXReader();  
  16.             Document doc = reader.read(f);  
  17.             Element root = doc.getRootElement();  
  18.             Element foo;  
  19.             for (Iterator i = root.elementIterator("node"); i.hasNext();) {  
  20.                 foo = (Element) i.next();  
  21.                 foo.elementText("name");  
  22.                 foo.elementText("space");  
  23.                 //System.out.println("name" + i + ":" + foo.elementText("name"));  
  24.                 //System.out.println("space" + i + ":" + foo.elementText("space"));  
  25.             }  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.         System.out.println("DOM4J RUNTIME:"  
  30.                 + (System.currentTimeMillis() - lasting) + " MS");  
  31.     }  
  32.   
  33.     public static void main(String[] args) {  
  34.         parser();  
  35.     }  
  36.   
  37. }  







本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Java中四种XML解析技术之不完全测试
Java中四种XML解析技术之不完全测试-JSP教程,Java与XML:站长天空|虚拟主机...
java读取xml文件的四种方法
kxml解析 xml的两种方式
Java通过XML Schema校验XML
Java开发笔记 — dom4j 和 xpath
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服