zoukankan      html  css  js  c++  java
  • DOM与SAX读取xml文件例程

    以如下xml为例

     <books> 
       <book id="001"> 
          <title>Harry Potter</title> 
          <author>afd</author> 
       </book> 
       <book id="002"> 
          <title>Learning XML</title> 
          <author>daf</author> 
       </book> 
     </books> 

    Dom4j读取信息源程序

    import java.io.File; 
     import java.io.IOException; 
     import javax.xml.parsers.DocumentBuilder; 
     import javax.xml.parsers.DocumentBuilderFactory; 
     import javax.xml.parsers.ParserConfigurationException; 
     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; 
    
     public class DOMParser { 
       DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 
       //Load and parse XML file into DOM 
       public Document parse(String filePath) { 
          Document document = null; 
          try { 
             //DOM parser instance 
             DocumentBuilder builder = builderFactory.newDocumentBuilder(); 
             //parse an XML file into a DOM tree 
             document = builder.parse(new File(filePath)); 
          } catch (ParserConfigurationException e) { 
             e.printStackTrace();  
          } catch (SAXException e) { 
             e.printStackTrace(); 
          } catch (IOException e) { 
             e.printStackTrace(); 
          } 
          return document; 
       } 
        
       public static void main(String[] args) { 
             DOMParser parser = new DOMParser(); 
             Document document = parser.parse("books.xml"); 
             //get root element 
             Element rootElement = document.getDocumentElement(); 
    
             //traverse child elements 
             NodeList nodes = rootElement.getChildNodes(); 
             for (int i=0; i < nodes.getLength(); i++) 
             { 
                Node node = nodes.item(i); 
                if (node.getNodeType() == Node.ELEMENT_NODE) {   
                   Element child = (Element) node; 
                   //process child element 
                  System.out.println( child.getAttribute("id"));              
                 Node eleTitle = child.getElementsByTagName("title").item(0);
                 Node eleAuthor = child.getElementsByTagName("author").item(0);
                 String title = eleTitle.getFirstChild().getNodeValue();
                 String author = eleAuthor.getFirstChild().getNodeValue();
                 System.out.println(title  + "---" + author);
                } 
             }       
       } 
     } 

    显示结果

    001
    Harry Potter---afd
    002
    Learning XML---daf

    SAX方法例程

     import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.xml.sax.Attributes; 
     import org.xml.sax.SAXException; 
     import org.xml.sax.XMLReader; 
     import org.xml.sax.helpers.DefaultHandler; 
    import org.xml.sax.helpers.XMLReaderFactory; 
    
     public class SAXParser { 
    
       class BookHandler extends DefaultHandler { 
          private List<String> nameList; 
          private boolean title = false; 
       
          public List<String> getNameList() { 
             return nameList; 
          } 
          // Called at start of an XML document 
          @Override 
          public void startDocument() throws SAXException { 
             System.out.println("Start parsing document..."); 
             nameList = new ArrayList<String>(); 
          } 
          // Called at end of an XML document 
          @Override 
          public void endDocument() throws SAXException {  
             System.out.println("End");  
          } 
          
          /** 
           * Start processing of an element. 
           * @param namespaceURI  Namespace URI 
           * @param localName  The local name, without prefix 
           * @param qName  The qualified name, with prefix 
           * @param atts  The attributes of the element 
           */ 
          @Override 
          public void startElement(String uri, String localName, String qName, 
             Attributes atts) throws SAXException { 
             // Using qualified name because we are not using xmlns prefixes here. 
             if (qName.equals("title")) { 
                title = true; 
             } 
          } 
       
          @Override 
          public void endElement(String namespaceURI, String localName, String qName) 
             throws SAXException { 
             // End of processing current element 
             if (title) { 
                title = false; 
             } 
          } 
                   
          @Override 
          public void characters(char[] ch, int start, int length) { 
             // Processing character data inside an element 
             if (title) { 
                String bookTitle = new String(ch, start, length); 
                System.out.println("Book title: " + bookTitle); 
                nameList.add(bookTitle); 
             }
          }             
       } 
        
       public static void main(String[] args) throws SAXException, IOException { 
          XMLReader parser = XMLReaderFactory.createXMLReader(); 
          BookHandler bookHandler = (new SAXParser()).new BookHandler(); 
          parser.setContentHandler(bookHandler); 
          parser.parse("books.xml"); 
          System.out.println(bookHandler.getNameList()); 
       } 
     } 
        
  • 相关阅读:
    phonegap
    iOS8以前与iOS8使用CoreLocation定位
    phonegap调用摄像头
    js与nativede 通信
    大数据基础---Spring+Mybatis+Phoenix整合
    大数据基础---Hbase的SQL中间层_Phoenix
    大数据基础---Hbase容灾与备份
    大数据基础---Hbase协处理器详解
    大数据基础---Hbase 过滤器详解
    大数据基础---Hive数据查询详解
  • 原文地址:https://www.cnblogs.com/vigarbuaa/p/2684163.html
Copyright © 2011-2022 走看看