利用SAX解析XML Document(实例解析http://api.google.com/GoogleSearch.wsdl)

来源:互联网 发布:mac装虚拟机好不好 编辑:程序博客网 时间:2024/04/29 18:10

最近在学习 Web Services , 这里介绍一个 利用SAX解析XML Document 的例子,用JAVA编写,可以直接运行的

 

  1. import java.io.StringWriter;
  2. import java.util.LinkedList;
  3. import javax.xml.parsers.SAXParser;
  4. import javax.xml.parsers.SAXParserFactory;
  5. import org.xml.sax.Attributes;
  6. import org.xml.sax.InputSource;
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.helpers.DefaultHandler;
  9. /*
  10.  * Created on 2008-10-28
  11.  *
  12.  * TODO To change the template for this generated file go to
  13.  * Window - Preferences - Java - Code Style - Code Templates
  14.  */
  15. /**
  16.  * @author Administrator
  17.  * 
  18.  * TODO To change the template for this generated type comment go to Window -
  19.  * Preferences - Java - Code Style - Code Templates
  20.  */
  21. public class ParseGoogleSearchWDSLusingSAX extends DefaultHandler {
  22.     // Constants
  23.     private static final String _CHARACTER_ELEMENT_NAME = "message";
  24.     private static final String _BINDING_ELEMENT_NAME = "binding";
  25.     
  26.     private static final String _URL = "http://api.google.com/GoogleSearch.wsdl";
  27.     // Flag to remember if we are dealing with character data while parsing
  28.     private boolean _messageFound = false;
  29.     private boolean _bindingFound = false;
  30.     // The data we're looking for in the document
  31.     private LinkedList _messageNames = new LinkedList();
  32.     private LinkedList _operationNames = new LinkedList();
  33.     /**
  34.      * 
  35.      * The method called when the start of a new element is
  36.      * 
  37.      * found.
  38.      *  
  39.      */
  40.     public void startElement(String namespaceURI,
  41.     String localName,
  42.     String qualifiedName,
  43.     Attributes attributes)
  44.     throws SAXException
  45.     {
  46.         _messageFound = qualifiedName.equals(_CHARACTER_ELEMENT_NAME);
  47.         if (!_bindingFound) {
  48.             _bindingFound = qualifiedName.equals(_BINDING_ELEMENT_NAME);
  49.         }
  50.         if (_messageFound) {
  51.             _messageNames.add(new String(attributes.getValue("name")));
  52.         }
  53.         
  54.         if (_bindingFound && qualifiedName.equals("operation")) {
  55.             _operationNames.add(new String(attributes.getValue("name")));
  56.         }
  57.     }
  58.     /**
  59.      * 
  60.      * The method called when the end of an element is found.
  61.      *  
  62.      */
  63.     public void endElement(String namespaceURI,
  64.     String localName,
  65.     String qualifiedName)
  66.     throws SAXException
  67.     {
  68.         _messageFound = false;
  69.         
  70.         if(qualifiedName.equals("binding")){_bindingFound=false;}
  71.     }
  72.     
  73.     /**
  74.      * 
  75.      * A convenience method to pretty-print the characters
  76.      * 
  77.      * found.
  78.      *  
  79.      */
  80.     public StringWriter outputCharacters()
  81.     {
  82.         StringWriter sw = new StringWriter();
  83.         sw.write("Output result 1:There are " + _messageNames.size() + " message names :/n");
  84.         sw.write("--------------------------------/n");
  85.         for (int i = 0; i < _messageNames.size(); i++)
  86.         {
  87.             sw.write(i + 1 + " | " + (String) _messageNames.get(i) + "/n");
  88.         }
  89.         sw.write("--------------------------------/n");
  90.         sw.write("/n");
  91.         
  92.         sw.write("Output result 2:There are " + _operationNames.size()
  93.                 + " operation names of binding element :/n");
  94.         sw.write("--------------------------------/n");
  95.         for (int i = 0; i < _operationNames.size(); i++)
  96.         {
  97.             sw.write(i + 1 + " | " + (String) _operationNames.get(i) + "/n");
  98.         }
  99.         return sw;
  100.     }
  101.     /**
  102.      * 
  103.      * mian method
  104.      *  
  105.      */
  106.     public static void main(String[] args) {
  107.         try
  108.         {
  109.             ParseGoogleSearchWDSLusingSAX swp = new          ParseGoogleSearchWDSLusingSAX();
  110.             SAXParserFactory sf = SAXParserFactory.newInstance();
  111.             SAXParser sp = sf.newSAXParser();
  112.             
  113.             //URL refers to the location on the web 
  114.             sp.parse(_URL, swp);
  115.             
  116.             /**an optional way which choose a file in local disk
  117.              * this is for test
  118.             */
  119.             //sp.parse(new InputSource("D:/google.xml"), swp);
  120.             
  121.             // Dump the character information to screen.
  122.             System.out.println(swp.outputCharacters().toString());
  123.         }
  124.         catch (Exception e)
  125.         {
  126.             e.printStackTrace();
  127.         }
  128.     }
  129. }

 

原创粉丝点击