首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSXMLElement在文本块中间添加标记

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

Stack Overflow用户
提问于 2010-09-05 11:32:15
回答 4查看 1.8K关注 0票数 2

我需要创建一个具有以下格式类型的XML文档。

这是一些你需要知道的文本。

我对一般的XML生成没有问题,但这一方面给我带来了一些问题。

谢谢大家

史考特

EN

回答 4

Stack Overflow用户

发布于 2010-09-05 13:10:06

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

如果您知道para节点是一个单独的文本串,那么您可以在我刚刚编写的NSXMLNode上使用这个类别,它在我看来比我所描述的要更详细一些。这取决于您的需求以及您有多喜欢摆弄NSMutableStrings。:)

代码语言:javascript
复制
@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

...and这里有一个小示例:

代码语言:javascript
复制
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"];
票数 3
EN

Stack Overflow用户

发布于 2010-09-05 12:43:33

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

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

票数 1
EN

Stack Overflow用户

发布于 2010-09-05 13:45:01

哇,谢谢Chris和Pixel

是的,一开始我只是用括号把名字括起来,而不是用codeVoice标签把someMethod括起来,这是一个错误。

但现在一切都很好。多亏了你们,我才产生了我需要的东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3644907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档