In iOS, I would like to parse an XML stream from the server while downloading. It should not have to wait until the server is done building the XML and the download is complete. The server builds XML in "chunks" and sends it directly to the client. In my app I have a UITableView
which should instantly show the elements of the XML as soon as I received it from the server.
I've tried it with the XMLParser(contentsOf: URL)
constructor, but this first downloads the whole XML and then parses it. There is another constructor XMLParser(stream: InputStream)
, but I don't know how to get an InputStream
from a URLConnection
. The only thing I found was this question which is almost 5 years old, and I couldn't understand it how to do this in Swift 3, if it even works.
The other thing I tried was via Libxml2, but there I have problems with RAM usage or other things (see my old question).
How can I parse an XML stream in chunks without having to wait for a complete download in Swift?
In Android, I would use an XMLPullParser
and have:
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
InputStream inputStream = connection.getInputStream();
XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = xmlFactoryObject.newPullParser();
pullParser.setInput(inputStream, null);
// here comes the actual parsing