0

我是 Iphone 新手,我知道有很多可用的教程,我也搜索了很多,但仍然无法读取以下 XML 文件。希望你能帮助我。我想阅读以下内容。1. ViewCounts 2. Media:Thumbnail 3. Media:Title 4. NumRates 5. Duration

提前致谢。

<?xml version="1.0" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <id>
    http://gdata.youtube.com/feeds/api/videos
  </id>
  <updated>
    2012-04-08T15:09:04.640Z
  </updated>
  <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/>
  <title type="text">
    YouTube Videos matching query: pakistan
  </title>
  <logo>
    http://www.youtube.com/img/pic_youtubelogo_123x63.gif
  </logo>
  <link href="http://www.youtube.com" rel="alternate" type="text/html"/>
  <link href="http://gdata.youtube.com/feeds/api/videos" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"/>
  <link href="http://gdata.youtube.com/feeds/api/videos/batch" rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml"/>
  <link href="http://gdata.youtube.com/feeds/api/videos?alt=atom&start-index=1&max-results=1&vq=pakistan&orderby=relevance" rel="self" type="application/atom+xml"/>
  <link href="http://gdata.youtube.com/feeds/api/videos?alt=atom&start-index=2&max-results=1&vq=pakistan&orderby=relevance" rel="next" type="application/atom+xml"/>
  <author>
    <name>
      YouTube
    </name>
    <uri>
      http://www.youtube.com/
    </uri>
  </author>
  <generator uri="http://gdata.youtube.com" version="2.1">
    YouTube data API
  </generator>
  <openSearch:totalResults>
    1000000
  </openSearch:totalResults>
  <openSearch:startIndex>
    1
  </openSearch:startIndex>
  <openSearch:itemsPerPage>
    1
  </openSearch:itemsPerPage>
  <entry>
    <id>
      http://gdata.youtube.com/feeds/api/videos/QP9K2YeO6kE
    </id>
    <published>
      2012-04-08T05:51:35.000Z
    </published>
    <updated>
      2012-04-08T14:18:21.000Z
    </updated>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/>
    <category label="News & Politics" scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="News"/>
    <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Pakistan"/>
    <category scheme="http://gdata.youtube.com/schemas/2007/keywords.cat" term="Avalanche"/>
    <title type="text">
      Avalanche Buries More Than 100 Pakistani Troops
    </title>
    <content type="text">
      Pakistani soldiers dug into a massive avalanche in a mountain battleground close to the Indian border on Saturday, searching for at least 135 people buried when the wall of snow engulfed a military complex. (April 7)
    </content>
    <link href="http://www.youtube.com/watch?v=QP9K2YeO6kE&feature=youtube_gdata" rel="alternate" type="text/html"/>
    <link href="http://gdata.youtube.com/feeds/api/videos/QP9K2YeO6kE/responses" rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml"/>
    <link href="http://gdata.youtube.com/feeds/api/videos/QP9K2YeO6kE/related" rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml"/>
    <link href="http://m.youtube.com/details?v=QP9K2YeO6kE" rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html"/>
    <link href="http://gdata.youtube.com/feeds/api/videos/QP9K2YeO6kE" rel="self" type="application/atom+xml"/>
    <author>
      <name>
        AssociatedPress
      </name>
      <uri>
        http://gdata.youtube.com/feeds/api/users/AssociatedPress
      </uri>
    </author>
    <gd:comments>
      <gd:feedLink countHint="55" href="http://gdata.youtube.com/feeds/api/videos/QP9K2YeO6kE/comments" rel="http://gdata.youtube.com/schemas/2007#comments"/>
    </gd:comments>
    <yt:location>
      New York, NY
    </yt:location>
    <media:group>
      <media:category label="News & Politics" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">
        News
      </media:category>
      <media:content duration="53" expression="full" isDefault="true" medium="video" type="application/x-shockwave-flash" url="http://www.youtube.com/v/QP9K2YeO6kE?version=3&f=videos&app=youtube_gdata" yt:format="5"/>
      <media:content duration="53" expression="full" medium="video" type="video/3gpp" url="rtsp://v6.cache4.c.youtube.com/CiILENy73wIaGQlB6o6H2Ur_QBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" yt:format="1"/>
      <media:content duration="53" expression="full" medium="video" type="video/3gpp" url="rtsp://v4.cache8.c.youtube.com/CiILENy73wIaGQlB6o6H2Ur_QBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" yt:format="6"/>
      <media:description type="plain">
        Pakistani soldiers dug into a massive avalanche in a mountain battleground close to the Indian border on Saturday, searching for at least 135 people buried when the wall of snow engulfed a military complex. (April 7)
      </media:description>
      <media:keywords>
        Pakistan, Avalanche
      </media:keywords>
      <media:player url="http://www.youtube.com/watch?v=QP9K2YeO6kE&feature=youtube_gdata_player"/>
      <media:thumbnail height="360" time="00:00:26.500" url="http://i.ytimg.com/vi/QP9K2YeO6kE/0.jpg" width="480"/>
      <media:thumbnail height="90" time="00:00:13.250" url="http://i.ytimg.com/vi/QP9K2YeO6kE/1.jpg" width="120"/>
      <media:thumbnail height="90" time="00:00:26.500" url="http://i.ytimg.com/vi/QP9K2YeO6kE/2.jpg" width="120"/>
      <media:thumbnail height="90" time="00:00:39.750" url="http://i.ytimg.com/vi/QP9K2YeO6kE/3.jpg" width="120"/>
      <media:title type="plain">
        Avalanche Buries More Than 100 Pakistani Troops
      </media:title>
      <yt:duration seconds="53"/>
    </media:group>
    <gd:rating average="4.7647057" max="5" min="1" numRaters="17" rel="http://schemas.google.com/g/2005#overall"/>
    <yt:recorded>
      2012-04-08
    </yt:recorded>
    <yt:statistics favoriteCount="4" viewCount="662"/>
  </entry>
</feed>
4

2 回答 2

3

你可能想要NSXMLParser


使用 NSXMLParser

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

设置

主要是你需要给你的解析器一些东西来解析!在我的示例中,我从包中获取资源并将其转换为 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];
于 2012-04-08T16:20:16.043 回答
0

另一种选择是使用GDataXmlParser(在谷歌上搜索,准备好的文件)。你只需要传递标签,它就会返回你的字典。

于 2012-04-08T16:32:11.790 回答