📄 qiulin.java~5~
字号:
import java.util.*;import java.io.*;import java.sql.Timestamp;import org.w3c.dom.*;public class Qiulin { private static final String source = "http://stock.sina.com.cn/cgi-bin/stock/quote/quote.cgi?symbol=600891&country=sh"; private static final String xslFile = "new-xml/600891.xsl"; private static final String xmlFile = "new-xml/600891-result.xml"; public Qiulin(){ //public static void main(String args[]) { try { // Retrieve the source document as XML Document xhtml = StockHelper.tidyHTML(source); // Parse the XSL document Document xsl = StockHelper.parseXMLFromFile(xslFile); // Get the current data by applying the XSL transform Document data = StockHelper.transformXML(xhtml, xsl); // For convenience, add a timestamp to what we have done Date now = new Date(); Timestamp time = new Timestamp(now.getTime()); Element root = data.getDocumentElement(); NodeList nl = root.getElementsByTagName("stock"); Element stock = (Element)nl.item(0); for(int i=0;i<stock.getChildNodes().getLength();i++){ String value=stock.getChildNodes().item(i).getChildNodes().item(0).getNodeValue(); stock.getChildNodes().item(i).getChildNodes().item(0).setNodeValue(value.trim()); } String newtime=stock.getChildNodes().item(0).getChildNodes().item(0).getNodeValue(); System.out.println("new time="+newtime); //Write the output file, merging if necessary File dataFile = new File(xmlFile); if (dataFile.exists()) { //If we have extracted before, merge the data and write the file Document oldData = StockHelper.parseXMLFromFile(dataFile); StockHelper.mergeXML(oldData.getDocumentElement(), stock, false); StockHelper.outputXMLToFile(oldData, xmlFile); Element oldroot = oldData.getDocumentElement(); NodeList oldnl = oldroot.getElementsByTagName("stock"); Element oldstock = (Element)oldnl.item(0); //String firsttime=oldstock.getElementsByTagName("time").item(0).getChildNodes().item(0).getNodeValue(); //System.out.println("first time="+firsttime); int len=oldroot.getChildNodes().getLength(); String lasttime=oldroot.getChildNodes().item(len-2).getChildNodes().item(1).getChildNodes().item(0).getNodeValue(); System.out.println("last time="+lasttime); if(newtime.equals(lasttime)){ System.out.println("do nothing !"); } else{ StockHelper.mergeXML(oldData.getDocumentElement(), stock, false); StockHelper.outputXMLToFile(oldData, xmlFile); } //System.out.print(stock.getChildNodes().item(0).getChildNodes().item(0).getNodeValue()); } else { // If this is our first extraction, just write the file StockHelper.outputXMLToFile(data, xmlFile); } } catch (XMLHelperException xmlhe) { System.err.println( "There was an error in the extraction process:\n" + xmlhe.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -