xml四种解析方式

 1 import java.io.File;
 2 import java.util.List;
 3 
 4 import org.dom4j.Attribute;
 5 import org.dom4j.Document;
 6 import org.dom4j.DocumentException;
 7 import org.dom4j.Element;
 8 import org.dom4j.io.SAXReader;
 9 
10 
11 /**
12  * dom4j is an easy to use, open source library for working with XML, XPath and XSLT 
13  * on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP.
14  * @author mulberries
15  *
16  */
17 
18 public class Dom4jParseXml {
19     public static void main(String[] args) throws DocumentException {
20         SAXReader saxReader=new SAXReader();
21         Document document=saxReader.read(new File("c:"+File.separator+"test.txt"));
22         /**
23          * 遍历所有节点以及节点下的属性
24          */
25         Element rootElement=document.getRootElement();
26         Dom4jParseXml dom4jParseXml=new Dom4jParseXml();
27         dom4jParseXml.listNodes(rootElement);
28     }
29     
30     /**
31      * 列出所有的节点
32      * @param element
33      */
34     public void listNodes(Element element){
35         @SuppressWarnings("unchecked")
36         List<Element> elements=element.elements();
37         //没有子元素
38         if(elements.size()==0){
39             System.out.print(element.getPath());
40             Dom4jParseXml dom4jParseXml=new Dom4jParseXml();
41             dom4jParseXml.listAttribute(element);
42             System.out.println();
43         }else{
44             for(int i=0;i<elements.size();i++){
45                 listNodes(elements.get(i));
46             }
47         }
48     }
49     /**
50      * 列出节点下的所有属性
51      * @param element
52      */
53     public void listAttribute(Element element){
54         @SuppressWarnings("unchecked")
55         List<Attribute> attributes=element.attributes();
56         if(attributes.size()==0)
57             return ;
58         else {
59             for(int i=0;i<attributes.size();i++){
60                 System.out.print("   "+attributes.get(i).getName()+"   "+attributes.get(i).getValue());
61             
62             }
63         }
64         
65     }
66 }

 

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 import javax.xml.parsers.DocumentBuilder;
 5 import javax.xml.parsers.DocumentBuilderFactory;
 6 import javax.xml.parsers.ParserConfigurationException;
 7 
 8 import org.w3c.dom.Document;
 9 import org.w3c.dom.Element;
10 import org.w3c.dom.NamedNodeMap;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13 import org.xml.sax.SAXException;
14 
15 
16 /**
17  * dom解析
18  * 将xml内容一次性全部载入,组成一个树,然后读取各个节点的信息
19  * 在小型xml文档中较为出色,但对于大型xml文档,由于每次都要全部载入,所以相对比较消耗
20 
21  *
22  */
23 public class DomParseXml {
24     public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
25         DomParseXml domParseXml=new DomParseXml();
26         DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
27         DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
28         Document document=documentBuilder.parse("c:"+File.separator+"test.txt");
29         //获取根节点
30         Element rootElement=document.getDocumentElement();
31         NodeList nodeList=rootElement.getChildNodes();
32         for(int i=0;i<nodeList.getLength();i++){
33             /*
34              * dom解析xml文档时,会涵盖空格等节点,#text的字段常量值为3(TEXT_NODE)
35              */
36             if(nodeList.item(i).getNodeType()!=Node.TEXT_NODE){
37                 System.out.println(nodeList.item(i).getNodeName());
38                 domParseXml.listNode(nodeList.item(i));
39                 System.out.println(nodeList.item(i).getNodeName());
40                 
41             }
42         }
43     
44     }
45     
46     public void listNode(Node node){
47         NodeList nodeList=node.getChildNodes();
48         if(nodeList.getLength()==0){
49             if(node.getNodeType()!=Node.TEXT_NODE){
50                 System.out.print(node.getNodeName()+"  ");
51                 listAttr(node);
52             
53             }
54         }else{
55             for(int i=0;i<nodeList.getLength();i++)
56                 listNode(nodeList.item(i));
57         }
58     }
59     
60     public void listAttr(Node node){
61        NamedNodeMap namedNodeMap= node.getAttributes();
62        if(null!=namedNodeMap&&namedNodeMap.getLength()!=0){
63           for(int i=0;i<namedNodeMap.getLength();i++){
64               System.out.println(namedNodeMap.item(i).getNodeName()+"  "+namedNodeMap.item(i).getNodeValue());
65           }
66        }
67     }
68     
69 }

 

 1 import java.io.File;
 2 import java.io.IOException;
 3 
 4 import javax.xml.parsers.ParserConfigurationException;
 5 import javax.xml.parsers.SAXParser;
 6 import javax.xml.parsers.SAXParserFactory;
 7 
 8 import org.xml.sax.Attributes;
 9 import org.xml.sax.SAXException;
10 import org.xml.sax.helpers.DefaultHandler;
11 
12 /**
13  * 以事件方式驱动解析过程,相对性能比较高,但是在解析的过程中不会保留结构信息
14  * 但是注意,在sax解析过程中,空白节点也会作为事件而驱动处理器
15  * @author Administrator
16  *
17  */
18 
19 
20 public class SaxHandle extends DefaultHandler {
21 
22     private String previousString=null;
23     public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
24         SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
25         SAXParser saxParser=saxParserFactory.newSAXParser();
26         File file=new File("c:"+File.separator+"test.txt");
27         saxParser.parse(file, new SaxHandle());
28         
29     }
30     @Override
31     public void startDocument() throws SAXException {
32         System.out.println("解析开始");
33         
34     }
35 
36     @Override
37     public void endDocument() throws SAXException {
38         System.out.println("解析结束");
39     }
40 
41     public void startElement(String uri, String localName, String qName,
42             Attributes attributes) throws SAXException {
43     
44         previousString=qName;
45         for(int i=0;i<attributes.getLength();i++){
46         
47             System.out.println(attributes.getQName(i)+" "+attributes.getValue(i));
48         }
49          
50     }
51 
52     @Override
53     public void endElement(String uri, String localName, String qName)
54             throws SAXException {
55     }
56 
57     @Override
58     public void characters(char[] ch, int start, int length)
59             throws SAXException {
60         if (previousString!=null) {
61             if(previousString.equals("name")||previousString.equals("sex")){
62                 System.out.println(new String(ch,start,length));
63                 previousString=null;
64             }
65         }
66 
67     }
68         
69 }

 

posted on 2014-01-17 16:21  遠樹  阅读(192)  评论(0)    收藏  举报

导航