3

For my application I have to save an XML document containing a few elements with HTML-text. Example as the result should be:

<gpx>
  <wpt>
  <elementInHTML>
    &lt;p&gt;Sample text.&lt;/p&gt;
  </elementInHTML>
etc...

But when I add this html element to my NSXMLDocument the '<' (to &lt;) is correctly escaped automatically, but the '>' not (to &gt;).

In code:

NSXMLElement *newWPT = [NSXMLElement elementWithName:@"wpt"];
NSXMLElement *htmlElement = [NSXMLElement elementWithName:@"elementInHTML"];
htmlElement.stringValue = @"<Sample text>";
[newWPT addChild:htmlElement];

But this results in an XML document like this:

<gpx>
  <wpt>
    <elementInHTML>
      &lt;p>Sample text.&lt;/p>
    </elementInHTML>
etc...

And this result is not valid for the device that has to process this xml file.

Anybody an idea how to enclose a correctly escaped html-string into a NSXMLDocument?

Marius
  • 258
  • 1
  • 9
  • I'm not able to reproduce your problem. When I use `htmlElement.stringValue = @"

    Sample text

    ";` in your example, I get `<p>Sample text</p>` (Using XCode 5.1.1).
    – helderdarocha May 31 '14 at 04:09
  • The greater than character is valid in a xml value, seems a device specific problem with it's implementation. Did you try using CDATA? – miguel-svq Jun 02 '14 at 00:15
  • @helderdarocha I get that, too, when using Mac OS X 10.9, but not on 10.8 and earlier. – Michael Tsai Jun 04 '14 at 14:05
  • Funny that a year old post now gets several answers :-) Using CDATA is not a working solution for this. – Marius Jun 05 '14 at 19:20
  • 1
    I had to look up how I solved the problem. It appeared there were other characters that the (GPS) device didn't accept. As there are control characters between 0x00 and 0x1F, apart from \t, \n, \f and \r. My solution was pretty hacky: 1) generate the NSXMLDocument. 2) convert it (via NSData) into a NSString 3) Scan and replace the invalid characters 4) Convert NSString back to NSData and save to file. – Marius Jun 05 '14 at 19:34
  • @Marius Interesting thing. One year ago, with the exact information about the string (those other "strange" characters) could have lead to a different conclusion, as said here: http://www.cocoabuilder.com/archive/cocoa/157815-nsxmldocument-question.html#157841 : @"whatever" only works with ASCII. Or (staying here) point to http://stackoverflow.com/questions/8086584/objective-c-url-encoding ( "url" encode the string, using ) – miguel-svq Jun 06 '14 at 15:41
  • @Maurius, my upvote for your comment goes because I think that's the right thing: share knowledge. Post your comment as and answser, please, if I ever forget your workaround I'll find it easily :) – miguel-svq Jun 06 '14 at 18:22

1 Answers1

3

&The string is correctly scaped for XML, greater than is a valid character where it is: http://www.w3.org/TR/REC-xml/#syntax

It seems it's a device implementation specific problem.

Your easy option is to include your html markup in a CDATA. ...and hope the device client XML parser implementation understand it properly.

(If your html markup include also CDATA sections you'll have to find/replace ">" with "&gt;", as stated in the link before.)

P.D.: NSXMLNode CDATA in any search engine will lead you to something closer to "copy-paste"

EDIT:

Knowing now more about the content of the string in the original question (see question comments) and depending on the nature of your string answers to this other question may also help: Objective-C and Swift URL encoding

Community
  • 1
  • 1
miguel-svq
  • 2,136
  • 9
  • 11