我需要创建一个具有以下格式类型的XML文档。
这是一些你需要知道的文本。
我对一般的XML生成没有问题,但这一方面给我带来了一些问题。
谢谢大家
史考特
发布于 2010-09-05 13:10:06
一种方法是制作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...and这里有一个小示例:
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"];发布于 2010-09-05 12:43:33
如果<someMethod>实际上是一个元素,那么您需要创建一个NSXMLNode类型的NSXMLTextKind (通过initWithKind:),创建<someMethod>节点,并创建另一个文本节点,然后依次将这三个节点作为子节点添加到<Para>节点。关键是将两个文本部分创建为单独的节点。
在重读了这个问题之后,我在想,也许<someMethod>的本意不是一个节点,而应该是文本?如果是这样的话,这只是一个逃避问题的(< | >),但我猜,考虑到你是谁,这并不是那么简单的事情。:)
发布于 2010-09-05 13:45:01
哇,谢谢Chris和Pixel
是的,一开始我只是用括号把名字括起来,而不是用codeVoice标签把someMethod括起来,这是一个错误。
但现在一切都很好。多亏了你们,我才产生了我需要的东西。
https://stackoverflow.com/questions/3644907
复制相似问题