Question

I am writing a simulator which communicates with a client's piece of software over a local socket. The communication language is XML. I have written some code which works - parsing the incoming XML string into Document via the DocumentBuilder interface.

I have been encountering a problem with CDATA (Having never seen it before). Basically, I need to access fields within the CDATA tag and change them. I load up a 'template' XML document (to reply to the messages with) and use values received in the first message inside the response. Some of the fields that need to be changed are in this CDATA tag (clear what I mean below).

public static String getOutputMessage(String input) throws Exception{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document inputDoc, outputDoc;
    Element messageElement = (Element)inputDoc.getElementsByTagName("TRANS").item(0);
    messageType = messageElement.getAttribute("name");
    if (messageType.equals("processTransaction")){
        outputDoc = db.parse(path+"processTransaction\\posPrintReceipt.xml");
        outputDoc = changeContent(outputDoc, "PAN_NUMBER", transaction.getPan_number());
        outputDoc = changeContent(outputDoc, "TOKEN", transaction.getToken());
        outputDoc = changeContent(outputDoc, "TOTAL_AMOUNT", transaction.getTotal_amount());
        outputDoc = changeContent(outputDoc, "TRANSACTION_TIME", transaction.getTransaction_time());
        outputDoc = changeContent(outputDoc, "TRANSACTION_DATE", transaction.getTransaction_date());
    }
}

private static Document changeContent(Document doc,String tag,String value) {
    System.out.println("Changing: ["+tag+" : "+value+"]");
    NodeList nodes=doc.getElementsByTagName(tag);
    Node node = nodes.item(0);
    Node parent=node.getParentNode();
    node.setTextContent(value);
    System.out.println(doc.getElementsByTagName(tag).item(0) + " " + node.getTextContent());
    parent.replaceChild(node, doc.getElementsByTagName(tag).item(0));
    return doc;
}

The functions above work on normal Elements but below is an example XML message I have to read and change some values such as

<RLSOLVE_MSG version="5.0">
<MESSAGE>
    <SOURCE_ID>DP01</SOURCE_ID>
    <TRANS_NUM>000001</TRANS_NUM>
</MESSAGE>
<POI_MSG type="interaction">
    <INTERACTION name="posPrintReceipt">
        <RECEIPT type="merchant" format="xml">
            <![CDATA[<RECEIPT>
    <AUTH_CODE>06130</AUTH_CODE>
    <CARD_SCHEME>VISA</CARD_SCHEME>
    <CURRENCY_CODE>GBP</CURRENCY_CODE>
    <CUSTOMER_PRESENCE>internet</CUSTOMER_PRESENCE>
    <FINAL_AMOUNT>1.00</FINAL_AMOUNT>
    <MERCHANT_NUMBER>8888888</MERCHANT_NUMBER>
    <PAN_NUMBER>454420******0382</PAN_NUMBER>
    <PAN_EXPIRY>12/15</PAN_EXPIRY>
    <TERMINAL_ID>04176421</TERMINAL_ID>
    <TOKEN>454420bbbbbkqrm0382</TOKEN>
    <TOTAL_AMOUNT>1.00</TOTAL_AMOUNT>
    <TRANSACTION_DATA_SOURCE>keyed</TRANSACTION_DATA_SOURCE>
    <TRANSACTION_DATE>14/02/2014</TRANSACTION_DATE>
    <TRANSACTION_NUMBER>000001</TRANSACTION_NUMBER>
    <TRANSACTION_RESPONSE>06130</TRANSACTION_RESPONSE>
    <TRANSACTION_TIME>17:13:17</TRANSACTION_TIME>
    <TRANSACTION_TYPE>purchase</TRANSACTION_TYPE>
    <VERIFICATION_METHOD>unknown</VERIFICATION_METHOD>
    <DUPLICATE>false</DUPLICATE>
</RECEIPT>]]>
        </RECEIPT>
    </INTERACTION>
</POI_MSG>

Was it helpful?

Solution

CDATA is an encoding mechanism to include arbitrary data within an XML file. Everything within CDATA is parsed as a single string when loading the XML into a Document instance. If you need to access the contents of the CDATA as a DOM document, you will need to instantiate a second Document object from the string contents, make your changes, then serialize that back to a string and put the string back into a CDATA in the original document.

OTHER TIPS

I dont think CDATA section will be parsed as other regular elements in the XML. CDATA section is purely to escape any syntax checks. My suggestion would be use a element to represent the data in CDATA section. If you still want to use CDATA section, I guess you'll need parse the section as a string and then load the data into a Document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top