zoukankan      html  css  js  c++  java
  • 4种xml解析器区别

    XML作用:不同应用之间的通信和数据共享
    Dom遍历法:对内存消耗大,容易内存溢出
    SAX方法:事件驱动模式,缺点 不易操作,很难同时访问多处不同数据,对内存消耗不大,速度快
    jdom方法:
    dom4j方法:优秀比jdom优秀
    
    
    
    
    Dom遍历法:全部加载内容到内存
    DocumentBuilderFactory d=DocumentBuilderFactory.newInstance();
    		try {
    			DocumentBuilder bu=d.newDocumentBuilder();
    			Document dom=bu.parse("pom.xml");
    			NodeList nl=dom.getElementsByTagName("dependency");
    			System.out.println(nl.getLength());
    			for(int i=0;i<nl.getLength();i++) {
    				Node node=nl.item(i);
    				NodeList nnm=node.getChildNodes();
    				
    				for(int k=0;k<nnm.getLength();k++) {
    					Node node1=nnm.item(k);
    					if(node1.getNodeType()==Node.ELEMENT_NODE) {
    						String tt=node1.getNodeName();
    						String tt1=node1.getTextContent();
    						System.out.println(tt+":"+tt1+"
    ");
    					}
    
    				}
    			}
    		} catch (ParserConfigurationException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (SAXException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    SAX方法 两个步骤:
    package com.xml;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class SAXhandler extends DefaultHandler {
    	int index=0;
    	public void startElement(String uri, String localName,String qName,Attributes attributes) throws SAXException {
    		super.startElement(uri, localName, qName, attributes);
    		if(qName=="project") {
    			index++;
    			System.out.println("开始遍历第"+index+"gejiedian");
    			//String va=attributes.getValue("id");//已知属性名称
    			int aa=attributes.getLength();
    			for(int i=0;i<aa;i++) {
    				String name=attributes.getQName(i);
    				String va=attributes.getValue(name);
    				System.out.println(name+":"+va);
    			}
    		}else if(qName.equals("dependency")) {
    			System.out.print("节点名称:"+qName);
    		}
    		
    		
    		
    		
    		
    		
    		
    	}
    	public void endElement(String uri, String localName, String qName) throws SAXException {
    		super.endElement(uri, localName, qName);
    		if(qName.equals("project")) {
    			System.out.println("end遍历第"+index+"gejiedian");
    		}
    		
    		
    		
    		
    	}
    	public void startDocument() throws SAXException {
    		super.startDocument();
    		System.out.println("解析开始");
    	}
    	public void endDocument() throws SAXException {
    		super.endDocument();
    		System.out.println("解析结束");
    	}
    	
    	
    	
    	public void characters(char[]ch, int start, int length) throws SAXException {
    		super.characters(ch, start, length);
    		String value=new String(ch, start, length);
    		if(!value.trim().equals("")) {
    			System.out.println(value);
    		}
    		
    	}
    }
    
    
    import java.io.IOException;
    
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.SAXException;
    
    public class SAX {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SAXParserFactory sf=SAXParserFactory.newInstance();
    		try {
    			SAXParser sp=sf.newSAXParser();
    			SAXhandler sh=new SAXhandler();
    			sp.parse("pom.xml", sh);
    			
    			
    			
    		} catch (ParserConfigurationException | SAXException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    }
    
    
    
    jdom方法:需要额外jar包
    
    import java.io.File;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.jdom.Attribute;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    
    
    
    public class JDOM {
    
    	public static void main(String[] args) throws JDOMException, IOException {
    		SAXBuilder sb=new SAXBuilder();
    		Document Document = sb.build(new File("pom1.xml"));
    		Element rootelement=Document.getRootElement();
    		List<Element> listele=rootelement.getChildren();
    		List<Object> lo=new ArrayList<Object>();
    		
    		for( Element ele:listele) {
    			obj o=new obj();
    			System.out.println("开始"+(listele.indexOf(ele)+1)+"个");
    			List<Attribute> attrs=ele.getAttributes();
    			for(Attribute attr:attrs) {
    				String attrname=attr.getName();
    				String attrvalue=attr.getValue();	
    				System.out.println(attrname+":"+attrvalue);
    			}
    			List<Element> list_sun= ele.getChildren();
    			for( Element ele1:list_sun) {
    				
    
    				if(ele1.getName().equals("groupId") ) {
    				o.setGroupId(ele1.getValue());
    			}else if(ele1.getName().equals("artifactId")) {
    				o.setArtifactId(ele1.getValue());
    			}
    				}
    			lo.add(o);
    			o=null;
    				
    		}
    		
    		
    		
    		System.out.println(lo);
    		
    	}
    
    }
    
    
    
    dom4j方法:
    import java.io.File;
    import java.util.Iterator;
    import java.util.List;
    
    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class dom4j {
    
    	public static void main(String[] args) throws DocumentException {
    		SAXReader sr=new SAXReader();
    		Document Document = sr.read(new File("pom1.xml"));
    		Element root=Document.getRootElement();
    		Iterator it=root.elementIterator();
    		while(it.hasNext()) {
    			Element ele=(Element) it.next();
    			List<Attribute> list=ele.attributes();
    			for(Attribute attr:list) {
    			System.out.println(attr.getName()+":"+attr.getValue());	
    			}
    		
    			Iterator  suneles=ele.elementIterator();
    			while(suneles.hasNext()) {
    				Element ele1=(Element) suneles.next();
    				
    				System.out.println(ele1.getName()+":"+ele1.getStringValue());	
    				}
    			
    			
    		}
    	}
    
    }
    

      

  • 相关阅读:
    codevs 1115 开心的金明
    POJ 1125 Stockbroker Grapevine
    POJ 2421 constructing roads
    codevs 1390 回文平方数 USACO
    codevs 1131 统计单词数 2011年NOIP全国联赛普及组
    codevs 1313 质因数分解
    洛谷 绕钉子的长绳子
    洛谷 P1276 校门外的树(增强版)
    codevs 2627 村村通
    codevs 1191 数轴染色
  • 原文地址:https://www.cnblogs.com/qinyios/p/11087961.html
Copyright © 2011-2022 走看看