I am making an API call and now I need to get a specific piece of data from the response. I am needing to get the DocumentID for the "Description" Invoice, which in the case below is 110107.
I have already created a method to get data from get a single tag by doing this:
public synchronized String getTagFromHTTPResponseAsString(String tag, String body) throws IOException {
final Pattern pattern = Pattern.compile("<"+tag+">(.+?)</"+tag+">");
final Matcher matcher = pattern.matcher(body);
matcher.find();
return matcher.group(1);
} // end getTagFromHTTPResponseAsString
However, my problem is with this result set, there are multiple fields with the same tag and I need a specific one. Here is the response:
<?xml version="1.0" encoding="utf-8"?>
<Order TrackingID="351535" TrackingNumber="TEST-843245" xmlns="">
<ErrorMessage />
<StatusDocuments>
<StatusDocument NUM="1">
<DocumentDate>7/14/2017 6:52:00 AM</DocumentDate>
<FileName>4215.pdf</FileName>
<Type>Sales Contract</Type>
<Description>Uploaded Document</Description>
<DocumentID>110098</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
<StatusDocument NUM="2">
<DocumentDate>7/14/2017 6:52:00 AM</DocumentDate>
<FileName>Apex_Shortcuts.pdf</FileName>
<Type>Other</Type>
<Description>Uploaded Document</Description>
<DocumentID>110100</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
<StatusDocument NUM="3">
<DocumentDate>7/14/2017 6:52:00 AM</DocumentDate>
<FileName>CRAddend.pdf</FileName>
<Type>Other</Type>
<Description>Uploaded Document</Description>
<DocumentID>110104</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
<StatusDocument NUM="4">
<DocumentDate>7/14/2017 6:52:00 AM</DocumentDate>
<FileName>test.pdf</FileName>
<Type>Other</Type>
<Description>Uploaded Document</Description>
<DocumentID>110102</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
<StatusDocument NUM="5">
<DocumentDate>7/14/2017 6:55:00 AM</DocumentDate>
<FileName>Invoice.pdf</FileName>
<Type>Invoice</Type>
<Description>Invoice</Description>
<DocumentID>110107</DocumentID>
<DocumentPlaceHolder />
</StatusDocument>
</StatusDocuments>
</Order>
I tried creating and testing out my regular expression on https://regex101.com/ and got this RegEx to work there, but I cannot get it to translate over correctly into my Java code:
<Description>Invoice<\/Description>
<DocumentID>(.*?)<\/DocumentID>