使用NSXMLElement在文本块的中间添加标记

时间:2010-09-05 03:32:15

标签: objective-c nsxml

我需要使用以下格式创建XML文档。

<帕拉>

这是关于< someMethod>的一些文字。你需要知道的。

< /帕拉>

我对通用XML生成没有任何问题,但这一方面给了我一些问题。

谢谢大家

斯科特

4 个答案:

答案 0 :(得分:3)

一种方法是制作para节点的stringValue的可变副本,然后在“someMethod”文本周围插入标记。使用-[NSXMLNode initWithXMLString:error:]从中创建一个新的NSXMLNode,并用新的NSXMLNode替换旧的NSXMLNode。这可能更短,但需要一些字符串操作。

如果您知道para节点是单行文本,那么您可以在我刚写的NSXMLNode上使用此类别,这对我来说比我描述的更冗长。取决于你的需求是什么以及你喜欢搞多少NSMutableStrings。 :)

@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

......这是一个小例子:

NSString *xml_string = @"<para>This is some text about something.</para>";
NSError *xml_error = nil;
NSXMLDocument *doc = [[NSXMLDocument alloc] initWithXMLString:xml_string options:NSXMLNodeOptionsNone error:&xml_error];

NSXMLElement *node = [[doc children] objectAtIndex:0];
NSString *childString = [node stringValue];    
NSRange splitRange = [childString rangeOfString:@"text about"];

[node splitTextAtRangeInStringValue:splitRange withElement:@"codeVoice"];

答案 1 :(得分:1)

如果<someMethod>实际上是一个元素,那么你需要创建一个类似NSXMLTextKind的NSXMLNode(通过initWithKind :),创建你的<someMethod>节点,并创建另一个文本节点,然后添加所有三个作为子节点订购<Para>节点。关键是将两个文本部分创建为单独的节点。

重读这个问题之后,我想也许<someMethod>不是一个节点,而应该是文本?如果是这样,那只是一个逃避问题(&lt; | &gt;),但我猜这不是那么简单,考虑到你是谁。 :)

答案 2 :(得分:1)

哇,谢谢Chris和Pixel。

是的,最初我错误地使用了名称周围的括号而不是someMethod周围的codeVoice标记。

但现在一切都很顺利。我正在生产我需要的东西,谢谢你们。

答案 3 :(得分:0)

不确定为什么我的更正没有发布......

这是关于&lt; codeVoice&gt; someMethod&lt; / codeVoice&gt;的一些文字。你需要知道的。

这就是我需要复制的内容。