1

I have a string which looks like an XML

Ex: String sample = "<GrpHdr><MsgId>MQSECJYJHRBPDTZTYNNEYXOZUPAUDEKVDFV</MsgId><CreDtTm>2023-02-02T21:48:58.075+05:30</CreDtTm></GrpHdr>";

I am trying to create an XML document with an element containing the above information:

Ex:

<ns1:TstCode>T</ns1:TstCode>
<ns1:FType>SCF</ns1:FType>
<ns1:FileRef>220811084023</ns1:FileRef>
<ns1:RoutingInd>ALL</ns1:RoutingInd>
<ns1:FileBusDt>2022-08-11</ns1:FileBusDt>
<ns1:FIToFI xmlns="urn:iso:std:iso:20022:tech:xsd">
    <GrpHdr>
        <MsgId>MQSECJYJHRBPDTZTYNNEYXOZUPAUDEKVDFV</MsgId>
        <CreDtTm>2023-02-02T21:48:58.075+05:30</CreDtTm>
    </GrpHdr>
</ns1:FIToFI>

When I create the document for the above XML using this code:

    private static DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();
    DOMImplementation domImpl = db.getDOMImplementation();

private Document buildExampleDocumentWithNamespaces(DOMImplementation domImpl, String output) {
    Document document = domImpl.createDocument("urn:Scf:xsd:$BlkCredTrf", "ns1:BlkCredTrf", null);
    document.getDocumentElement().appendChild(document.createElement("ns1:TstCode")).setTextContent("T");
    document.getDocumentElement().appendChild(document.createElement("ns1:FType")).setTextContent("SCF");
    document.getDocumentElement().appendChild(document.createElement("ns1:FileRef")).setTextContent("220811084023");
    document.getDocumentElement().appendChild(document.createElement("ns1:RoutingInd")).setTextContent("ALL");
    document.getDocumentElement().appendChild(document.createElement("ns1:FileBusDt")).setTextContent("2022-08-11");
    document.getDocumentElement().appendChild(document.createElementNS("urn:iso:std:iso:tech:xsd","ns1:FIToFI");
    return document;
}

I do not have issues until this point.

When I try to add <GrpHdr><MsgId>MQSECJYJHRBPDTZTYNNEYXOZUPAUDEKVDFV</MsgId><CreDtTm>2023-02-02T21:48:58.075+05:30</CreDtTm></GrpHdr> as a Text content to the FIToFI tag at last, using the code:

document.getDocumentElement().appendChild(document.createElementNS("urn:iso:std:iso:tech:xsd","ns1:FIToFI").setTextContent(sample);

The XML gets created like this:

<ns1:TstCode>T</ns1:TstCode>
<ns1:FType>SCF</ns1:FType>
<ns1:FileRef>220811084023</ns1:FileRef>
<ns1:RoutingInd>ALL</ns1:RoutingInd>
<ns1:FileBusDt>2022-08-11</ns1:FileBusDt>
<ns1:FIToFI xmlns="urn:iso:std:iso:20022:tech:xsd">
    &lt;GrpHdr&gt;
        &lt;MsgId&gt;MQSECJYJHRBPDTZTYNNEYXOZUPAUDEKVDFV&lt;/MsgId&gt;
        &lt;CreDtTm&gt;2023-02-02T21:48:58.075+05:30&lt;/CreDtTm&gt;
    &lt;/GrpHdr&gt;
</ns1:FIToFI>

Please help me to create this XML without the escape characters.

0

1 Answer 1

5

That the content is escaped is intended. When you set the text content of an element, any special character like < have to be escaped like &lt;, otherwise the text content will be interpreted as other XML content like elements or comments. That's why setTextContent() will escape the content for you.

When you want to add an element instead, you use methods like appendChild() with an Element argument. Build your elements as usual with the createElement() method and add them together like this:

Element element = document.createElementNS("urn:iso:std:iso:tech:xsd","ns1:FIToFI");
Element grpHdr = document.createElement("GrpHdr");
    
Element msgId = document.createElement("MsgId");
msgId.setTextContent("MQSECJYJHRBPDTZTYNNEYXOZUPAUDEKVDFV");
grpHdr.appendChild(msgId);
    
Element creDtTm = document.createElement("CreDtTm");
creDtTm.setTextContent("2023-02-02T21:48:58.075+05:30");
grpHdr.appendChild(creDtTm);
    
element.appendChild(grpHdr);
    
document.getDocumentElement().appendChild(element);

This will add the XML element inside the other XML element.

When you have the inner XML as a string, parse the XML string with DocumentBuilder.parse() (see How to create a XML object from String in Java?) and import the Element with the Document.importNode() method (see org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it). The code can look like this:

String innerXml = "<GrpHdr><MsgId[...]eDtTm></GrpHdr>";
    
Document innerDocument = db.parse(new InputSource(new StringReader(innerXml)));
Element innerRootElement = innerDocument.getDocumentElement();
    
Node importedNode = document.importNode(innerRootElement, true);
    
element.appendChild(importedNode);
3
  • Thanks for your comment. The string I posted here in the description: <GrpHdr> <MsgId>MQSECJYJHRBPDTZTYNNEYXOZUPAUDEKVDFV</MsgId> <CreDtTm>2023-02-02T21:48:58.075+05:30</CreDtTm> </GrpHdr> is only a sample. The real string that I want to add as a value to the element is very big. It would be cumbersome to add each element from that string individually. Is there a way I can do without escaping, like using some other method apart from setTextContent().
    – Vishnu
    Feb 4 at 3:25
  • @Vishnu You can parse the XML code to a Document instance by using the DocumentBuilder.parse() method, see stackoverflow.com/questions/7607327/…. Then you get the Element and add it to your target Document. Updated the answer.
    – Progman
    Feb 4 at 9:02
  • Thanks a lot Mr. Progman. You are my saviour. Your answer has solved my problem.
    – Vishnu
    Feb 4 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.