开发者

iPhone - Find and replace multiple occurences of tags and contents from a String with regular expressions

I have a file (let's say a mutable String) with multiple tags like this one :

<Tag><minitag>40.23,12.7</minitag></Tag>

And I'd like to replace each o开发者_C百科ne by this one :

<Title>some hard text</Title><Value>40.23</Value><Title>some other hard text</Title><Value>12.7</Value>

using [xmlString replaceOccurrencesOfString:@"???????" withString:@"???????" options:NSRegularExpressionSearch range:NSMakeRange(0, [xmlString length])];

Or any other method if this one cannot achieve this goal in a couple of minutes.

Of course, the file contains many other things, numbers, commas, ...

How may I do that ?


Whether your can rewrite the xml in a couple minutes would depend on the size of the xml but a couple minutes in compute time is a l o n g time.

I don't think you'll accomplish this with simple string replace. I think you will need to parse the xml and as it's parsing, you need to break apart the string (split on commas etc...) and in parallel be writing the new xml document in the format you want.

Here's a quick example parsing the string you had. Of course if it's more complicated in reality, the custom string parsing becomes more complicated:

- (void) DoWork
{
    NSString *xmlSource = @"<Tag><minitag>40.23,12.7</minitag></Tag>";
    NSData *data = [xmlSource dataUsingEncoding:NSUTF8StringEncoding];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName caseInsensitiveCompare:@"minitag"] == NSOrderedSame)
    {
        NSLog(@"minitag");
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"chars:%@", string);
    NSArray *components = [string componentsSeparatedByString:@","];
    for (NSString *comp in components)
    {
        NSLog(@"comp:%@", comp);

        // This outputs:
        // minitag
        // chars:40.23,12.7
        // comp:40.23
        // comp:12.7    

        //
        // At this point, you have the parts parsed, maintain an NSXmlDocument 
        // (or other xml writer)
        // that you are writing to as you're parsing this
        // Add your constant string elements and your sub data elements as you go
        //
    }
}


dunno about regexp, the most eficient way as i can see - parse xml to extract the numbers, after it create new one and place the numbers there.


Found it :

[xmlString replaceOccurrencesOfString:@"<Tag><miniTag>([0-9.-]*){1},([0-9.-]*){1}</miniTag></Tag>" withString:@"<Title>Some hard text</Title><Value>$1</Value><Title>Some other hard text</Title><Value>$2</Value>" options:NSRegularExpressionSearch range:NSMakeRange(0, [xmlString length])];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜