【问题标题】:Read "%" sign from xml in iphone从 iphone 中的 xml 读取“%”符号
【发布时间】:2012-01-14 16:52:47
【问题描述】:

我搜索了我的问题并在网上找到了各种解决方案,例如:

Using the British pound sign in an XML feed to be read by an iPhone

NOt able to parse "&"

Special Characters in NSString from HTML

This 和 here also

但这对我来说并不富有成效,因为在阅读了 xml 之后我将它保存在我的数据库中。

这里是xml:

<root>
    −<Discounts>
        <DiscountID>1</DiscountID>
        <Title>WeekEnd Offers</Title>
        <Description>Offer upto 50%</Description>
        <SalePrice>50</SalePrice>
        <RegularPrice>100</RegularPrice>
        <CreatedDate>11/14/2011</CreatedDate>
        <LastModifiedDate>11/14/2011</LastModifiedDate>
        <Status>1</Status>
        <PhotoPath>/photos/documents/coupzila/hiteshi-logo.png</PhotoPath>
    </Discounts>
</root>

m 获取字符串中的描述为:

  NSString * strDescription = [item.mdictXMLTagsData valueForKey:@"Description"];

    NSLog(@"String in description is : ",strDescription);

但描述字段在控制台中为空,我无法将其存储在我的数据库中。

请建议我如何读取特殊字符并将其存储到我的数据库中。

我开始知道 % 是阅读 % 符号的关键字,但我不知道如何应用它。

请帮忙!!!

编辑:我当前的代码。

-(void)CallAPIForDetails
{

//RequestType = 2;
//UIApplication sharedApplication].networkActivityIndicatorVisible=YES;

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ParsingAPI *api = [[ParsingAPI alloc]init];
api.delegate = self;


NSString *strurl = [@"" stringByAppendingFormat: @"http://192.168.1.108/Coupzila/api/default.aspx?method=GetData&RequestType=Discounts&StoreID=%d",StoreId];

NSLog(@"URL : %@",strurl);
strurl = [strurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

api.strEntityName=@"Discounts";

api.arrayXMLTags = nil; 
api.arrayXMLAttributeTags = nil;

[api parseXMLFileAtURL:[NSURL URLWithString:strurl] showProgress:NO];   

[api release];  
[pool release];

}

- (void)feedDataArray:(ParsingAPI *)feed didFindItemsArray:(NSMutableArray *)items
{
/*
 if(RequestType == 2 )
 {*/
NSMutableArray *arraydeleteDetails = [CoreDataAPIMethods getObjectsFromContext:@"DiscountsDetail" :@"DiscountId" :YES :self.managedObjectContext];

for (float i=0;i<[arraydeleteDetails count];i++) 
{
    [self.managedObjectContext deleteObject:[arraydeleteDetails objectAtIndex:i]];
    NSError *error;
    if (![self.managedObjectContext save:&error]) {
        // Handle the error...
    }
}   

for (float i=0;i<[items count];i++) 
{               
    ParsingItem *item = [items objectAtIndex:i];

    NSMutableArray *arr = [CoreDataAPIMethods getObjectsFromContext:@"DiscountsDetail" :@"Index" :NO :self.managedObjectContext];

    if ([arr count] > 0) 
    {
        DiscountsDetail * DiscountsDetailObject = [arr objectAtIndex:0];

        NewEntryID = [DiscountsDetailObject.Index intValue] +1;

    }
    else 
    {
        NewEntryID = 1;
    }

    /*
     if (countImage == 0) 
     {
     countImage = NewEntryID;

     }*/


    //Now add the data in database 
    DiscountsDetail *data = (DiscountsDetail*)[NSEntityDescription insertNewObjectForEntityForName:@"DiscountsDetail" inManagedObjectContext:self.managedObjectContext];

    data.DiscountId = [NSNumber numberWithInt:[[item.mdictXMLTagsData valueForKey:@"DiscountID"] intValue ]];
    data.Title = [item.mdictXMLTagsData valueForKey:@"Title"];
    data.SalePrize = [item.mdictXMLTagsData valueForKey:@"SalePrice"];
    data.RegularPrize = [item.mdictXMLTagsData valueForKey:@"RegularPrice"];
    //data.Description = @"This is test descriptionsakdj bfaisdfj bgasdlkab gfdil ufghiub nrungdfg bndfiosggn iouer5nn gdfnsngdfsu gndklfngdufn gkdfjlbgn lidsfub gdilufbf gldfb ghdfsiosa ifgho aibfg iodfb dfug hbdfg iygofadqi igdfhaibg adofibg ifdbgioadfbg adfigyb asg ig agiobfg finish" ;  


  NSString * strDescription = [item.mdictXMLTagsData valueForKey:@"Description"];

    NSXMLParser * parser = [item.mdictXMLTagsData valueForKey:@"Description"];

    [self parser:parser foundCharacters:strDescription];
    NSLog(@"String in description is : ",strDescription);

    data.Description =[item.mdictXMLTagsData valueForKey:@"Description"];
    data.Index = [NSNumber numberWithInt:NewEntryID];


    NSLog(@"Description is :",data.Description);


    if ([item.mdictXMLTagsData valueForKey:@"PhotoPath"] != nil)
    {

        NSString *photopath=@"http://192.168.1.108/Coupzila/";

        NSData *dataimage=[NSData dataWithContentsOfURL:[NSURL URLWithString:[photopath stringByAppendingString:[item.mdictXMLTagsData valueForKey:@"PhotoPath"]]]];

        data.ImageDiscounts = dataimage;

    }


    NSError *error;             

    if (![self.managedObjectContext save:&error])
    {
        // Handle the error...
    }

    self.arrayDiscountDetails = [CoreDataAPIMethods getObjectsFromContext:@"DiscountsDetail" :@"DiscountId" :NO :self.managedObjectContext];

}

}

【问题讨论】:

  • 你可以改变编码:NSASCIIStringEncoding
  • 如何做到这一点???我应该导入任何框架吗??
  • 您必须在 XML 解析代码中进行。你用过哪个解析器?
  • 据我所知,解析不需要框架。 NSXmlparser 一般用于解析。我可能在这里错了。
  • 我是 iphone 开发的新手,即使我不知道如何忽略解析中的特殊字符,因为我没有通过它的知识。

标签: iphone objective-c xml ios4 xml-parsing


【解决方案1】:

已编辑:- 希望您正在初始化 currentParsedCharacter...

这不是你的确切代码......但它应该是相似的......试试看,或者为你尝试做的事情提供代码,我们可以以更好的方式提供帮助。

-(id)init{
    if(self == [super init]){
        ObjectsMutableArray = [[NSMutableArray alloc] init];
        currentParsedCharacterData = [[NSMutableString alloc]init];
    }
    return self; 
}


- (void)parserDidStartDocument:(NSXMLParser *)parser
{

}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;

    if([currentElement isEqualToString:@"Table"]) {
        obj = [[SomeClass alloc]init];
    }

    if ( [currentElement isEqualToString:@"Description"])
    {

        self.currentParsedCharacterData =(NSMutableString *) @"";
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([currentElement isEqualToString:@"OtherThanDescription"]) {
        obj.CatName=[NSString stringWithFormat:@"%@",string];
        NSLog(@"CatName to be saved in Array :- %@",string);
    }

    else if([currentElement isEqualToString:@"Description"]) {

        NSMutableString *outputBuilder =  [[NSMutableString alloc]init] ;

        [outputBuilder appendString:[NSString stringWithFormat:@"%@", self.currentParsedCharacterData]];

        [outputBuilder appendString:[NSString stringWithFormat:@"%@", string]];

        self.currentParsedCharacterData = outputBuilder;

        [outputBuilder release];
    }

    else 
    {
        self.currentParsedCharacterData = (NSMutableString *)string;
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"Table"]) {
        NSLog(@"Current element in End Element Category:- %@",currentElement);
        [ObjectsMutableArray addObject:obj];

    }
    if([elementName isEqualToString:@"Description"]) {
        obj.CatDesc = self.currentParsedCharacterData;
        NSLog(@"Current element in End Element Category:- %@",obj.CatDesc);

    }
    currentElement = @"";
}



- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
    appDelegate.ObjectsArray = [[NSMutableArray alloc]initWithArray:ObjectsMutableArray];

}

// 释放变量然后...

【讨论】:

  • 我想问一个愚蠢的问题,上面代码中的当前元素是什么?当我用我的数据库类对象替换它时。描述,它是否正确???
  • 呵呵... currentElement 在 didStartElement 方法中用于知道我们正在解析哪个节点... 它保存了我正在为其解析的节点 foundCharacters 方法
  • 再次转到答案,我已对其进行了修改...给出了我的一段代码..试试这个...如果它有助于投票给我或勾选答案..: )
猜你喜欢
  • 1970-01-01
  • 2013-02-07
  • 2023-03-03
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多