Presumably your intent is to do that with Android APIs, so please translate as appropriate.
In plain Java, there is XmlStreamReader
for StAX parsing, and it can be instantiated with an InputStream
from an URLConnnection
, like this:
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class ParseStream {
public static void main(String[] args) throws Exception {
URLConnection connection =
new URL("http://api.androidhive.info/pizza/?format=xml").openConnection();
InputStream inputStream = connection.getInputStream();
XMLStreamReader streamReader =
XMLInputFactory.newInstance().createXMLStreamReader(inputStream, "UTF-8");
while (streamReader.hasNext())
if (streamReader.next() == XMLStreamConstants.START_ELEMENT)
System.out.println("START_ELEMENT " + streamReader.getName());
}
}
In fact I would refine it somewhat, e.g. get the encoding out of the URLConnection rather than using a fixed one.
But it should give the idea.