来自 url 的 xml 响应
我编写了一些小代码来从 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
根据网站说或其他内容。我尝试过,但到目前为止,它对我尝试过的任何网站都不起作用。有什么解决办法吗?
I wrote some small codes to read XML from a URL response as below:
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());
}
}
the problem lies here: it gives so many errors, for example, it says
Exception in thread "main" java.net.SocketException: Connection reset
or other things , based on the website. i tried but it didnt work for any website i tried so far. any solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
URL 应指向文档或文件以将其作为流读取。
例如 URL:http://www.example.com/products/downloads/information.xml< /a>
the URL should point to a document or a file to read it as a stream.
eg URL: http://www.example.com/products/downloads/information.xml