You need to sign in to do that
Don't have an account?
Robinrob
Programmatically insert XML newline from Apex String
This is really bugging me!
I am trying to fill a <gd:recurrence> tag for a Google Calendar event XML feed, using a String created in apex like so:
Dom.Xmlnode node = event.getXMLRoot(); Dom.Xmlnode child = null; node.setAttribute('xmlns:gd', GData.XMLNS_GD_ATOM); node.setAttribute('xmlns:gCal', GData.CALENDAR_ATOM); child = node.addChildElement('gd:transparency', null, null); child.setAttribute('value', GDataEntry.KIND_EVENT + '.opaque'); child = node.addChildElement('gd:eventStatus', null, null); child.setAttribute('value', GDataEntry.EVENT_STATUS_CONFIRMED); child = node.addChildElement('gd:recurrence', null, null); String text = 'DTSTART;VALUE=DATE:20120101\nDTEND;VALUE=DATE:20120101\nRRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20123101'; child.addTextNode(text);
What i need in the XML output for the <gd:recurrence> tag is this:
<gd:recurrence>DTSTART;VALUE=DATE:20120101 DTEND;VALUE=DATE:20120101 RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20123101 </gd:recurrence>
What i get is this:
<gd:recurrence>DTSTART;VALUE=DATE:20120101
The XML terminates there. Should i be inserting something else for the newline? It is necessary to have newlines in the XML between the parameters of the <gd:recurrence> tag.
Thanks for your time!
The /n didn't work unfortunately.
The \ character in an APEX string is an "escape character", which may be why that string is terminating out like that.
Depending on how you're constructing that string - instead of a single slash, put TWO slashes in there. That should escape out to be a single slash for processing.
-Andy
Aaahh BINGO - good point. I just did a #facepalm when I saw what exactly he was trying to do versus what I just told him to do. "//n" for the win?
Thanks for the reply.
\\n gives this output:
Try the other slash - "/".
Not "\".
-Andy
Wait wait wait - sorry man, you were correct in your logic in your initial post. The \n character inline does indeed output the CR at least in APEX-land.
I'm wondering if it has something to do with the ".addTextNode". Checking on something...
-Andy
You guys posted a few messages there whilst i was writing one as a response to an earlier post :)
By the way, //n gave this output:
AH HA! "\r\n" is the key.
String text = 'DTSTART;VALUE=DATE:20120101\r\nDTEND;VALUE=DATE:20120101\r\nRRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20123101';
-Andy
(Debug)
13:25:03.021 (21059000)|USER_DEBUG|[25]|DEBUG|XMLNode[ELEMENT,Body,http://schemas.xmlsoap.org/soap/envelope/,null,[common.apex.api.dom.XmlNode$NamespaceDef@51b24da5],[XMLNode[ELEMENT,echo,http://www.myservice.com/services/MyService/,null,[common.apex.api.dom.XmlNode$NamespaceDef@1107b58a],[XMLNode[ELEMENT,category,http://www.myservice.com/services/MyService/,null,[common.apex.api.dom.XmlNode$NamespaceDef@111f7fc2],[XMLNode[TEXT,null,null,null,null,null,classifieds,]],null,]],null,], XMLNode[ELEMENT,gd:recurrence,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,DTSTART;VALUE=DATE:20120101
DTEND;VALUE=DATE:20120101
RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20123101,]],null,]
Thanks a lot for your replies!
'\r\n' does indeed work.
However, i then made a stupid discovery - so does '\n'. I was 'looking' for an error when one wasn't there, simply because i'd been copy-pasting the debug log so many times and the original single line of output went off the screen. When i thought the string had terminated it hadn't, it was just on the next two lines.
Robin