• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Escape xml with CFXMLCreateStringByEscapingEntities
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Escape xml with CFXMLCreateStringByEscapingEntities


  • Subject: Re: Escape xml with CFXMLCreateStringByEscapingEntities
  • From: Andreas Grosam <email@hidden>
  • Date: Fri, 02 Dec 2011 14:11:05 +0100

On Dec 1, 2011, at 9:19 AM, Claude Saelens wrote:

> Hello,
>
> I need to put XML fragments into another XML. Each time an XML fragment is put into another XML it needs to be escaped to be send to backend a java application. I used the function "CFXMLCreateStringByEscapingEntities" but it seems it can only escape an XML fragment once, the second time strange characters are added.
>
> For this is wrote following method:
>
> -(NSMutableString*)xmlEscape:(NSMutableString*)xmlString
> {
> 	NSMutableString *aNSString = [NSMutableString stringWithString:xmlString];
> 	CFStringRef aCFString;
> 	CFStringRef bCFString;
> 	aCFString = CFStringCreateWithCString(NULL, [aNSString UTF8String], NSUTF8StringEncoding);
> 	bCFString = CFXMLCreateStringByEscapingEntities(kCFAllocatorDefault, aCFString, NULL);
>
> 	NSMutableString *returnValue = [NSMutableString stringWithString:(NSMutableString*)bCFString];
>
> 	CFRelease(aCFString);
> 	CFRelease(bCFString);
>
> 	return returnValue;
> }
>
> The result is as follow:
>
> First fragment:
>
> <item type="Struct">
> <item name="path">Links/lets&dance.jpg</item>
> <item name="spread_index">0</item>
> </item>
> <item type="Struct">
> <item name="path">Links/doubles_1/lets&dance.jpg</item>
> <item name="spread_index">0</item>
> </item>
>
> Escaping this and put in a second fragment gives:
>
> <item type="Struct">
> <item name="level_id">4_2</item>
> <item name="images" type="Array">&lt;item type=&quot;Struct&quot;&gt;
> &lt;item name=&quot;path&quot;&gt;Links/lets&amp;dance.jpg&lt;/item&gt;
> &lt;item name=&quot;spread_index&quot;&gt;0&lt;/item&gt;
> &lt;/item&gt;
> &lt;item type=&quot;Struct&quot;&gt;
> &lt;item name=&quot;path&quot;&gt;Links/doubles_1/lets&amp;dance.jpg&lt;/item&gt;
> &lt;item name=&quot;spread_index&quot;&gt;0&lt;/item&gt;
> &lt;/item&gt;
> </item>
> </item>
>
> Escaping this fragment again gives:
>
> &lt;item type=&quot;Struct&quot;&gt;
> &lt;item name=&quot;level_id&quot;&gt;4_2&lt;/item&gt;
> &lt;item name=&quot;images&quot; type=&quot;Array&quot;&gt;قÄ-&amp;lt;قÄ،item typeقÄ-=&amp;quot;قÄ،StructقÄ-&amp;quot;&amp;gt;قÄ،
> قÄ-&amp;lt;قÄ،item nameقÄ-=&amp;quot;قÄ،pathقÄ-&amp;quot;&amp;gt;قÄ،Links/lets&amp;amp;dance.jpgقÄ-&amp;lt;/قÄ،itemقÄ-&amp;gt;قÄ،
> قÄ-&amp;lt;قÄ،item nameقÄ-=&amp;quot;قÄ،spread_indexقÄ-&amp;quot;&amp;gt;قÄ،0قÄ-&amp;lt;/قÄ،itemقÄ-&amp;gt;قÄ،
> قÄ-&amp;lt;/قÄ،itemقÄ-&amp;gt;قÄ،
> قÄ-&amp;lt;قÄ،item typeقÄ-=&amp;quot;قÄ،StructقÄ-&amp;quot;&amp;gt;قÄ،
> قÄ-&amp;lt;قÄ،item nameقÄ-=&amp;quot;قÄ،pathقÄ-&amp;quot;&amp;gt;قÄ،Links/doubles_1قÄ-/قÄ،lets&amp;amp;dance.jpgقÄ-&amp;lt;/قÄ،itemقÄ-&amp;gt;قÄ،
> قÄ-&amp;lt;قÄ،item nameقÄ-=&amp;quot;قÄ،spread_indexقÄ-&amp;quot;&amp;gt;قÄ،0قÄ-&amp;lt;/قÄ،itemقÄ-&amp;gt;قÄ،
> قÄ-&amp;lt;/قÄ،itemقÄ-&amp;gt;قÄ،
> &lt;/item&gt;
> &lt;/item&gt;
>
>
> Has anyone a solution for this problem?
>
> Thanks a lot for your help.


You didn't show the relevant code. (Your code above performs a couple of unnecessary copies).

On Lion 10.7.2,  using Xcode 4.2.1 with default Foundation Template, this works for me:


int main (int argc, const char * argv[])
{
    @autoreleasepool {

        CFStringRef input = CFSTR("<item type=\"Struct\">\n"
                                  "<item name=\"path\">Links/lets&dance.jpg</item>\n"
                                  "<item name=\"spread_index\">0</item>\n"
                                  "</item>\n"
                                  "<item type=\"Struct\">\n"
                                  "<item name=\"path\">Links/doubles_1/lets&dance.jpg</item>\n"
                                  "<item name=\"spread_index\">0</item>\n"
                                  "</item>");


        NSLog(@"input:\n%@\n\n", input);
        CFStringRef firstOut = CFXMLCreateStringByEscapingEntities(
                                                          kCFAllocatorDefault,
                                                          input,
                                                          NULL);
        NSLog(@"output:\n%@\n\n", (id)firstOut);


        NSString* format =  @"<item type=\"Struct\">\n"
                            @"<item name=\"level_id\">4_2</item>\n"
                            @"<item name=\"images\" type=\"Array\">%@</item>\n"
                            @"</item>";

        NSString* second = [NSString stringWithFormat:format, (id)firstOut];

        CFStringRef secondOut = CFXMLCreateStringByEscapingEntities(
                                                                   kCFAllocatorDefault,
                                                                   (CFStringRef)second,
                                                                   NULL);
        NSLog(@"secondOut:\n%@\n\n", (id)secondOut);

        CFRelease(firstOut);
        CFRelease(secondOut);
    }
    return 0;
}


2011-12-02 14:06:21.593 XMLEscape[61121:707] input:
<item type="Struct">
<item name="path">Links/lets&dance.jpg</item>
<item name="spread_index">0</item>
</item>
<item type="Struct">
<item name="path">Links/doubles_1/lets&dance.jpg</item>
<item name="spread_index">0</item>
</item>

2011-12-02 14:06:21.595 XMLEscape[61121:707] output:
&lt;item type=&quot;Struct&quot;&gt;
&lt;item name=&quot;path&quot;&gt;Links/lets&amp;dance.jpg&lt;/item&gt;
&lt;item name=&quot;spread_index&quot;&gt;0&lt;/item&gt;
&lt;/item&gt;
&lt;item type=&quot;Struct&quot;&gt;
&lt;item name=&quot;path&quot;&gt;Links/doubles_1/lets&amp;dance.jpg&lt;/item&gt;
&lt;item name=&quot;spread_index&quot;&gt;0&lt;/item&gt;
&lt;/item&gt;

2011-12-02 14:06:21.596 XMLEscape[61121:707] secondOut:
&lt;item type=&quot;Struct&quot;&gt;
&lt;item name=&quot;level_id&quot;&gt;4_2&lt;/item&gt;
&lt;item name=&quot;images&quot; type=&quot;Array&quot;&gt;&amp;lt;item type=&amp;quot;Struct&amp;quot;&amp;gt;
&amp;lt;item name=&amp;quot;path&amp;quot;&amp;gt;Links/lets&amp;amp;dance.jpg&amp;lt;/item&amp;gt;
&amp;lt;item name=&amp;quot;spread_index&amp;quot;&amp;gt;0&amp;lt;/item&amp;gt;
&amp;lt;/item&amp;gt;
&amp;lt;item type=&amp;quot;Struct&amp;quot;&amp;gt;
&amp;lt;item name=&amp;quot;path&amp;quot;&amp;gt;Links/doubles_1/lets&amp;amp;dance.jpg&amp;lt;/item&amp;gt;
&amp;lt;item name=&amp;quot;spread_index&amp;quot;&amp;gt;0&amp;lt;/item&amp;gt;
&amp;lt;/item&amp;gt;&lt;/item&gt;
&lt;/item&gt;


Regards
Andreas

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Escape xml with CFXMLCreateStringByEscapingEntities (From: Claude Saelens <email@hidden>)

  • Prev by Date: Re: I've Got Those NSKeyedUnarchiver Blues!
  • Next by Date: Re: Escape xml with CFXMLCreateStringByEscapingEntities
  • Previous by thread: Re: Escape xml with CFXMLCreateStringByEscapingEntities
  • Next by thread: launchctl agents unload clean up
  • Index(es):
    • Date
    • Thread