最近在学习 Web Services , 这里介绍一个 利用SAX解析XML Document 的例子,用JAVA编写,可以直接运行的
- import java.io.StringWriter;
- import java.util.LinkedList;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.Attributes;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- /*
- * Created on 2008-10-28
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- /**
- * @author Administrator
- *
- * TODO To change the template for this generated type comment go to Window -
- * Preferences - Java - Code Style - Code Templates
- */
- public class ParseGoogleSearchWDSLusingSAX extends DefaultHandler {
- // Constants
- private static final String _CHARACTER_ELEMENT_NAME = "message";
- private static final String _BINDING_ELEMENT_NAME = "binding";
- private static final String _URL = "http://api.google.com/GoogleSearch.wsdl";
- // Flag to remember if we are dealing with character data while parsing
- private boolean _messageFound = false;
- private boolean _bindingFound = false;
- // The data we're looking for in the document
- private LinkedList _messageNames = new LinkedList();
- private LinkedList _operationNames = new LinkedList();
- /**
- *
- * The method called when the start of a new element is
- *
- * found.
- *
- */
- public void startElement(String namespaceURI,
- String localName,
- String qualifiedName,
- Attributes attributes)
- throws SAXException
- {
- _messageFound = qualifiedName.equals(_CHARACTER_ELEMENT_NAME);
- if (!_bindingFound) {
- _bindingFound = qualifiedName.equals(_BINDING_ELEMENT_NAME);
- }
- if (_messageFound) {
- _messageNames.add(new String(attributes.getValue("name")));
- }
- if (_bindingFound && qualifiedName.equals("operation")) {
- _operationNames.add(new String(attributes.getValue("name")));
- }
- }
- /**
- *
- * The method called when the end of an element is found.
- *
- */
- public void endElement(String namespaceURI,
- String localName,
- String qualifiedName)
- throws SAXException
- {
- _messageFound = false;
- if(qualifiedName.equals("binding")){_bindingFound=false;}
- }
- /**
- *
- * A convenience method to pretty-print the characters
- *
- * found.
- *
- */
- public StringWriter outputCharacters()
- {
- StringWriter sw = new StringWriter();
- sw.write("Output result 1:There are " + _messageNames.size() + " message names :/n");
- sw.write("--------------------------------/n");
- for (int i = 0; i < _messageNames.size(); i++)
- {
- sw.write(i + 1 + " | " + (String) _messageNames.get(i) + "/n");
- }
- sw.write("--------------------------------/n");
- sw.write("/n");
- sw.write("Output result 2:There are " + _operationNames.size()
- + " operation names of binding element :/n");
- sw.write("--------------------------------/n");
- for (int i = 0; i < _operationNames.size(); i++)
- {
- sw.write(i + 1 + " | " + (String) _operationNames.get(i) + "/n");
- }
- return sw;
- }
- /**
- *
- * mian method
- *
- */
- public static void main(String[] args) {
- try
- {
- ParseGoogleSearchWDSLusingSAX swp = new ParseGoogleSearchWDSLusingSAX();
- SAXParserFactory sf = SAXParserFactory.newInstance();
- SAXParser sp = sf.newSAXParser();
- //URL refers to the location on the web
- sp.parse(_URL, swp);
- /**an optional way which choose a file in local disk
- * this is for test
- */
- //sp.parse(new InputSource("D:/google.xml"), swp);
- // Dump the character information to screen.
- System.out.println(swp.outputCharacters().toString());
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }