-1
<CstmrCdtTrfInitn>
    <GrpHdr>
        <MsgId>201805231510</MsgId>
        <CreDtTm>2018-05-23T12:01:14</CreDtTm>
        <NbOfTxs>1</NbOfTxs>
        <CtrlSum>111.00</CtrlSum>
        <InitgPty>
            <Nm>custName</Nm>
            <Id>
                <OrgId>
                    <Othr>
                        <Id>orgNumber</Id>
                        <SchmeNm>
                            <Cd>ABCD</Cd>
                        </SchmeNm>
                    </Othr>
                </OrgId>
            </Id>
        </InitgPty>
    </GrpHdr>
</CstmrCdtTrfInitn>

I have found several examples on how to modify some text in a XML file. But my problem is that there are repetitive nodes, like in the below example where node Id occurs several times. I would like to modify the value orgNumber with something else but I don´t really manage to find correct Id. Any input would be appreciated.

4

1 Answer 1

-1

Your XML looks like an ISO 20022 interbanking/payment message. I've had success using JAXB with ISO 20022 schemas. JAXB (part of J2EE/JEE, or Jakarta EE as it is called now) will give you a Java POJO representation of the XML message which you can then freely construct and otherwise manipulate like any other Java object graph. For example, you would be able to access the content of the (innermost) Id element in your example using Java expressions such as

CstmrCdtTrfInitn myMessage = ...;
String valueOfIdElement =
 myMessage.getGrpHdr().getInitgPty().getId().getOrgId().getId();

For using JAXB, it's required to have XML Schema files (.xsd files) describing your XML data. You will need to generate Java classes from .xsd files using eg. https://github.com/highsource/maven-jaxb2-plugin (from maven) or just the xjc tool which is part of the JAXB reference implementation (from the command line) and then configure JAXB for the schemas you have. You'll generally want to at least configure the Java package namespaces of the generated Java classes.

Sign up to request clarification or add additional context in comments.

15 Comments

Thank you all for commenting my post. This JAXB sound like something I would need to learn more about but as you can notice I have not done so much programming so I am trying with things where I can at least find some examples on. I have tried in many different way but more or less they are all in this below way which I cannot get to work
NodeList InitgPtyNodeList = doc.getElementsByTagName("InitgPty"); for(int i = 0; i < InitgPtyNodeList.getLength(); i++) { Node InitgPty = InitgPtyNodeList.item(i); if(InitgPty.hasChildNodes()) { NodeList InitgPtyChilds = InitgPty.getFirstChild().getNextSibling().getChildNodes();
OK, I checked som examples about JAXB and actually I managed to generate all the classes from my xsd. I did it via CMD by typing "...Test\src>xjc -p pain113.jaxb.beans pain113.xsd". Now I have many Java files and as you said I can write somethine like myMessage.getGrpHdr().getInitgPty().getId().getOrgId().getId(); However, now of course another problem occurs. How can I connect these classes to my actual XML? I read something about marshalling and unmarshalling but I don´t understand it completely. This is what I am trying at the moment:
File file = new File("C:\\Test\\src\\T1.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); pain113.jaxb.beans.Document d = (pain113.jaxb.beans.Document) jaxbUnmarshaller.unmarshal(file); System.out.println(d.getCstmrCdtTrfInitn().getGrpHdr().getInitgPty().getNm()); But this just gives me a lot of errors. Any suggestion?
@AmirSejdinovic I suggest you post another SO question with the details (description of your setup and the error message you get) - it's hard to tell not knowing your CLASSPATH setup, your IDE (if any), etc. Congrats for getting this far!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.