function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

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';



What i need in the XML output for the <gd:recurrence> tag is this:






What i get is this:




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!







Platy ITPlaty IT
I believe you need a /n for a new line (you currently have \n ).

The /n didn't work unfortunately.

Andy BoettcherAndy Boettcher

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.



Platy ITPlaty IT
Sorry, it's coming back to me now- what you have is correct but needs the escape character before it. Try //n
Andy BoettcherAndy Boettcher

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:





Andy BoettcherAndy Boettcher

Try the other slash - "/".


Not "\".



Andy BoettcherAndy Boettcher

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...




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:





Andy BoettcherAndy Boettcher

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';






13:25:03.021 (21059000)|USER_DEBUG|[25]|DEBUG|XMLNode[ELEMENT,Body,,null,[common.apex.api.dom.XmlNode$NamespaceDef@51b24da5],[XMLNode[ELEMENT,echo,,null,[common.apex.api.dom.XmlNode$NamespaceDef@1107b58a],[XMLNode[ELEMENT,category,,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


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.