I am running a query through an API and I'm receiving a larger set of XML through an InputStream than I normally expect. This is causing me to run into heap space errors while trying to parse the XML into a String. Here is how I am currently parsing the input stream:
public String parseXml(InputStream inputStream) throws IOException {
String myString;
List<String> matchingXmlList = new ArrayList<>();
byte[] byteArray = toByteArray(inputStream);
String tempString = new String(byteArray, StandardCharsets.UTF_8);
return tempString;
}
Under normal circumstances, this is the output:
<?xml version="1.0" encoding="UTF-8">
<queryResult>
<records />
<records />
<records />
<records />
</queryResult>
After receiving an output like this, I then use a Matcher to parse each individual records node and add it to a List of Strings. But when the InputStream is too large, I run into memory problems when trying to put the InputStream into a String.
Is there a way to parse the InputStream without running into memory problems? I need to be able to parse each individual records element to a string of XML.