Java Reference
In-Depth Information
r += "* P/E: " + pe + "\r\n";
return r;
}
}
2.
You need a way to instantiate a Stock object based on the response you receive from the web
service. There are multiple ways to approach this. You can create an additional constructor, add
a static method (e.g., createStockFromXML ) to the Stock class, or abstract out this functionality
using a separate class. In this exercise the last method is used, to keep your Stock class as clean as
possible and to add a layer of abstraction between domain logic and web-service-oriented logic. As
such, create a StockFactory class that will just contain some static methods to help instantiate a
Stock object using a received SOAP reply:
package withoutwsdlobjectoriented;
import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import java.util.HashSet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class StockFactory {
public static Set<Stock> newStocksFromSOAPReplyMessage(SOAPMessage soapResponse) {
try {
Node responseNode = soapResponse.getSOAPBody().getFirstChild();
String xmlText = responseNode.getTextContent();
return newStocksFromXMLString(xmlText);
} catch (SOAPException e) {
e.printStackTrace();
}
// If we end up here, something went wrong:
return null;
}
public static Set<Stock> newStocksFromXMLString(String xmlText) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Search WWH ::




Custom Search