0
我無法理解NSXMLParser。我嘗試了一些外部的XML解析器,但是我沒有運氣。我想要做的是解析GDATA Youtube API xml,它檢索視頻的xml。這工作正常,我有XML,但它很難通過元素重申。我只是需要一個例子,可以檢索元素,如(媒體:縮略圖URL =「」)(/媒體:縮略圖)無法理解NSXMLParser
如何我從元素媒體的URL值:縮略圖
我無法理解NSXMLParser。我嘗試了一些外部的XML解析器,但是我沒有運氣。我想要做的是解析GDATA Youtube API xml,它檢索視頻的xml。這工作正常,我有XML,但它很難通過元素重申。我只是需要一個例子,可以檢索元素,如(媒體:縮略圖URL =「」)(/媒體:縮略圖)無法理解NSXMLParser
如何我從元素媒體的URL值:縮略圖
這是你如何使用NSXMLParser
:
在您的.h文件中聲明:
NSMutableData *webPortFolio;
NSMutableString *soapResultsPortFolio;
NSURLConnection *conn;
//---xml parsing---
NSXMLParser *xmlParserPortFolio;
BOOL elementFoundPortFolio;
NSMutableURLRequest *req;
NSString *theXMLPortFolio;
NSString *strSoapMsg;
UIAlertView *alertView;
在您.m文件使用下面的代碼:
-(void)callURL
{
//Your logic to call URL.
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn)
{
webPortFolio = [[NSMutableData data] retain];
}
}
,並處理您可以使用以下功能的響應:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webPortFolio setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webPortFolio appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
{
NSLog(@"error...................%@",[error description]);
[webPortFolio release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
//Check the request and returns the response.
NSLog(@"DONE. Received Bytes: %d", [webPortFolio length]);
theXMLPortFolio = [[NSString alloc]
initWithBytes: [webPortFolio mutableBytes]
length:[webPortFolio length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(@"shows the XML %@",theXMLPortFolio);
[theXMLPortFolio release];
if(xmlParserPortFolio)
{
[xmlParserPortFolio release];
}
xmlParserPortFolio = [[NSXMLParser alloc] initWithData: webPortFolio];
[xmlParserPortFolio setDelegate: self];
[xmlParserPortFolio setShouldResolveExternalEntities:YES];
[xmlParserPortFolio parse];
[webPortFolio release];
[connection release];
}
//---when the start of an element is found---
-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName
namespaceURI:(NSString *) namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict
{
if([elementName isEqualToString:@"GetPortfolioListResult"])
{
if (!soapResultsPortFolio)
{
soapResultsPortFolio = [[NSMutableString alloc] init];
}
elementFoundPortFolio = TRUE;
NSLog(@"Registration...%@",soapResultsPortFolio);
}
else if([elementName isEqualToString:@"your_tag_name"])
{
elementFoundPortFolio = TRUE;
}
else if([elementName isEqualToString:@"your_tag_name"])
{
elementFoundPortFolio = TRUE;
}
else if([elementName isEqualToString:@"your_tag_name"])
{
elementFoundPortFolio = TRUE;
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFoundPortFolio)
{
[soapResultsPortFolio appendString: string];
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"Parser error %@ ",[parseError description]);
}
//---when the end of element is found---
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"GetPortfolioListResult"])
{
NSLog(@"display the soap results%@",soapResultsPortFolio);
}
else if([elementName isEqualToString:@"your_tag_name"])
{
//Perform required action
}
else if([elementName isEqualToString:@"your_tag_name"])
{
//Perform required action
}
else if([elementName isEqualToString:@"your_tag_name"])
{
//Perform required action
}
[soapResultsPortFolio setString:@""];
elementFoundPortFolio = FALSE;
}
編輯:
if ([elementName isEqualToString:@"media:thumbnail"])
{
//Save your URL in an Array
}
感謝您對烏爾回覆,但我明白,我不明白如何獲得媒體:縮略圖形式的XML文件 – redoc01 2013-02-15 13:14:05
@ redoc01:你正在獲取圖像的URL? – Rushi 2013-02-15 13:27:39
是的,只是第一個圖像不是所有的其他URL – redoc01 2013-02-15 13:28:59