字符串替换操作在目标c中

时间:2012-03-01 12:39:57

标签: objective-c xml xcode xml-parsing

我得到的结果是

<ZmaterialGroupList><Matkl>001</Matkl><Text>Metal processing</Text></ZmaterialGroupList>

我需要将结果作为001 Metal处理,当我应用字符串替换函数时,它会给出异常。请帮助我。

添加了评论代码:

for(int i=0; i<[soapArray.items count]; i++) {
    NSString *str = [soapArray.items objectAtIndex:i];
    str = [str stringByReplacingOccurrencesOfString:@"<Matkl>" withString:@""];
}

通过这种方式我写了,但我有一个例外 str

传递的参数无效

2 个答案:

答案 0 :(得分:2)

您最好使用NSXMLParser而不是尝试替换XML片段。

如果你更新你的问题以包含更多解释你的代码(特别是处理soapArray的代码),我应该能够解释为什么你的代码不能正常工作。


使用NSXMLParser

重要的是要记住,NSXMLParser只是按顺序读取您提供的数据,它不使用DOM结构。

设置

主要是你需要给你的解析器解析一些东西!在我的示例中,我从bundle获取资源并将其转换为NSData。然而,initWithContentsOfURL还有另一种选择。确保你不要忘记设置你的代表!

-(void) parse
{
    NSString *file = @"myXMLFile.xml";
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[file stringByDeletingPathExtension] ofType:[file pathExtension]]];
    //If you already have a string:
    //NSData* data=[xmlString dataUsingEncoding:NSUTF8StringEncoding];
    if (data.length == 0)
    {
        //No data
        return nil;
    }

    ...
}

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];

解析

-(void) parserDidStartDocument:(NSXMLParser *)parser
{
   //Here you set up any variables you might need while parsing
}

-(void) parserDidEndDocument:(NSXMLParser *)parser
{
   //I usually don't have to do anything here but you might need to release some variables here for example.
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    //Here is where the bulk of the parsing is done if you are using attributes. I prefer to use attributes as it looks cleaner both in the XML file and the parser.
    if ([elementName isEqualToString:@"element1"])
    {
        //Just an example of what you might want to do
        int index = [[attributeDict valueForKey:@"attribute1"] intValue];
        NSString *name = [attributeDict valueForKey:@"n"];
        [exampleDictionary setValue:name forKey:[NSString stringWithFormat:@"%d", index]];
    }
    if ([elementName isEqualToString:@"element2"])
    {
        //We need to know that the next piece of information (from foundCharacters) is for element 2
        currentElement = ELEMENT_2;
    }

}

//If you haven't used attributes you might find that you have a lot of parsing to do here instead.
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 
    //Check what this info is for?
    if(currentElement == ELEMENT_2)
    {
        element2Data = [NSString stringWithString:string];
    }
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{    
    if ([elementName isEqualToString:@"element2"])
    {
        myObject.somethingThatNeedsElement2 = element2;
    }
}

整理

完成之后,最好检查没有出错并释放解析器内存。因此,在parse方法中,请在调用[parser parse]后添加此内容。

if ([parser parserError] != nil)
{
    [[[[UIAlertView alloc] initWithTitle:@"Error parsing XML" message:[[parser parserError] localizedDescription] delegate:nil cancelButtonTitle:@"Done" otherButtonTitles:nil] autorelease] show];
}
[parser release];

答案 1 :(得分:0)

考虑使用XML解析器,或者您可以使用NSScanner

示例:

NSString *wanted;
NSString *fullMessage;
NSString *xml = @"<ZmaterialGroupList><Matkl>001</Matkl><Text>Metal processing</Text></ZmaterialGroupList>";
NSScanner *scanner = [NSScanner scannerWithString:xml];

[scanner scanUpToString:@"<Matkl>" intoString:nil];
[scanner scanString:@"<Matkl>" intoString:nil];
[scanner scanUpToString:@"</Matkl>" intoString:&wanted];
fullMessage = wanted;

[scanner scanUpToString:@"<Text>" intoString:nil];
[scanner scanString:@"<Text>" intoString:nil];
[scanner scanUpToString:@"</Text>" intoString:&wanted];
fullMessage = [fullMessage stringByAppendingFormat:@" %@", wanted];

NSLog(@"fullMessage: '%@'", fullMessage);

NSLog输出:
fullMessage:'001 Metal processing'