gpt4 book ai didi

iphone - 在 objective-c 中从多级xml获取子标记

转载 作者:行者123 更新时间:2023-12-01 16:47:16 25 4
gpt4 key购买 nike

我的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>

objective-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

我想同时解析父标签和子标签的内容。但是,发现Child标记的内容为空。我查看了各种帖子和教程,但未完成。请问有人可以帮助我吗

最佳答案

didStartElement方法上添加一个Log,查看是否所有标签都被记录了,它将被正确列出,并且您必须在正确的位置将其提取出来

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

看看解析器首先添加这个标签是如何工作的,它不是解析器解析数据的父子方式,而只是先到先服务的方式,值接近并且您必须编写逻辑来支持

有关解析的更多信息,请阅读 document by apple

关于iphone - 在 objective-c 中从多级xml获取子标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18998265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com