public void readFromUrl(String inurl){
try {
URL url = new URL(inurl);
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
// HERE YOU CAN PRINT/LOG/SAVE THE LINE.
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2条答案
按热度按时间goqiplq21#
解析还是打印?
这是基于我对so的微小研究。
解析:
对于inputstream的xml解析,可以执行以下操作:
打印:
如果要将xml直接打印到屏幕上,可以使用
TransformerFactory
```URL url = new URL(urlString);
URLConnection conn = url.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
TransformerFactory factory = TransformerFactory.newInstance();
Transformer xform = factory.newTransformer();
// that’s the default xform; use a stylesheet to get a real one
xform.transform(new DOMSource(doc), new StreamResult(System.out));
jslywgbw2#
以下是我用于此类任务的代码:
附笔
可以使用stringbuilder构建包含xml的字符串。