0

I have to build a string for transmission to one of our servers, and I started experiencing failures in the the process, and I see what the problem is, but I can not figure out how to fix this one. If anyone has any ideas, I'm very opening to hearing them.

Here's my entire method, which converts the class into an xml string.

- (NSData *)classToXML
{
    NSMutableString *xml = [@"<JournalData xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" mutableCopy];
    // 150
    if( _journalSubject.length > 150 )
        _journalSubject = [_journalSubject stringByReplacingCharactersInRange:NSMakeRange(151, _journalSubject.length - 150) withString:@""];
    [xml appendFormat:@"<JournalSubject>%@</JournalSubject>", _journalSubject];
    if( self.recipientList == nil )
         [xml appendFormat:@"<RecipientList isNull=\"True\"/>"];
    else
    {
        [xml appendString:@"<RecipientList>"];
        for( CTIJournalRecipients *r in self.recipientList )
             [xml appendString:[r classToXML]];
        [xml appendString:@"</RecipientList>"];
    }
    [xml appendFormat:@"<JournalSysCategoryid1>%@</JournalSysCategoryid1>", _sysCategoryid1];
    [xml appendFormat:@"<JournalSysCategoryid2>%@</JournalSysCategoryid2>", _sysCategoryid2];
    [xml appendFormat:@"<Userid>%@</Userid>", _userid];
    [xml appendFormat:@"<Deptid>%@</Deptid>", _deptid];
    [xml appendFormat:@"<Activityid>%@</Activityid>", _activityid ? _activityid : @(0)];
    // 50
    if( _deptDescription.length > 50 )
       _deptDescription = [_deptDescription stringByReplacingCharactersInRange:NSMakeRange(51, _deptDescription.length - 50) withString:@""];
    [xml appendFormat:@"<DeptDescription>%@</DeptDescription>", _deptDescription];
    [xml appendFormat:@"<SecLevel>%@</SecLevel>", _securityLevel];
    [xml appendFormat:@"<AttachedDocs>%@</AttachedDocs>", _attachedDocs];
    [xml appendFormat:@"<Priority>%@</Priority>", _priority];
    // 60
    if( _associatedActivity.length > 60 )
        _associatedActivity = [_associatedActivity stringByReplacingCharactersInRange:NSMakeRange(61, _associatedActivity.length - 60) withString:@""];
    [xml appendFormat:@"<AssociatedActivity>%@</AssociatedActivity>", _associatedActivity.length > 0 ? _associatedActivity : @"No Activity"];
    [xml appendFormat:@"<JournalPerson>%@</JournalPerson>", _journalPerson];
    [xml appendFormat:@"<JournalDoc>%@</JournalDoc>", _journalDoc];
    [xml appendFormat:@"<JournalTransmission>%@</JournalTransmission>", _recipientList.count > 0 ? @"Y" : @"N"];
    [xml appendFormat:@"<JournalAssignment>%@</JournalAssignment>", _journalAssignment];
    [xml appendFormat:@"<JournalTag>%@</JournalTag>", _journalTag];
    [xml appendString:@"<JournalMemo>%@</JournalMemo>", _journalMemo];
    [xml appendFormat:@"<CreateUserFullname>%@</CreateUserFullname>", _createUserFullName];
    [xml appendFormat:@"<CreateUserID>%@</CreateUserID>", _userid];
    [xml appendFormat:@"<Macroid>%@</Macroid>", _macroid];
    [xml appendFormat:@"<sContext>%@</sContext>", [_sContext capitalizedString]];
    [xml appendFormat:@"<iDetail1>%@</iDetail1>", _iDetail1 ? _iDetail1 : @(0)];
    [xml appendFormat:@"<iDetail2>0</iDetail2>"];
    [xml appendFormat:@"<iDetail3>0</iDetail3>"];
    [xml appendFormat:@"<iDetail4>0</iDetail4>"];
    [xml appendFormat:@"<sConcerning>%@</sConcerning>", _sConcerning];
    [xml appendFormat:@"<sFaxCover>%@</sFaxCover>", _sFaxCover];
    [xml appendFormat:@"<bTransmit>%@</bTransmit>", _recipientList.count > 0 ? @"true" : @"false" ];
    // 18
    if( _journalHeaderAccess.length > 18 )
        _journalHeaderAccess = [_journalHeaderAccess stringByReplacingCharactersInRange:NSMakeRange(19, _journalHeaderAccess.length - 18) withString:@""];
    [xml appendFormat:@"<JournalHeaderAccess>%@</JournalHeaderAccess>", _journalHeaderAccess.length > 0 ? _journalHeaderAccess : _sContext];
    [xml appendFormat:@"<vParams isNull=\"True\"></vParams>" ];
    [xml appendFormat:@"<DocumentList isNull=\"True\"></DocumentList>"];
    [xml appendFormat:@"<iJournalReplyHeaderid>%@</iJournalReplyHeaderid>", _iJournalReplyHeader];
    [xml appendFormat:@"<iReplyToTransmissionid>%@</iReplyToTransmissionid>", _iReplyToTransmissionid];
    [xml appendFormat:@"</JournalData>"];
    //NSString *xmlFinal = [NSString stringWithFormat:xml, [_journalMemo dataUsingEncoding:NSUTF8StringEncoding]];
    //CLSNSLog(@"%@", xml);
    return xmlEncoded;
}

My problem is with the JournalMemo field, when I use this string:

n\n\n12/9/14, 11:26 PM: Someone Else wrote:\n\nTest reply please.\r\n\r\nThanks!\r\n\r\n\r\n

I tried several different methods to insert that memo field into the xml and here's what the xml looks like just before I insert at the JournalMemo token:

<JournalData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<JournalSubject>RE: Test reply please</JournalSubject>
<RecipientList>
     <JournalRecipients>
         <RecipientMethod>rmTo</RecipientMethod>
         <CommID>6711</CommID>
         <CommTypeID>52</CommTypeID>
         <CommName>someperson@example.com</CommName>
         <PersonID>2656</PersonID>
         <PersonName>Some Person</PersonName>
         <PersonCategoryID>0</PersonCategoryID>
         <PersonCategory />
         <PersonType />
         <ToType />
    </JournalRecipients>
</RecipientList>
<JournalSysCategoryid1>2000</JournalSysCategoryid1>
<JournalSysCategoryid2>490</JournalSysCategoryid2>
<Userid>2656</Userid>
<Deptid>0</Deptid>
<Activityid>0</Activityid>
<DeptDescription>No Department Selected</DeptDescription>
<SecLevel>1</SecLevel>
<AttachedDocs>N</AttachedDocs>
<Priority>NORMAL</Priority>
<AssociatedActivity>No Activity</AssociatedActivity>
<JournalPerson>N</JournalPerson>
<JournalDoc>N</JournalDoc>
<JournalTransmission>Y</JournalTransmission>
<JournalAssignment>N</JournalAssignment>
<JournalTag>X</JournalTag>
<JournalMemo>%@</JournalMemo>
<CreateUserFullname>Someone Else</CreateUserFullname>
<CreateUserID>2676</CreateUserID>
<Macroid>0</Macroid>
<sContext>(null)</sContext>
<iDetail1>0</iDetail1>
<iDetail2>0</iDetail2>
<iDetail3>0</iDetail3>
<iDetail4>0</iDetail4>
<sConcerning>(null)</sConcerning>
<sFaxCover>Primary Cover Sheet</sFaxCover>
<bTransmit>true</bTransmit>
<JournalHeaderAccess>(null)</JournalHeaderAccess>
<vParams isNull="True"></vParams>
<DocumentList isNull="True"></DocumentList> 
<iJournalReplyHeaderid>557762</iJournalReplyHeaderid>
<iReplyToTransmissionid>1170918</iReplyToTransmissionid>
</JournalData>

And after I insert the string, this is what the xml variable looks like:

<JournalData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<JournalSubject>RE: Test reply please</JournalSubject>
<RecipientList>
     <JournalRecipients>
         <RecipientMethod>rmTo</RecipientMethod>
         <CommID>6711</CommID>
         <CommTypeID>52</CommTypeID>
         <CommName>someperson@example.com</CommName>
         <PersonID>2656</PersonID>
         <PersonName>Some Person</PersonName>
         <PersonCategoryID>0</PersonCategoryID>
         <PersonCategory />
         <PersonType />
         <ToType />
    </JournalRecipients>
</RecipientList>
<JournalSysCategoryid1>2000</JournalSysCategoryid1>
<JournalSysCategoryid2>490</JournalSysCategoryid2>
<Userid>2656</Userid>
<Deptid>0</Deptid>
<Activityid>0</Activityid>
<DeptDescription>No Department Selected</DeptDescription>
<SecLevel>1</SecLevel>
<AttachedDocs>N</AttachedDocs>
<Priority>NORMAL</Priority>
<AssociatedActivity>No Activity</AssociatedActivity>
<JournalPerson>N</JournalPerson>
<JournalDoc>N</JournalDoc>
<JournalTransmission>Y</JournalTransmission>
<JournalAssignment>N</JournalAssignment>
<JournalTag>X</JournalTag>
<JournalMemo>


12/9/14, 11:26 PM: Someone Else wrote:

Test reply please.

Thanks!

And I'm not entirely sure what is causing this problem, or how I can fix it. Any help would be greatly appreciated.

arythiafan
  • 30
  • 2
  • 11

2 Answers2

0

The \n is causing issue, you need to escape it:

Use:

_journalMemo = [_journalMemo stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • I tried that, and the string still fails to create. I escaped the \n and the \r too. I'm still open for other ideas too. – arythiafan Dec 10 '14 at 17:04
0

This answer helped me overcome my problem.

I ended up using this code, as apparently there are some issues with UTF8 encoding.

NSString *str = [NSString stringWithUTF8String: [_journalMemo cStringUsingEncoding: [ NSString defaultCStringEncoding ] ] ];
[xml appendFormat:@"<JournalMemo>%@</JournalMemo>", str];
Community
  • 1
  • 1
arythiafan
  • 30
  • 2
  • 11