Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One approach is to make a mutable copy of the stringValue of the <code>para</code> node and then insert your tags around the "someMethod" text. Create a new NSXMLNode from that using <code>-[NSXMLNode initWithXMLString:error:]</code> and replace the old NSXMLNode with the new NSXMLNode. That's probably shorter, but it requires some string manipulation.</p> <p>If you know the <code>para</code> node is a single run of text, then you can use this category on NSXMLNode I just wrote which seems a bit more verbose to me than what I described. Depends on what your needs are and how much you like messing around with NSMutableStrings. :)</p> <pre><code>@implementation NSXMLElement (ElementSplitting) - (void)splitTextAtRangeInStringValue:(NSRange)newNodeRange withElement:(NSString *)element { /* This is pretty simplistic; it assumes that you're attempting to split an element node (the receiver) with a single stringValue. If you need to do anything more complicated, you'll have to do some more work. For this limited example, we need three new nodes(!): 1. One new text node for the first part of the original string 2. One new element node with a stringValue of the annotated part of the string 3. One new text node for the tail part of the original string An alternate approach is to use -[NSXMLNode initWithXMLString:error:] after making a mutable copy of the string and modifying that string with the new markup you want. */ NSXMLNode *prefaceTextNode = [[NSXMLNode alloc] initWithKind:NSXMLTextKind]; NSXMLElement *elementNode = [[NSXMLNode alloc] initWithKind:NSXMLElementKind]; NSXMLNode *suffixTextNode = [[NSXMLNode alloc] initWithKind:NSXMLTextKind]; NSString *fullStringValue = [self stringValue]; NSString *prefaceString = [fullStringValue substringToIndex:newNodeRange.location]; NSString *newElementString = [fullStringValue substringWithRange:newNodeRange]; NSString *suffixString = [fullStringValue substringFromIndex:newNodeRange.location + newNodeRange.length]; [prefaceTextNode setStringValue:prefaceString]; [elementNode setName:element]; [elementNode setStringValue:newElementString]; [suffixTextNode setStringValue:suffixString]; NSArray *newChildren = [[NSArray alloc] initWithObjects:prefaceTextNode, elementNode, suffixTextNode, nil]; for (id item in newChildren) { [item release]; } // The array owns these now. [self setChildren:newChildren]; [newChildren release]; } @end </code></pre> <p>...and here's a small example:</p> <pre><code>NSString *xml_string = @"&lt;para&gt;This is some text about something.&lt;/para&gt;"; NSError *xml_error = nil; NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:xml_string options:NSXMLNodeOptionsNone error:&amp;xml_error]; NSXMLElement *node = [[doc children] objectAtIndex:0]; NSString *childString = [node stringValue]; NSRange splitRange = [childString rangeOfString:@"text about"]; [node splitTextAtRangeInStringValue:splitRange withElement:@"codeVoice"]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload