title: Read XML as String from Input Stream continuously
body: <p>I'm trying to read XML data from a continuous stream, I need to store each XML in a list of strings. I need the raw XML data simply as string data and not DOM or SAX or any kind of serialization.
  I'm currently reading from an InputStream into a Scanner and using a delimeter, but I need to account for a scenario where the XML header is not present.</p>
  
  <pre><code>String xml = "&lt;?xml version=\"1.0\" standalone=\"yes\"?&gt;&lt;root&gt;&lt;/root&gt;"
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>String xml = "&lt;root&gt;&lt;/root&gt;
  </code></pre>
  
  <p>my current implementation is</p>
  
  <pre><code>try (Socket socket = server.accept()) {
      try (InputStream in = socket.getInputStream()) {
          final Scanner scanner = new Scanner(new InputStreamReader(in, "UTF-8"));
          scanner.useDelimiter("&lt;\\?xml.*?\\?&gt;");  //Stop stream read when XMl tag is found
      }
  }
  </code></pre>
  
  <p>Is it possible to write a regex that account for either the xml header or the first node? (The first node is always the same)</p>
  
  <p>I've attempted to use a XMLStreamReader but as far as I can figure out it only pull parses the data by traversing through it. That's fine, but I ultimately need the entire XML as a String each time.</p>
  
  <p>EDIT: For clarification there's only one "root" node per XML, the header my be there and it may not. so some scenarios.</p>
  
  <pre><code>&lt;?xml version=\"1.0\" standalone=\"yes\"?&gt;
  &lt;root&gt;
  &lt;/root&gt;
  &lt;?xml version=\"1.0\" standalone=\"yes\"?&gt;
  &lt;root&gt;
  &lt;/root&gt;
  &lt;root&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I want to handle all 3 of these xmls as strings</p>