📄 weatherextractor.java~14~
字号:
import java.util.*;
import java.io.*;
import java.sql.Timestamp;
import org.w3c.dom.*;
/**
* The WeatherExtractor class extracts weather information from
* a web page and saves it in XML format. If a weather XML file
* all ready exists, the data is merged with that file, otherwise
* a new XML data file is created
*/
public class WeatherExtractor {
// The location of the various resources and files
private static final String source =
"http://weather.yahoo.com/forecast/UKXX0085_c.html?force_units=1";
private static final String xslFile = "new-xml/london.xsl";
private static final String xmlFile = "new-xml/london-result.xml";
private static Calendar date = Calendar.getInstance();
public static void main(String args[]) {
try {
// Retrieve the source document as XML
Document xhtml = XMLHelper.tidyHTML(source);
// Parse the XSL document
Document xsl = XMLHelper.parseXMLFromFile(xslFile);
// Get the current data by applying the XSL transform
Document data = XMLHelper.transformXML(xhtml, xsl);
// For convenience, add a timestamp to what we have done
//Timestamp time = new Timestamp(now.getTime());
Element root = data.getDocumentElement();
NodeList nl = root.getElementsByTagName("WEATHER");
Element weather = (Element)nl.item(0);
int year=2003;
int month=date.getTime().getMonth()+1;
int day=date.getTime().getDate();
int hours=date.getTime().getHours()-7;
//System.out.println("hours= "+hours);
//System.out.println("date="+date);
if (hours<0){
day=day-1;
//hours=date.getTime().getHours()+24-7;
}
String calendar=year+"-"+month+"-"+day;
weather.setAttribute("Retreived",calendar);
String value="";
System.out.print("length="+weather.getChildNodes().getLength());
for(int i=0;i<weather.getChildNodes().getLength();i++){
value=weather.getChildNodes().item(i).getChildNodes().item(0).getNodeValue();
weather.getChildNodes().item(i).getChildNodes().item(0).setNodeValue(value.trim());
//System.out.println("value="+value);
}
String time=weather.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
if(time.endsWith("pm")){
System.out.print("this is pm");
}else{
System.out.println("this is am ,time="+time.substring(0,2));
//char hour=time.substring(0,1);
//time.replace(time.substring(0,1).toCharArray(),(time.substring(0,1)+12).toCharArray());
}
//System.out.println("date="+(day.getTime().getMonth()+1)+"-"+day.getTime().getDate());
// 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 = XMLHelper.parseXMLFromFile(dataFile);
XMLHelper.mergeXML(oldData.getDocumentElement(), weather, false);
XMLHelper.outputXMLToFile(oldData, xmlFile);
} else {
// If this is our first extraction, just write the file
XMLHelper.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 + -