CDATA tags in XML not accepted by customer

2
I have noticed that - I think since 2.5.3 - Mendix generates XML with <![CDATA[ ]]> tags. I have not noticed this behaviour in older versions of Mendix. XML message exchange is an important part in several of our projects in development and even a live application. However, today we received notice of one of our customers that they are not accepting our XML messages because of the CDATA tags. We have to remove them. What are our options? We are really not very keen on a solution where we dive into the generated file to remove the tags after it has been generated. Even if the customer should perhaps support CDATA tags, that is not really an option here. I'm kind of wondering why there is no option in the modeler to turn these tags on/off?
asked
4 answers
1

Unfortunately, there is no magic fix here. Your options are actually the ones you outlined:

  1. file a feature request to make this configurable in the modeler.
  2. remove the cdata tags manually
  3. convince the customer to accept the cdata tags (which are perfectly valid xml, btw)

On a side note: the cdata tags are there for a reason. I don't know about the specifics of your case, but aren't you afraid that some of the data might contain xml data (which should be escaped with cdata)

answered
0

JavaAction fix

        // BEGIN USER CODE
    InputStream inputstream  = Core.getFileDocumentContent(getContext(), FileDoc.getMendixObject());
    String S = convertStreamToString(inputstream);
    inputstream.close();        
    S = S.replace("<![CDATA[", "");
    S = S.replace("]]>", "");
    InputStream is = new ByteArrayInputStream( S.getBytes( "UTF-8" ));
    // save in Mendix file document
    Core.storeFileDocumentContent(getContext(), FileDoc.getMendixObject(), 
            (String) FileDoc.getName(), is);
    return true;
    // END USER CODE

    // BEGIN EXTRA CODE
public String convertStreamToString(InputStream is) throws IOException {
    /*
     * To convert the InputStream to String we use the
     * Reader.read(char[] buffer) method. We iterate until the
     * Reader return -1 which means there's no more data to
     * read. We use the StringWriter class to produce the string.
     */
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(
                    new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {        
        return "";
    }
}

// END EXTRA CODE
answered
0

@Michel String.Replace replaces all occurences. String.ReplaceAll does the same but works with regular expressions. In this case Replace is better due to the '[' and ']' characters.

answered
-1

@Chris: Concerning the fix: you should add

S = S.replaceAll(">|<", "");

as well, otherwise this fix will break as soon as somebody inserts an < or > character.

Furthermore i think you should use replaceAll instead of replace, which only replaces the first occurrence.

update of course this regexp should only be applied to the part inside the CDATA. Best is to use a Matcher class in combination with the appendReplacement method which macthes something like

"(<\\!\\[CDATA[)(.(?!\\]\\]))*(]]>)"

and then perform the replaceAll(">|<","") (or perform html escaping) on group 2. Using the append replacement you can then feed your escaped data back into the stream.

answered