如果各位不用代理能访问http://weather.unisys.com/
编译通过后在dos下输入 java Weather austin如果有austin的信息告诉我,谢谢!大家也可以试试java Weather shenyang (beijing/qingdao)看看有没有数据import java.io.*;
import java.net.*;
import java.util.*;import javax.xml.parsers.*;
import org.w3c.dom.*;/**
* Use this class to grab online XML weather information.

* @author Dan Becker
* @version 20010822
*
*/
public class Weather {
public static String weatherSite = "http://weather.unisys.com/forexml.cgi?";
public static String observationTagName = "observation";
public static String unknownAttribute = "?";/**
* This method gathers arguments and processes the document.
*/
public static void main( String [] args ) {
if ( args.length == 0) {
System.out.println( "Usage: java Weather city [all|attributes]" );
System.out.println( "Example: java Weather Austin temp.F" );
System.exit( 0 );
}
// Gather arguments
String city = args[ 0 ];
String [] attrNames = null;
if ( args.length == 1) {
attrNames = new String [] { "all" };
} else {
// args.length > 1
attrNames = new String [ args.length - 1 ];
for ( int i = 1; i < args.length; i++)
attrNames[ i - 1 ] = args[ i ];
} // if// Process document
processDocument( city, observationTagName, attrNames );
} // main/** This method uses the default DocumentBuilder
* to fetch an XML document from the a URL.
* The document is parsed and attributes are printed.
*/
public static void processDocument( String city, 
String elementName, String [] attrNames ) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();if ( city != null ) {
String urlString = weatherSite + city;Document document = getDocument( db, urlString );
if ( document != null ) {
Node [] matchNodes = 
getAttributes( document, elementName, attrNames );
if (null != matchNodes ) {
if ( matchNodes.length > 6 )
printNodes( "City=" + city, matchNodes, true );
else
printNodes( "City=" + city, matchNodes, false );
} else
System.out.println( "City=" + city + ", could not get element \"" + elementName + "\" from XML document." );
} else
System.out.println( "No XML document could be created from URL=" + urlString );
} // if 
} catch ( ParserConfigurationException e ) {
e.printStackTrace();
}
} // processDocument/**
* This method gets an XML document from the given URL.
*/
public static Document getDocument( DocumentBuilder db, String urlString ) {
try {
URL url = new URL( urlString );
try {
URLConnection URLconnection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
int responseCode = httpConnection.getResponseCode();
if ( responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
try {
Document doc = db.parse( in );
return doc;
} catch( org.xml.sax.SAXException e ) {
e.printStackTrace();
}
} else
System.out.println( "HTTP connection response != HTTP_OK" );
} catch ( IOException e ) {
e.printStackTrace();
} // catch
} catch ( MalformedURLException e ) {
e.printStackTrace();
} // catch
return null;
} // getDocument/**
* Given an XML document, this method gets the named element
* and gets Nodes matching the given attribute names.
* Attribute names may include "all" or actual names.
* Use getNodeName and getNodeValue to see name and value.
*/
public static Node [] getAttributes( Document document,
String elementName, String [] attrNames ) {// Get elements with the given tag name (matches on * too).
NodeList nodes = document.getElementsByTagName( elementName );
if ( nodes.getLength() < 1) {
// System.out.println( "getAttributeValues, elementName=\"" + elementName + ", no element tags by this name." );
return null;
}Node firstElement = nodes.item( 0 );
NamedNodeMap nnm = firstElement.getAttributes();
if (nnm != null) {
// Report the value of each attribute.
Node [] matchNodes = new Node[ attrNames.length ];
for ( int i = 0; i < attrNames.length; i++ ) {
boolean all = attrNames[ i ].equalsIgnoreCase( "all" );
if (all) {
int nnmLength = nnm.getLength(); // named node map
matchNodes = new Node[ nnmLength ];
for ( int j = 0; j < nnmLength; j++)
matchNodes[ j ] = nnm.item( j );
return matchNodes;
} else {
// Later, may want to match on partially named nodes.
// e.g. let attr name "temp" find "temp.F" and "temp.C"
matchNodes[ i ] = nnm.getNamedItem( attrNames[ i ] );
if ( matchNodes[ i ] == null ) {
matchNodes[ i ] = document.createAttribute( attrNames[ i ] );
((Attr)matchNodes[ i ]).setValue( unknownAttribute );
}
} // if
} // for
return matchNodes;

// else System.out.println( "getAttributeValues, elementName=\"" + elementName + ", attributes for this element are null." );
return null;
} // printDocumentAttrs/**
* Prints a list of node names and values.
* @param title is a descriptive title for the set.
* @param grouped determines whether to print on one line or not.
*/
public static void printNodes( String title, Node [] nodes,
boolean grouped ) {
// Report the name value of each node.
System.out.print( title );
if (grouped) 
System.out.println();
for ( int i = 0; i < nodes.length; i++ ) {
Node node = nodes[ i ];
if ( grouped )
System.out.print( " " );
else 
System.out.print( ", " );
System.out.print( node.getNodeName() + "=" + node.getNodeValue() );
if ( grouped ) System.out.println();
} // for
if (!grouped) 
System.out.println();
} // printAttributeValues} // public class Weather-