Home > OS >  > and < gets converted to > and < while adding a xml like string in element.setTextConte
> and < gets converted to > and < while adding a xml like string in element.setTextConte

Time:02-05

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.

CodePudding user response:

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);
  • Related