gpt4 book ai didi

iphone - 如何使用 nsxmlparser

转载 作者:行者123 更新时间:2023-11-29 04:52:27 28 4
gpt4 key购买 nike

我正在尝试解析一些简单的东西,但我不知道该怎么做..我已经解析了之前有一些 xml,但这个结构没有任何内容,所以我有点迷失。

这是 xml。

    <item>
<title><![CDATA[Preview: Courtney Crumrin and the Night Things Special Edition]]></title>
<description><![CDATA[]]></description>
<pubDate>Tue, 20 Dec 2011 11:52:48 -0800</pubDate>
<link>http://www.comicbookresources.com/?page=preview&amp;id=10864</link>
<guid>http://www.comicbookresources.com/?page=preview&amp;id=10864</guid>
</item>

This is how I am currently parsing the xml

#pragma mark - Parsing lifecycle

- (void)startTheParsingProcess:(NSData *)parserData
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process

[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"title"])
{
self.titleString = [[NSMutableString alloc] init];
}
if ([elementName isEqualToString:@"description"])
{
self.descriptionString = [[NSMutableString alloc] init];
}
if ([elementName isEqualToString:@"link"])
{
self.linkString = [[NSMutableString alloc] init];
}
if ([elementName isEqualToString:@"guid"])
{
self.titleString = [[NSMutableString alloc] init];
}

}

-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
NSMutableString *dicString = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
currentElement = dicString;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"title"])
{
titleString = currentElement;
NSLog(@"%@", titleString);
}
if ([elementName isEqualToString:@"description"])
{
descriptionString = currentElement;
NSLog(@"%@", descriptionString);
}
if ([elementName isEqualToString:@"link"])
{
linkString = currentElement;
NSLog(@"%@", linkString);
NSLog(@" ------------------------ ");
}
if ([elementName isEqualToString:@"guid"])
{
guidString = currentElement;
NSLog(@"%@", guidString);
}
currentElement = nil;
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"Paser Error = %@", parseError);
//TODO: Create Alert message error
}

但是我想知道如何将上面的这些值(标题、描述、链接和 guid)放入 NSDictionary 中,然后将该字典放入数组中...这可能吗?这就是我的输出当前的样子

2011-12-21 16:08:08.645 NSCacheTest[15019:207] CBR Previews
2011-12-21 16:08:08.646 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.646 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.647 NSCacheTest[15019:207] Preview: Supergirl
2011-12-21 16:08:08.647 NSCacheTest[15019:207] Trapped and powerless aboard a space station, Supergirl is at the mercy of an ingenious new foe who wants to learn everything there is to know about her. But Supergirl is more than just her powers, and she&#039;ll soon show this foe – and all of Earth – what&#039;s she truly capable of! Plus, more on the Girl of Steel&#039;s mysterious origin!
2011-12-21 16:08:08.647 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.647 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.647 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.648 NSCacheTest[15019:207] Preview: Red Hood and the Outlaws
2011-12-21 16:08:08.648 NSCacheTest[15019:207] The man who has pursued Starfire all the way to the wilds of Colorado has only one thing on his mind: her slow and painful death. His name is Crux – and he&#039;s done some dark and dangerous work on his DNA to reach his goal of ridding the Earth of every alien on the planet! And Kori&#039;s about to learn that Red Hood and Roy Harper have their hands full with the ancient evil known as The Untitled!
2011-12-21 16:08:08.648 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.648 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.649 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.649 NSCacheTest[15019:207] Preview: Nightwing
2011-12-21 16:08:08.649 NSCacheTest[15019:207] As Haly&#039;s continues to travel the east coast, Dick&#039;s search for answers take him – and the circus – to Miami! But when a case from Gotham City brings Barbara Gordon to Florida, Nightwing and Batgirl must work together to bring a thief to justice. Be here as Nightwing and Batgirl tackle South Beach – and discover the true nature of the circus! The answers start this issue!
2011-12-21 16:08:08.650 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.650 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.650 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.650 NSCacheTest[15019:207] Preview: Legion of Super-Heroes
2011-12-21 16:08:08.651 NSCacheTest[15019:207] With time running out for the Legionnaire in the hands of the Dominators, it&#039;s down to Cosmic Boy to crack the riddle of how the all-powerful Daxamite Renegade made it through an impenetrable blockade… and why would Element Lad leave Chemical Kid alone in a room with that same very, very angry Renegade?
2011-12-21 16:08:08.651 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.651 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.652 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.653 NSCacheTest[15019:207] Preview: Justice League
2011-12-21 16:08:08.653 NSCacheTest[15019:207] The superstar team of Geoff Johns and Jim Lee continue the origin of the Justice League as The World&#039;s Greatest Heroes face the might of Apokolips – and find aid in an unlikely hero, as Cyborg is created!

Plus, Andy Kubert returns an amazing variant cover – his first new work after the smash-hit FLASHPOINT!
2011-12-21 16:08:08.654 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.654 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.655 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.656 NSCacheTest[15019:207] Preview: Green Lantern Corps
2011-12-21 16:08:08.656 NSCacheTest[15019:207] John Stewart has gone from being a Green Lantern soldier to prisoner of war! And in the process, he learns who the Corps&#039; mysterious enemy truly is and what they really want. The truth will rock the Corps to its foundation!
2011-12-21 16:08:08.657 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.657 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.658 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.659 NSCacheTest[15019:207] Preview: DC Universe Presents
2011-12-21 16:08:08.659 NSCacheTest[15019:207] Step right up, step right up! Watch as Deadman plays &quot;20 Questions&quot; with the enigmatic Son of the Morning on a roller coaster ride like no other! Plus: Ever wonder why good things happen to bad people? Why banks put Braille writing on drive-through ATMs? And just what exactly is the nature of your relationship with God? Then you must read DC UNIVERSE PRESENTS #4!
2011-12-21 16:08:08.660 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.661 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.661 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.662 NSCacheTest[15019:207] Preview: Catwoman
2011-12-21 16:08:08.662 NSCacheTest[15019:207] Catwoman says goodbye to an old friend, says hello to an older one, and resolves more firmly than ever to strike out on her own. Only one of those things almost gets her killed.
2011-12-21 16:08:08.663 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.663 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.663 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.664 NSCacheTest[15019:207] Preview: Captain Atom
2011-12-21 16:08:08.666 NSCacheTest[15019:207] Before he received his godlike powers, Captain Atom was a military man – an Air Force pilot. Now, he&#039;s being recruited again to serve his country as only he can. But will Captain Atom go along with his new role as a weapon of mass destruction?
2011-12-21 16:08:08.666 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.666 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.667 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.667 NSCacheTest[15019:207] Preview: Birds of Prey
2011-12-21 16:08:08.667 NSCacheTest[15019:207] The only thing worse than a bomb on a speeding train is three bombs. Hidden inside the skulls of three different people. All on the same speeding train. And the Birds of Prey – Black Canary, Starling, Katana and Poison Ivy – know the names of only two of these walking bombs. Don&#039;t miss the shocking revelation that will rewrite the rules of the team forever! Yes, we know it&#039;s only issue #4! Guest starring Batgirl!
2011-12-21 16:08:08.670 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.670 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.670 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.670 NSCacheTest[15019:207] Preview: Batman
2011-12-21 16:08:08.671 NSCacheTest[15019:207] Shocking discoveries lead Batman deeper into the mystery of the Court of Owls and its secret and bloody ties to both Gotham City and the Wayne family. But an even deadlier threat awaits Batman: a trap set hundreds of years ago, far beneath his city. The war for Gotham&#039;s soul begins here!
2011-12-21 16:08:08.671 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.671 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.674 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.674 NSCacheTest[15019:207] Preview: Blue Beetle
2011-12-21 16:08:08.674 NSCacheTest[15019:207] What is Jaime Reyes going to do? La Dama and the unstoppable, mechanical monster known as Silverback both want the scarab for their own nefarious purposes, and Jaime&#039;s family and friends want to know why he disappeared. Can he keep his secret safe, or will he risk everything to tell his loved ones the truth?
2011-12-21 16:08:08.674 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.675 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.675 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.675 NSCacheTest[15019:207] Preview: Daken: Dark Wolverine
2011-12-21 16:08:08.675 NSCacheTest[15019:207] • The Kingpin of L.A. revealed!
• A standalone issue featuring Daken and FBI agent Donna Kiel
• Can Daken be redeemed?
2011-12-21 16:08:08.676 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.676 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.676 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.677 NSCacheTest[15019:207] Preview: Secret Avengers
2011-12-21 16:08:08.677 NSCacheTest[15019:207] Critically acclaimed writer Rick Remender kicks his explosive run on Secret Avengers in style in Secret Avengers #21.1! Joined by superstar artist Patrick Zircher, Remender sends Captain America’s covert strike force into the belly of the beast as he and Hawkeye uncover a huge threat to the Marvel Universe. The two must break into Red Light Nation, a country run by criminals FOR criminals, to stop an evil plot from breaching the border…headed up by an all-new Masters of Evil?! No fan can miss this perfect jumping on point as Remender and Zircher take the Secret Avengers on action packed thrill ride uncovering the dark underbelly of the Marvel Universe. It all begins next month in Secret Avengers #21.1!
2011-12-21 16:08:08.677 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.677 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.678 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.678 NSCacheTest[15019:207] Preview: Courtney Crumrin and the Night Things Special Edition
2011-12-21 16:08:08.678 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.678 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.679 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.679 NSCacheTest[15019:207] Preview: Batman: Odyssey
2011-12-21 16:08:08.680 NSCacheTest[15019:207] Batman has sidestepped death time and time again, but that all ends in this issue with the gut-wrenching reality of the Underworld. It&#039;s a surreal – yet all too real – voyage into a personal hell of his own. Batman will never be the same.
2011-12-21 16:08:08.680 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.680 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.681 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.681 NSCacheTest[15019:207] Preview: Sonic Universe
2011-12-21 16:08:08.681 NSCacheTest[15019:207] “Babylon” Part Three: The Babylon Rogues and the Battle Bird Armada – united?! And why is this fowl allegiance moving in to attack New Mobotropolis when they’re really searching for the fabled Babylon Garden? The skies erupt in battle as Sonic and Jet finally have their rematch! The surprises keep coming right through to the shocking final assault!
2011-12-21 16:08:08.681 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.682 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.682 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.682 NSCacheTest[15019:207] Preview: Conan the Barbarian
2011-12-21 16:08:08.682 NSCacheTest[15019:207] In this sweeping adaptation of Robert E. Howard’s fan-favorite “Queen of the Black Coast,” Conan turns his back on the civilized world and takes to the high seas alongside the pirate queen Bêlit, setting the stage for an epic of romance, terror, and swashbuckling. This is Conan as you’ve never seen him, with the combination of one of Robert E. Howard’s greatest tales and the most dynamic creative team in comics!
<li>A perfect jumping-on point for new readers!<br><li>A bold, fresh take on the Cimmerian.<br><li>“Queen of the Black Coast” is the most-requested Conan adaptation!</li>
2011-12-21 16:08:08.683 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.683 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.683 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.683 NSCacheTest[15019:207] Preview: Xenoholics
2011-12-21 16:08:08.683 NSCacheTest[15019:207] &quot;ALIEN GONE WILD!&quot;
It&#039;s an all out war between the Men in White and the Grey Aliens for possession of the Xenoholics! As our heroes continue on the run, they start to uncover the truth about their abductions. Secrets are revealed as this hot new series continues!
2011-12-21 16:08:08.684 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.684 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.684 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.684 NSCacheTest[15019:207] Preview: Strange Girl Omnibus HC
2011-12-21 16:08:08.685 NSCacheTest[15019:207] FOR THE FIRST TIME, IN ONE VOLUME: RICK REMENDER&#039;S CRITICALLY ACCLAIMED INDIE HIT, FROM START TO FINISH! Ten years after the Rapture, a beautiful occultist and her pet demon embark on a road trip to the last open gateway to heaven, in hopes of befriending God and escaping hell on earth. A bare-knuckle, action-driven series, peppered with social commentary and dark humor, STRANGE GIRL is a story about the bewildering nature of religion, what would happen if the dark future predicted by many came true, and what a good person would endure for not having faith in advance of it. Featuring story and art by a handful of today&#039;s hottest new talents including RICK REMENDER (Uncanny X-Force, Venom), ERIC NGUYEN (Halo, Sandman), JEROME OPENA (Uncanny X-Force, Punisher) and NICK STAKAL (Criminal Macabre). Collects STRANGE GIRL #1-18
2011-12-21 16:08:08.685 NSCacheTest[15019:207] (null)
2011-12-21 16:08:08.685 NSCacheTest[15019:207] ------------------------
2011-12-21 16:08:08.685 NSCacheTest[15019:207] (null)

最佳答案

I dont know how to get the values (title, description, pubDate etc into the dictionary??)

您应该做的第一件事是阅读 Event-Driven XML Programming Guide ,特别是 Handling XML Elements and Attributes 部分(但请阅读整个文档)。该部分解释了如何处理解析不同元素并存储结果数据。

在您的特定情况下,您有一个 -parser:didEndElement:... 方法,其中包含以下内容:

if ([elementName isEqualToString:@"title"]) 
{
[valueDictionary ad....//I
}

这是正确的想法。您实际上只需要阅读 NSMutableDictionary 即可完成条件正文:

if ([elementName isEqualToString:@"title"]) 
{
[self.valueDictionary setObject:self.titleString forKey:@"title"];
}

管理同一件事的另一种方法是等到整个 item 元素结束,然后立即将所有相关值添加到字典中,然后将字典添加到 item 数组中:

if ([elementName isEqualToString:@"item"]) 
{
[self.valueDictionary setObject:self.titleString forKey:@"title"];
[self.valueDictionary setObject:self.descriptionString forKey:@"desc"];
// ...and so on...
[self.itemArray addObject:self.valueDictionary];
self.valueDictionary = nil;
self.titleString = nil;
self.descriptionString = nil;
// ...and so on...
}

在处理当前项目结束时将所有属性设置为 nil 是可选的 - 无论如何,您都会在下一个项目开始时分配一些新值 - 但这似乎是很好的内务管理。

另外,请注意,在我的示例中,我使用的是属性(即 self.valueDictionary 而不是 valueDictionary)。如果您不厌其烦地创建属性,则应该使用它们——它将避免将来出现内存管理问题。

关于iphone - 如何使用 nsxmlparser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8584150/

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