我写了一些小代码来从URL响应中读取XML,如下所示:
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class example{
private static String url="http://www.example.com/";
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());
}
}
问题在于:它会产生很多错误,例如,它说
Exception in thread "main" java.net.SocketException: Connection reset
或其他东西,基于网站。我试过但它对我迄今为止尝试的任何网站都没有用。任何解决方案
答案 0 :(得分:0)
URL应指向文档或文件以将其作为流读取。 例如URL:http://www.example.com/products/downloads/information.xml