import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Node; import org.w3c.dom.Document;/** * A factory used to craete Stock objects, in this case from an XML document. * * @author Simon Brown */ public class StockFactory { /** the singleton instance */ private static final StockFactory instance = new StockFactory(); /** * Private constructor for the singleton pattern. */ private StockFactory() { } /** * Gets the singleton instance. * * @return the singleton StockFactory instance */ public static StockFactory getInstance() { return instance; } /** * Parses the XML document provided by the InputStream into a Stock instance. * * @param in the InputStream from which to read the document * @return a Stock instance * @throws Exception should anything go wrong in parsing the document */ public Stock getStock(InputStream in) throws StockException { Stock stock; try { // create a factory and builder - an abstraction for an XML parser DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // get a reference to the document Document doc = builder.parse(in); Node root = doc.getDocumentElement(); Node node; String symbol; float price; float change; Date date; // and now traverse the document for the required information // (not forgetting to skip over whitespace in the document) node = root.getFirstChild(); node = node.getNextSibling(); symbol = node.getFirstChild().getNodeValue(); node = node.getNextSibling(); node = node.getNextSibling(); price = Float.parseFloat(node.getFirstChild().getNodeValue()); node = node.getNextSibling(); node = node.getNextSibling(); change = Float.parseFloat(node.getFirstChild().getNodeValue()); SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); node = node.getNextSibling(); node = node.getNextSibling(); date = sdf.parse(node.getFirstChild().getNodeValue()); return new Stock(symbol, price, change, date); } catch (Exception e) { throw new StockException("Could not parse stock from XML document."); } }} 用xml解析器, URL url = new URL(url); InputStream in = url.openStream(); 将得到的in放入以上的代码!
import java.text.SimpleDateFormat;
import java.util.Date;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Node;
import org.w3c.dom.Document;/**
* A factory used to craete Stock objects, in this case from an XML document.
*
* @author Simon Brown
*/
public class StockFactory { /** the singleton instance */
private static final StockFactory instance = new StockFactory(); /**
* Private constructor for the singleton pattern.
*/
private StockFactory() {
} /**
* Gets the singleton instance.
*
* @return the singleton StockFactory instance
*/
public static StockFactory getInstance() {
return instance;
} /**
* Parses the XML document provided by the InputStream into a Stock instance.
*
* @param in the InputStream from which to read the document
* @return a Stock instance
* @throws Exception should anything go wrong in parsing the document
*/
public Stock getStock(InputStream in) throws StockException { Stock stock; try {
// create a factory and builder - an abstraction for an XML parser
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); // get a reference to the document
Document doc = builder.parse(in);
Node root = doc.getDocumentElement();
Node node; String symbol;
float price;
float change;
Date date; // and now traverse the document for the required information
// (not forgetting to skip over whitespace in the document)
node = root.getFirstChild();
node = node.getNextSibling();
symbol = node.getFirstChild().getNodeValue(); node = node.getNextSibling();
node = node.getNextSibling();
price = Float.parseFloat(node.getFirstChild().getNodeValue()); node = node.getNextSibling();
node = node.getNextSibling();
change = Float.parseFloat(node.getFirstChild().getNodeValue()); SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
node = node.getNextSibling();
node = node.getNextSibling();
date = sdf.parse(node.getFirstChild().getNodeValue()); return new Stock(symbol, price, change, date);
} catch (Exception e) {
throw new StockException("Could not parse stock from XML document.");
}
}}
用xml解析器, URL url = new URL(url);
InputStream in = url.openStream();
将得到的in放入以上的代码!
不行就用apache soap里面提供一些http的类库,那个比较高级一点。