title: Parse an XML file in chunks without waiting for complete download in Swift
body: <p>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 <code>UITableView</code> which should instantly show the elements of the XML as soon as I received it from the server.</p>
  
  <p>I've tried it with the <code>XMLParser(contentsOf: URL)</code> constructor, but this first downloads the whole XML and then parses it. There is another constructor <code>XMLParser(stream: InputStream)</code>, but I don't know how to get an <code>InputStream</code> from a <code>URLConnection</code>. The only thing I found was <a href="https://stackoverflow.com/questions/10777604/using-nsxmlparser-initwithstream-no-parser-delegate-methods-received">this question</a> which is almost 5 years old, and I couldn't understand it how to do this in Swift 3, if it even works.</p>
  
  <p>The other thing I tried was via Libxml2, but there I have problems with RAM usage or other things (see my <a href="https://stackoverflow.com/questions/41721369/large-ram-usage-when-parsing-xml-using-libxml2">old question</a>).</p>
  
  <p>How can I parse an XML stream in chunks without having to wait for a complete download in Swift?</p>
  
  <p>In Android, I would use an <code>XMLPullParser</code> and have:</p>
  
  <pre class="lang-java prettyprint-override"><code>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
  </code></pre>