利用SAX解析XML Document(实例解析http://api.google.com/GoogleSearch.wsdl)
来源:互联网 发布:mac装虚拟机好不好 编辑:程序博客网 时间:2024/04/29 18:10
最近在学习 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();
- }
- }
- }
- 利用SAX解析XML Document(实例解析http://api.google.com/GoogleSearch.wsdl)
- 利用SAX解析XML
- sax解析xml实例
- Xml-Sax解析实例
- sax 解析xml实例
- SAX解析XML实例
- 利用SAX解析xml文件
- jaxp利用Sax解析XML
- SAX实战---利用SAX解析XML帮助文件
- SAX PULL DOCUMENT解析XML总结
- 使用SAX解析XML实例
- 用SAX解析XML实例
- SAX解析xml文件实例
- SAX解析xml简单实例
- SAX解析xml文件实例
- sax解析xml简单实例
- sax解析xml实例二
- 利用sax解析器解析xml文件
- 平面设计小知识
- JSON介绍
- 设计工作经验分享
- prototype-Ajax.request的使用
- 灰平衡
- 利用SAX解析XML Document(实例解析http://api.google.com/GoogleSearch.wsdl)
- 软件界面设计浅谈
- SQL Server 2000优化之高级篇
- 设计专家 网站设计必知的65条原则
- commons-fileupload在web.xml文件中的配置
- 创意设计手法14点
- 技术路线的选择重要但不具有决定性
- new与delete
- 设计界面说—探讨设计艺术