-1

I new in Java. I get xml as text from Database. Next I need to get field value from this xml as text How I can do it? I do not understand.

My xml example:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:">
    <GetWlltInf>
        <MsgHdr>
            <MsgId>123125412</MsgId>
            <CreDt>2022-12-28T10:37:45.363Z</CreDt>
            <From>
                asfaoi232sdianscv
            </Fr>
                <To>lkmalksnd71t2987hd</To>
                <OperationId>a3cf7eb8-372c-4712-82bf-1a926171a701</OperationId>
        </MsgHdr>
        <Req>
            <ReqTp>HSTR</ReqTp>
            <Prd>
                <FromDateTime>
                    2022-12-20T16:00:00.000Z
                </FrDtTm>
                    <ToDateTime>
                        2022-12-22T00:00:00.000Z
                    </ToDtTm>
            </Prd

            <ReqCrit>
                <Pty>
                    <Wllt>
                        <Id>sck12312-asdasd621j23-asdasdgsfg</Id>
                    </Wllt>
                </Pty>
            </ReqCrit>
        </Req>
    </GetWlltInf>
</Document>

I googled different ideas, but wasn't able to understand. And I found a lot of answers on how to do it if you work with xml, but I work with xml as text

Thanks a lot!

Mikhail2048
  • 1,715
  • 1
  • 9
  • 26
  • "I found a lot of answers how to do it if u work with xml, but i work with xml as text". No. You do it the way you found, using an XML parser. – Jorn Jun 30 '23 at 12:26
  • This looks to be an [XY Problem](https://xyproblem.info/). You should provide more clarity into what are you trying to achieve, what is the original problem you are trying to resolve. Why are you trying to do with the XML exactly? – aled Jun 30 '23 at 12:30
  • @aled i need to get operationId from this xml and use it next in my tests – Ruslan Fazylyanov Jun 30 '23 at 12:33
  • 1
    Then you need to parse the XML using a Java XML parser to extract the text value of the that element. You say you got different ideas. Please share them and what are the issues you are having with them. Your research should be part of the question. Please read [How to Ask](https://stackoverflow.com/help/how-to-ask). – aled Jun 30 '23 at 12:44
  • @RuslanFazylyanov Please check this thread it will help. https://stackoverflow.com/questions/4076910/how-to-retrieve-element-value-of-xml-using-java – Kedar Jun 30 '23 at 12:51
  • You wrote (in your question): _I need to get field value from this xml_ What field value do you need to get? – Abra Jun 30 '23 at 13:33

1 Answers1

1

First, your XML is not correct and it won't be parsed by most libraries:

<From>
  asfaoi232sdianscv 
</Fr> <!-- How do you expect this to work?  -->

Maybe SAX parser will be able to handle it, but it's just due to the parsing mechanism and still it is not an excuse. I would suggest you to:

  1. Correct your XML to make it parsable
  2. And for parsing you can use Java DOM API. It is just easier and more readable than SAX. So use DOM if performance is not your concern. So your code would look like this. Here I just get the value of a specific element in the entire XML, if that is what you want:
try {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("... your stuff here ...".getBytes(StandardCharsets.UTF_8));
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    Document parse = documentBuilderFactory.newDocumentBuilder().parse(byteArrayInputStream);
    NodeList nodeList = parse.getElementsByTagName("Wllt");
    Node item = nodeList.item(0);
    System.out.println(item.getTextContent().trim());
} catch (ParserConfigurationException | SAXException e) {
    throw new RuntimeException(e);
}

You can read more about Java DOM Api in this article. Have a nice day

Mikhail2048
  • 1,715
  • 1
  • 9
  • 26