I get a problem when I try to get field value from XML
My XML example:
<?xml version="1.0" encoding="UTF-8"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:notificationName">
<Ntfctn>
<MsgHdr>
<MsgId>47e22e35-6866-46f0-bc5d-8b5bb6afaba7</MsgId>
<CreDt>2023-07-03T20:41:20Z</CreDt>
<Fr>66666666-6666-4666-8666-666666666666</Fr>
<To>93216692-7432-444d-88a1-18297159a92a</To>
<OprId>4e8cf5f6-c6e1-4314-b043-65dfe13ab2fa</OprId>
</MsgHdr>
<OprInfAndSts>
<OprInf>
<DCXchgOprInf>
<XchgOprAmt>
<TtlAmt Ccy="RUB">147.71</TtlAmt>
</XchgOprAmt>
<DgtlCcyTrfData>
<DCBuyr>
<Wllt>
<Id>93216692-7432-444d-88a1-18297159a92a</Id>
<Bal>
<TtlAmt Ccy="RUB">13122.60</TtlAmt>
</Bal>
</Wllt>
</DCBuyr>
</DgtlCcyTrfData>
<SttlmDtTm>2023-07-03T20:41:20Z</SttlmDtTm>
</DCXchgOprInf>
</OprInf>
</OprInfAndSts>
</Ntfctn>
</Document>
I have a method that helps me to get the OprId field value
public String getFieldValue(String xml, String tagName, String tagValue) {
xml = xml.trim().replaceFirst("<?","<");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Document parse = documentBuilderFactory.newDocumentBuilder().parse(byteArrayInputStream);
NodeList nodeList = parse.getElementsByTagName(tagName);
Node item = nodeList.item(0);
return item.getTextContent();
}
I added replaceFirst with trim because I had an error - content is not allowed in prolog.
And this thing helped me
But now when I call getFieldValue method I have SAXParseExeception - XML document structures must start and end within the same entity
I understand the meaning of the error, but I don't know how to fix this issue.
Thanks a lot! Have a nice day.