如果你使用Java,基于事件的流式处理方式应该更适合你。示例代码:
try {
URL url = new URL("
http://www.meituan.com/api/v2/beijing/deals");
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(60000);
InputStream is = connection.getInputStream();
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader xmlEventReader = factory.createXMLEventReader(new BufferedInputStream(is));
while (xmlEventReader.hasNext()) {
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if (xmlEvent.isStartElement()) {
StartElement startElement = xmlEvent.asStartElement();
System.out.println(startElement.getName().getLocalPart());
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
} catch (IOException e) {
System.err.println(e.getMessage());
}