⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soapresponse.java

📁 javaP2P技术内幕课程111213141516源代码
💻 JAVA
字号:
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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -