?? soapresponse.java
字號(hào):
import javax.xml.parsers.*;
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import java.io.*;
public class SOAPResponse extends DefaultHandler {
//Holds the HTML that we are authoring.
private String html = "<html>" + "<body bgcolor=\"whitesmoke \">" +
"<p align=\"center \">";
//These flags will be set when
//corresponding events are found.
private boolean body = false;
private boolean method = false;
private boolean value = false;
//Constructor takes a File object (XML file).
//It creates a SAXParserFactory.
//Then creates a SAXParser on the Factory and
//parses the input File object.
public SOAPResponse(File file) {
//Calling parser to parse the xml file.
try {
SAXParserFactory parserfactory = SAXParserFactory.newInstance();
SAXParser parser = parserfactory.newSAXParser();
parser.parse(file, this);
} //try
catch (Exception e) {
System.out.println(e.getMessage());
} //catch
} //Constructor
public void startElement(String uri, String localName, String qName,
Attributes attributes) {
if (localName.equals("Body ")) {
//We 抳e found SOAP Body.
body = true;
} //if
else if (body && !method && !value) {
//We 抳e found SOAP Method.
html += ("<b>Method Name:" + localName + "</b>");
method = true;
} //else if
else if (method && !value) {
//We can expect the value that we were looking for.
html += "<table>";
value = true;
//The character event will actually work
//after we 抳e set the value equal to true.
} //else if
} //startElement
public void characters(char[] ch, int start, int length) {
String tstr = new String(ch, start, length);
String str = "";
//Ignore everything except integers and alphabets.
for (int i = 0; i < tstr.length(); i++) {
if ((tstr.charAt(i) >= '0' && tstr.charAt(i) <= '9') ||
(tstr.charAt(i) >= 'a' && tstr.charAt(i) <= 'z') ||
(tstr.charAt(i) >= 'A' && tstr.charAt(i) <= 'Z')) {
str += tstr.charAt(i);
} //if
} //for
if (value && (str.length() > 0)) {
//Value flag was set in startElement event handler.
//Now is the time to read contents of <return>tag.
html += ("<tr>" + "<td align=\"right \">Freight:</td>" + "<td>" + str + "</td>" + "</tr>");
value = false; //don 't read again.
} //if
} //characters
public void printHTML(File file) {
try {
FileWriter fw = new FileWriter(file);
fw.write(html, 0, html.length());
fw.close();
} //try
catch (Exception e) {
System.out.println("File Write Error....");
} //catch
System.out.println(html);
} //printHtml
public void endDocument() {
html += ("</table>" + "</p>" + "</body>" + "</html>");
} //endDocument
public static void main(String[] arg) {
String fileName = "SOAPResponse";
SOAPResponse sr = new SOAPResponse(new File(fileName + ".xml "));
sr.printHTML(new File(fileName + ".html "));
} //main
} //class
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -