⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 countxmlelementsusingsax.java

📁 Beginning Java 2, SDK 1.4 Edition Exercise Code samples for this book
💻 JAVA
字号:
// Chapter 21 Exercise 1
// Use a SAX parser to count XML elements
/*
  The inner ElementCounterclass  defines a SAX handler that counts element. For each
  type of element and ElementRecord object is created that records the count for that type.
  ElementRecord objects are stored in the elementRecords field of the ElementCounter object.
  The ElementRecord class is an inner class to the ElementCounter class. 
*/
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.Iterator;

public class CountXMLElementsUsingSAX {
  public static void main(String args[]) {
    if(args.length == 0) {
      System.out.println("No file to process. Usage is:"
                                                 +"\njava CountXMLElements \"filename\" ");
      return;
    }
    File file = new File(args[0]);
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = null;
    spf.setNamespaceAware(true);
    spf.setValidating(true);

    try {
     parser = spf.newSAXParser();
     System.out.println("Parser object is: "+ parser);
    } catch(SAXException e) {
      e.printStackTrace(System.err);
      System.exit(1);    
    } catch(ParserConfigurationException e) {
      e.printStackTrace(System.err);
      System.exit(1);    
    }

    System.out.println("\nStarting processing of "+file+"\n");
    ElementCounter elementCounter = new ElementCounter(); 
    try {
       parser.parse(file, elementCounter);
    }  catch(IOException e) {
      e.printStackTrace(System.err);
    }
    catch(SAXException e) {
      e.printStackTrace(System.err);
    }
    elementCounter.listElementCounts();
  }

  // Defines a SAX handler to count elements
  static class ElementCounter extends DefaultHandler {
    public void startDocument() {
      System.out.println("Starting document processing... ");
    }    

    public void endDocument()  {
      System.out.println("Document processing finished. ");
    }
  
    public void startElement(String uri, String localName, String qname, 
                                                               Attributes attr) {
      ElementRecord element = findElement(localName);
      if(element == null)
        elementRecords.add(new ElementRecord(localName));
      else
        element.increment();
    }
  
    private ElementRecord findElement(String name) {
      ElementRecord element = null;
      Iterator iter = elementRecords.iterator();
      while(iter.hasNext()) {
        element = (ElementRecord)(iter.next());
        if(name.equals(element.getName()))
          return element;
      }
      return null;
    }
    
    public void listElementCounts() {
    Iterator iterator = elementRecords.iterator();
    while(iterator.hasNext())
      System.out.println((ElementRecord)(iterator.next()));
    }
  
    public void ignorableWhitespace(char[] ch, int start, int length) {
      // Uncomment below to see that this is happening
      // System.out.println("Ignoring ignorable whitespace... ");
    }

    // Defines an object recording the count for an element
    class ElementRecord {
      public ElementRecord(String name) {
        this.name = name;
      }

      public void increment() {
        ++count;
      }

      public int getCount() {
        return count;
      }
    
      public String getName() {
        return name;
      }
    
      public String toString() {
        return name + " " + count;
      }
          
      private String name = null;
      private int count = 1;    
    }

    private Vector elementRecords = new Vector();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -