views:

98

answers:

2

I am writing an app that ultimately wants to send some XML via email.

I have the mailto/URL thing sussed, thanks to various links on the interweb, including Brandon and Simon Maddox.

So I can send emails with the xml formatted using square brackets ([ ]), rather than the usual angle brackets (< >). But when I send angle brackets, with the XML mangled using the stringByAddingPercentEscapesUsingEncoding call, It treats it as HTML and just prints the values.

If change them to "& lt;" and "& gt;" then it totally strips the XML out... (I know there should not be a space after the & - but the SO formatter turns them into <,>...)

I tried adding some HTML in front to see if that helped, to no avail.

I don't suppose anyone has done this?

Perhaps in-app email is the easy route for me to go... must look into that.

Thanks in advance.

+1  A: 

Did you try replacing all the '<' and '>' characters with '&lt' and '&gt' after you had wrapped it in the basic HTML headers?

As I understand it, this is the usual technique to display XML on a web page.

CynicismRising
Yes, see my edit above - although when you say "wrap in HTML", I tried prefixing with a <p>, <div> only - no closing tag... maybe that would help...
Chris Kimpton
+1  A: 

The following code worked for me... I have SIP message data containing <> that needed escaping.

/* remember to call urlEscapeStringDone to free the malloced string.. */
char *urlEscapeString(char *str)
{
    int i, l;
    char *escStr;

    escStr = malloc(strlen(str)*3 + 1);
    if(!escStr) return NULL;

    memset(escStr, 0, strlen(str)*3);

    l = strlen(escStr);
    for(i = 0; i < strlen(str); i++)
    {
     char c = str[i];

     /* < and > handling for HTML interpreters.. (apple mail) */
     if(c == '<')
     {
      strcat(escStr, "%26lt%3b");
      l += 8;
     }
     else if(c == '>')
     {
      strcat(escStr, "%26gt%3b");
      l += 8;
     }
     else if(must_escape(c))
     {
      char tmp[3];

      sprintf(tmp, "%02x", (unsigned) c);
      escStr[l] = '%'; l++;
      escStr[l] = tmp[0]; l++;
      escStr[l] = tmp[1]; l++;
     }
     else
     {
      escStr[l] = str[i];
      l++;
     }
    }

    printf("escaped: %s\n", escStr);

    return escStr;
}

void urlEscapeStringDone(char *str)
{
    if(str) free(str);
}

int must_escape(char c)
{
    char *allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._";

    if(!strchr(allowedChars, c)) return 1;
    return 0;
}
justinb