1.SAX解析,是一行一行的把xml文件读到内存,通过是时间通知的方式来解析的,有点比较省内存,适合与移动开发。
package com.hw;
import java.io.StringWriter;
import java.util.LinkedList;
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 SAXhw extends DefaultHandler {
private static final String _MY_MESSAGE_NAMESPACE_URI = "http://schemas.xmlsoap.org/wsdl/";
private static final String _MESSAGE_ELEMENT_NAME = "message";
private static final String _OPERATION_BINDING_NAME = "operation";
private static final String _BINDING_NAME = "binding";
private boolean _messageFound = false;
private boolean _operationFound = false;
private boolean _bindingFound = false;
private boolean _bindingIsOver = true;
private LinkedList _messages = new LinkedList();
private LinkedList _operationName = new LinkedList();
public void startElement(String namespaceURI,
String localName,
String qualifiedName,
Attributes attributes)
throws SAXException
{
_messageFound = namespaceURI.toLowerCase().equals(
_MY_MESSAGE_NAMESPACE_URI)
&&
localName.toLowerCase().equals(_MESSAGE_ELEMENT_NAME);
if (_messageFound) {
_messages.add(attributes.getValue(0).toString());
}
//The work 2:
_bindingFound = namespaceURI.toLowerCase().equals(
_MY_MESSAGE_NAMESPACE_URI)
&&
localName.toLowerCase().equals(_BINDING_NAME);
if (_bindingFound) {
_bindingIsOver = false;
}
_operationFound = namespaceURI.toLowerCase().equals(
_MY_MESSAGE_NAMESPACE_URI)
&&
localName.toLowerCase().equals(_OPERATION_BINDING_NAME);
if (_operationFound && !_bindingIsOver) {
_operationName.add(attributes.getValue(0));
}
}
public void endElement(String namespaceURI, String localName, String qualifiedName)
throws SAXException
{
_bindingFound = namespaceURI.toLowerCase().equals(
_MY_MESSAGE_NAMESPACE_URI)
&&
localName.toLowerCase().equals(_BINDING_NAME);
if (_bindingFound) {
_bindingIsOver = true;
}
_messageFound = false;
_bindingFound = false;
_operationFound = false;
}
public StringWriter outputAllNames()
{
StringWriter sw = new StringWriter();
sw.write("The all message names:\n");
for (int i = 0; i < _messages.size(); i++){
sw.write((i + 1) + ":" + (String) _messages.get(i) + "\n");
}
return sw;
}
public StringWriter outputOperationNames() {
StringWriter sw = new StringWriter();
sw.write("The all operation names of binding element:\n");
for (int i = 0; i < _operationName.size(); i++) {
sw.write((i + 1) + ":" + (String) _operationName.get(i) + "\n");
}
return sw;
}
public static void main(String[] args) throws Exception
{
SAXhw saxHx = new SAXhw();
XMLReader parser = null;
try
{
parser = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");
parser.setContentHandler(saxHx);
}
catch (Exception e)
{
System.err.println("Unable to create Xerces SAX parser - check classpath");
}
try
{
parser.parse("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
System.out.println(saxHx.outputAllNames().toString());
System.out.println(saxHx.outputOperationNames().toString());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
2.DOM解析方式,它是把xml组成一个树结构,全部读入内存。比SAX内存消耗大
using System;
using System.Xml;
public class DOMExample
{
private string getXMLDocument(string url)
{
System.Net.WebClient wc = new System.Net.WebClient();
byte[] webData = wc.DownloadData(url);
char[] charData = new char[webData.Length];
for (int i = 0; i < charData.Length; i++)
{
charData[i] = (char)webData[i];
}
string xmlStr = new String(charData);
int start = xmlStr.IndexOf("<", 0, xmlStr.Length - 1);
int length = xmlStr.LastIndexOf(">") - start + 1;
return xmlStr.Substring(start, length);
}
public static void Main(string[] args)
{
String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";
DOMExample domExample = new DOMExample();
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(domExample.getXMLDocument(url));
XmlNodeList xmlNodeList = xmlDoc.getElementsByTagName_r("message", "http://schemas.xmlsoap.org/wsdl/");
XmlNodeList xmlBindingList = xmlDoc.getElementsByTagName_r("binding", "http://schemas.xmlsoap.org/wsdl/");
XmlNodeList xmlOperationList;
//The work 1:
Console.Write("The message names is :\n");
for (int i = 0; i < xmlNodeList.Count; i++)
{
Console.WriteLine(xmlNodeList.Item(i).Attributes.Item(0).Value);
}
//The work 2:
Console.Write("The
operation names of binding element:\n");
for (int i = 0; i < xmlBindingList.Count; i++)
{
Console.WriteLine("binding:"+ xmlBindingList.Item(i).Attributes.Item(0).Value);
xmlOperationList = xmlBindingList.Item(i).ChildNodes;
for (int j = 0; j < xmlOperationList.Count; j++)
{
if (xmlOperationList.Item(j).Name.Equals("wsdl:operation".ToString()))
{
Console.WriteLine(xmlOperationList.Item(j).Attributes.Item(0).Value);
}
}
}
Console.ReadKey();
}
}
3.XSLT:
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="wsdl">
"wsdl:definitions/wsdl:binding/wsdl:operation">
package com.hw;
import java.io.StringWriter;
import java.util.LinkedList;
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 SAXhw extends DefaultHandler {
}
2.DOM解析方式,它是把xml组成一个树结构,全部读入内存。比SAX内存消耗大
using System;
using System.Xml;
public class DOMExample
{
}
3.XSLT:
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"