2

我的 XML

<MemberBookDTO>
   <BookKey>abc</BookKey>
   <OrderLineKey>5f8-</OrderLineKey>
   <ValidationHash>512D0</ValidationHash>
   <BookId>101</BookId>
   <Name>Dönüş</Name>
   <Author>Abc</Author>
   <Narrator>Apn</Narrator>
   <OrderDateAsText>01.01.2011 15:00</OrderDateAsText>
   <Description>abcd123456.</Description>
   <TotalDurationAsSec>0</TotalDurationAsSec>
   <TotalSizeAsByte>658943266</TotalSizeAsByte>
   <ListImageURL>http://abc.com/SeslenenKitap/File/Picture/abc.png</ListImageURL>
   <DetailImageURL>http://abc.com/SKitap/File/Picture/abc.png</DetailImageURL>
   <PlayerImageURL>http://abc.com/SKitap/File/Picture/abc.png</PlayerImageURL>
   <ImageSign>63514212345693424</ImageSign>
   <Chapters>
      <ChapterDTO>
         <Id>350</Id>
         <Name>Bolum 1 - Kaybolmak</Name>
         <Sort>1</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/61efdf42-ce30-4c52-aa4f-84748fa28343.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>36</Id>
         <Name>Bolum 2 - Kacis</Name>
         <Sort>2</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://abc.com/Kitap/File/Audio/730a25a9-a01f-41ee-acda-3c23edbe078a.mp3</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>37</Id>
         <Name>Bölüm 3 - Karşılaşma</Name>
         <Sort>3</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/7d029835-99a6-4c24-b9f8-735d1496be2e.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>38</Id>
         <Name>Bolum 4 - Kavusma</Name>
         <Sort>4</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/f9e7535f-5ffb-4be8-ad00-67be3906bffe.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>39</Id>
         <Name>Bolum 5 - Yuzlesme</Name>
         <Sort>5</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/ae4cdab9-7fdc-4c9b-b2b7-cec7299d2353.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>40</Id>
         <Name>Bolum 6 - Itiraf</Name>
         <Sort>6</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/60bfc2f9-9fe0-4c90-bdd0-c6253bca7056.ses</DownloadURL>
      </ChapterDTO>
      <ChapterDTO>
         <Id>41</Id>
         <Name>Bolum 7 - Donus</Name>
         <Sort>7</Sort>
         <DurationAsSec>0</DurationAsSec>
         <DownloadURL>http://test.magnifo.com/SeslenenKitap/File/Audio/45b31c6a-9b38-4b00-91f1-4d9153dff138.mp3</DownloadURL>
      </ChapterDTO>
   </Chapters>
</MemberBookDTO>

目标 C 代码:

 -(id) loadXMLByURL:(NSString *)urlString
{
    bookListArray= [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:urlString];
    parser= [[NSXMLParser alloc] initWithContentsOfURL:url];
    parser.delegate=self;
    [parser parse];
    return self;
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"MemberBookDTO"])
    {
        currentBook = [[BookListInfo alloc] init];
        currentNodeContentBook=[[NSMutableString alloc] init];
    }

    else if ([elementname isEqualToString:@"Chapters"])
    {
        //currentBook = [[BookListInfo alloc] init];
        currentNodeContentChapters=[[NSMutableString alloc] init];
    }


}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
  //  bookKey,orderLineKey,bookAuthor,bookDescription,bookDuration,bookId,bookName,bookNarrator,bookOrderDate,bookSizeAtByte,listImageUrl,detailImageUrl,playerImageUrl,imageSign;
    if ([elementname isEqualToString:@"BookKey"])
    {
        currentBook.bookKey = currentNodeContentBook;
       // NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"OrderLineKey"])
    {
        currentBook.orderLineKey = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"ValidationHash"])
    {
        currentBook.validationKey = currentNodeContentBook;
       // NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"BookId"])
    {
        currentBook.bookId = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"Name"])
    {
        currentBook.bookName = currentNodeContentBook;
       // NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"Author"])
    {
        currentBook.bookAuthor = currentNodeContentBook;
       // NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"Narrator"])
    {
        currentBook.bookNarrator = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"OrderDateAsText"])
    {
        currentBook.bookOrderDate = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"Description"])
    {
        currentBook.bookDescription = currentNodeContentBook;
        //NSLog(@"Description: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"TotalDurationAsSec"])
    {
        currentBook.bookDuration = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"TotalSizeAsByte"])
    {
        currentBook.bookSizeAtByte = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"ListImageURL"])
    {
        currentBook.listImageUrl = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"DetailImageURL"])
    {
        currentBook.detailImageUrl = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }
    if ([elementname isEqualToString:@"PlayerImageURL"])
    {
        currentBook.playerImageUrl = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }

    if ([elementname isEqualToString:@"ImageSign"])
    {
        currentBook.imageSign = currentNodeContentBook;
        //NSLog(@"currentNodeContent: %@",currentNodeContent);
    }

   else if ([elementname isEqualToString:@"Chapters"])
    {
        if ([elementname isEqualToString:@"Id"])
        {
            currentBook.chapterId = currentNodeContentChapters;
            NSLog(@"chapterId: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");

        }
        if ([elementname isEqualToString:@"Name"])
        {
            currentBook.chapterName = currentNodeContentChapters;
            NSLog(@"chapterName: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");



        }

        if ([elementname isEqualToString:@"Sort"])
        {
            currentBook.chapterSort = currentNodeContentChapters;
            NSLog(@"chapterSort: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");



        }
        if ([elementname isEqualToString:@"DurationAsSec"])
        {
            currentBook.chapterDurationAsSec = currentNodeContentChapters;
            NSLog(@"DurationAsSec: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");



        }

        if ([elementname isEqualToString:@"DownloadURL"])
        {
            currentBook.chapterDownloadUrl = currentNodeContentChapters;
            NSLog(@"chapterDownloadUrl: %@",currentNodeContentChapters);
            NSLog(@"___________________________________");



        }


    }


    if ([elementname isEqualToString:@"MemberBookDTO"])
    {
        [bookListArray addObject:currentBook];
        currentBook = nil;
        currentNodeContentBook = nil;
    }

    else if ([elementname isEqualToString:@"Chapters"])
    {
        currentNodeContentChapters=nil;
    }
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{


*currentNodeContentBook = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        currentNodeContentChapters = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];*
    }
@end

我想解析父标签和子标签的内容。但是发现子标签内容为空。我查看了各种帖子和教程,但没有完成。谁能帮助我

4

1 回答 1

0

在方法上添加一个 LogdidStartElement并查看是否记录了所有标签。它将正确列出,您必须以正确的方式提取它

   NSLog(@"\n%@", elementname);

先看看解析器是如何工作的

有关解析的更多信息,请阅读Apple 的此文档

于 2013-09-25T07:15:14.147 回答