0

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.

  • What you are doing with 'replaceFirst("","<")' is incorrect. By doing that you are converting the XML header to an element, which causes this error. You can get some ideas to solve your initial issue in this answer https://stackoverflow.com/a/3030913/2140633 – Udith Gunaratna Jul 04 '23 at 08:19
  • @UdithGunaratna i had error "content is not allowed in prolog." and this replaceFirst helped me – Ruslan Fazylyanov Jul 04 '23 at 08:22
  • If I copy your sample document into a file, `DocumentBuilder` parses the file without problems. This suggests that `xml` contains something other than your sample document. This makes helping you fairly impossible. That said, I wanted to point out that you do not need to convert your `xml` text to a `ByteArrayInputStream` for parsing. Just use a `StringReader` and an `InputSource`: `documentBuilder.parse(new InputSource(new StringReader(xml)))`. – Thomas Behr Jul 04 '23 at 09:06

0 Answers0