I'm using TouchXML to parse my RSS.
The new XML
<channel>
<item>
<title> </title>
<social>
<views>
<total_views>2</total_views>
</views>
<reviews>
<average_rating>2</average_rating>
</reviews>
</social>
</item>
</channel>
I currently parsing the XML and passing it with initWithData to my detail view for the title of the article like this:
[rssData objectForKey: #"title" ];
But how do I show the value of stars its a subnode of rate and rate is a subnode from social?
I have tried this: [rssData objectForKey: #"social/rate/stars" ];
But it won't work. Please tell me how.
The entire premise that I based my original answers seem to be wrong. If you've used the following snippet,
for (CXMLElement *node in nodes) {
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
int counter;
for(counter = 0; counter < [node childCount]; counter++) {
// common procedure: dictionary with keys/values from XML node
[item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];
}
[results addObject:item];
[item release];
}
to generate your code than children of social element are stored as a string value and any reasonable means to extract them are lost. I would suggest that you store the nodes rather than go about creating the dictionary. Here's the example of extracting the rating,
NSString * xmlString = #"<channel> <item> <title> </title> <social> <views> <total_views>2</total_views> </views> <reviews> <average_rating>2</average_rating> </reviews> </social> </item> </channel>";
CXMLDocument * xmlDocument = [[[CXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil] autorelease];
NSArray * nodes = [xmlDocument nodesForXPath:#"//item" error:nil];
for (CXMLElement * node in nodes) {
NSString * title = [[node nodeForXPath:#"title" error:nil] stringValue];
NSString * average_rating = [[node nodeForXPath:#"social/reviews/average_rating" error:nil] stringValue] ;
NSLog(#"%#", title);
NSLog(#"%#", average_rating);
}
try this , you need to pass readNode value like "channle", and it will return array, including child subnode
-(NSMutableArray *)parseNodeFromXML:(NSString *)strXML readForNode:(NSString *)strReadValue
{
NSError *theError = NULL;
CXMLDocument *theXMLDocument = [[CXMLDocument alloc] initWithXMLString:strXML options:0 error:&theError] ;
NSMutableArray *arrReturn = [[NSMutableArray alloc] init];
NSArray *nodes = [[theXMLDocument rootElement] nodesForXPath:strReadValue error:nil];
for (CXMLNode *itemNode in nodes)
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
// PROCESS FOR READ ELEMENT ATTRIBUTES IN CURRENT NODE -------------------------
if([itemNode isMemberOfClass:[CXMLElement class]]){
CXMLElement *elements=(CXMLElement*)itemNode;
NSArray *arAttr=[elements attributes];
for (int i=0; i<[arAttr count]; i++) {
if([[arAttr objectAtIndex:i] name] && [[arAttr objectAtIndex:i] stringValue]){
[dic setValue:[[arAttr objectAtIndex:i] stringValue] forKey:[[arAttr objectAtIndex:i] name]];
}
}
}
// PROCESS FOR READ CHILD NODE IN CURRENT NODE -------------------------
for (CXMLNode *childNode in [itemNode children])
{
// NSLog(#"count -- %d --- %#",[childNode childCount],[childNode children]);
// IF ANY CHILD NODE HAVE MULTIPLE CHILDRENS THEN DO THIS....-
if ([childNode childCount] > 1)
{
NSMutableDictionary *dicChild = [[NSMutableDictionary alloc] init];
for (CXMLNode *ChildItemNode in [childNode children])
{
[dicChild setValue:[ChildItemNode stringValue] forKey:[ChildItemNode name]];
}
[dic setValue:dicChild forKey:[childNode name]];
}else
{
[dic setValue:[childNode stringValue] forKey:[childNode name]];
}
}
[arrReturn addObject:dic];
}
// NSLog(#"%#",arrReturn);
return arrReturn;
}
Related
How can I get the contents of a directory and all of its subfolders? I would like to have tree stored in a NSDictionary.
I want the dictionary to print something like this:
{
MyFolder = (
"Water.png",
{
MySubfolder = (
"Note.txt",
{
Sub-Subfolder = (
"3D.pdf",
"MyFile.txt"
);
}
);
}
);
}
Ive tried:
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *array = [manager contentsOfDirectoryAtPath:path error:nil];
NSMutableDictionary *files = [[NSMutableDictionary alloc]init];
NSString *newPath = #"";
for (int i=0; array.count>i; i++) {
newPath = [NSString stringWithFormat:#"%#/%#", path, [array objectAtIndex:i]];
//echo(newPath);
if ([[[manager attributesOfItemAtPath:newPath error:nil] objectForKey:NSFileType] isEqualToString:NSFileTypeRegular])
{
NSLog(#"Setting: %#||%#", [array objectAtIndex:i], [newPath lastPathComponent]);
[files setObject:[array objectAtIndex:i] forKey:[newPath lastPathComponent]];
}
else
{echo([NSString stringWithFormat:#"newPath=%#", newPath]);
dict = [self reachedDirectory:newPath dict:dict oldPath:path];
}
}
NSMutableDictionary *transferred = [dict objectForKey:[newPath lastPathComponent]];
if (!transferred) {
transferred = [[NSMutableDictionary alloc]init];
}
for (int n=0; files.count>n; n++) {
[transferred setObject:[[files allValues] objectAtIndex:n] forKey:[[files allKeys] objectAtIndex:n]];
}
echo([newPath lastPathComponent]);
[dict setObject:transferred forKey:[path lastPathComponent]];
return dict;
But All of the folders are not aligned and it doesnt go past the second dimension of subfolders. I would like it to be able to have as many subfolders that is possible.
Thanks for your help!
You can use NSDirectoryEnumerator class and it provides enumerateAtPath method so that you need to just pass your main folder path and inside that just loop your condtion. So that whatever your subfolder exist it will print the path accordingly.
If i enumerate an array i get
<myArray: 0x71b26b0>
<myArray: 0x71b2830>
<myArray: 0x71b2900>
I could take it that myData is behind the pointers listed, but if I wanted to explicitly see (log) the contents at each address, how to do that?
I have tried the &myData to no avail
--
for the benefit of uchuugaka:
-(void)loadObservedItems{
NSString *path = [self observationFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
myArray = [unarchiver decodeObjectForKey:#"ObserveKey"];
[unarchiver finishDecoding];
} else {
myArray = [[NSMutableArray alloc] initWithCapacity:10];
}
NSLog(#" %#",myArray);
}
Add to MyClass.m:
-(NSString*)description {
NSMutableDictionary* descDict = [NSMutableDictionary dictionary];
[descDict addObject:someField forKey:#"someField"]
[descDict addObject:anotherField forKey:#"anotherField"];
[descDict addObject:yetAnotherField forKey:#"yetAnotherField"];
return [descDict description];
}
Then just use NSLog(#"myObject is %#", myObject);. Just like the big guys.
Slightly more sophisticated is to (within the method) pre-pend your class name and the object address to the result string, but that's usually unnecessary for simple debugging.
But I think you can do that like this:
return [NSString stringWithFormat:#"%# : %#", [super description], [descDict description]];
Firstly, I am new with Objective C.
I have my class Song that has a pair of attributes.
In my main class i got a variable allSongs that is a NSMutableArray and in this array have I added all my song-objects.
My problem comes when trying to call [self.allSongs removeObject:OBJECT];
Using the debugger, I can see that before the call, the list looks as expected. But after the call it will result that the targeted object will be removed but also the first element in the array will turn to nil.
Is this a common pointer problem or what?
Here is my code:
in h file
#property (nonatomic, strong) NSMutableArray *songs;
#property (nonatomic, strong) NSMutableArray *allChapters;
in m file
- (void)viewDidLoad
{
self.chosenChapter = [[NSString alloc]initWithFormat:self.chosenChapter];
self.allChapters = [[NSMutableArray alloc]init];
//Chapter names and chapter page range
chapters = [[NSArray alloc]initWithObjects:#"chapter1", #"chapter2", #"chapter3", nil];
chaptersRange = [[NSArray alloc]initWithObjects:#"25", #"51", #"88", nil];
//Filnames of every song
files = [[NSMutableArray alloc] initWithObjects:#"test", #"Feta_fransyskor", nil];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: #"page" ascending: YES];
self.songs = [[NSMutableArray alloc]init];
for(int i = 0; i < chapters.count; i++){
Song *chapter = [[Song alloc]init];
[chapter setPage:(NSString *)self.chaptersRange[i]];
[chapter setTitle:(NSString *)self.chapters[i]];
[self.allChapters addObject:chapter];
[self.songs addObject:chapter];
}
NSString *filePath;
int i;
for (i = 0; i < files.count; i++) {
filePath = [[NSBundle mainBundle] pathForResource:files[i] ofType:#"txt"];
if(filePath){
NSError *error;
NSString *textFromfile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: &error];
/*
index
0 for page number
1 for title name
2 for melody name
3 -> for lyrics
*/
NSMutableArray *newLineseparatedText = (NSMutableArray *)[textFromfile componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
if(newLineseparatedText){
Song *newSong = [[Song alloc]init];
[newSong setPage:newLineseparatedText[0]];
[newSong setTitle:newLineseparatedText[1]];
[newSong setMelody:newLineseparatedText[2]];
[newLineseparatedText removeObjectAtIndex:0]; //remove page number
[newLineseparatedText removeObjectAtIndex:0]; //remove title name
[newLineseparatedText removeObjectAtIndex:0]; //remove melody name
[newSong setLyric:[newLineseparatedText componentsJoinedByString:#"\n"]];
[songs addObject:newSong];
}
}
}
[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[super viewDidLoad];
}
-(void)addChapters{
for(int i = 0; i < self.allChapters.count; i++){
if([self.songs containsObject:self.allChapters[i]] == false)
[self.songs addObject:self.allChapters[i]];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: #"page" ascending: YES];
[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
}
-(void)addChapters{
for(int i = 0; i < self.allChapters.count; i++){
if([self.songs containsObject:self.allChapters[i]] == false)
[self.songs addObject:self.allChapters[i]];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: #"page" ascending: YES];
[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
}
-(void)removeChaptersExcept:(Song *) chapter{
for(int i = 0; i < self.allChapters.count; i++){
if(self.allChapters[i] != chapter && [self.songs containsObject:self.allChapters[i]])
[self.songs removeObject:self.allChapters[i]];
}
}
last line of this code is were i get an error, as the mutableArray has a couple of nil elements.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.chosenChapter isEqualToString:#"Alla"]){
[self addChapters];
}
else{
Song *chapter = nil;
for(int i = 0; i < self.allChapters.count; i++){
if([((Song *)self.allChapters[i]).title isEqualToString:self.chosenChapter]){
chapter = self.allChapters[i];
break;
}
}
[self addChapters];
[self removeChaptersExcept:chapter];
}
NSString *cellIdentifier = nil;
UITableViewCell *cell = nil;
NSString *page = ((Song *)self.songs[indexPath.row]).page;
and here are some screen bumps
This is before removing first object
This is after the first object was removed. You see how one element disapeared as expected and the other is set too nil?
Sometimes the Variables View shows incorrect values.
An array can't contain nil values, so this is definitely a case where the values are wrong.
You state that your application crashes on this line:
NSString *page = ((Song *)self.songs[indexPath.row]).page;
My guess is that self.songs[indexPath.row] simply doesn't have a property called page.
Try to replace this line with this code:
Song *s = self.songs[indexPath.row];
if (s == nil)
{
NSLog("Song is nil! How can that be?");
}
else
{
NSLog("Page is %#", s.page);
}
It will help you pin-point the problem. Good luck.
I tried to parse following String using TBXML.
<panel><start><post_id>4</post_id><user_id>2</user_id><post>Hyder here</post><created_on>2012-01-09 06:36:59</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>3</post_id><user_id>2</user_id><post>Hello hyder here</post><created_on>2012-01-09 06:34:09</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>2</post_id><user_id>0</user_id><post>Hi, This is Syed Hyder....</post><created_on>2012-01-09 01:07:36</created_on><likes>0</likes><noc>0</noc><status>A</status></start><start><post_id>1</post_id><user_id>0</user_id><post>Hello, gaurav....How are you.</post><created_on>2012-01-09 01:05:11</created_on><likes>0</likes><noc>0</noc><status>A</status></start></panel>
My code: -
NSMutableArray *newArr = [[NSMutableArray alloc] init];//21
RXMLElement *rxml = [[RXMLElement alloc] initFromXMLString:response];
[rxml iterate:#"panel.start" with:^(RXMLElement *start) {
ModelPost *newPost = [[ModelPost alloc] init];
newPost.message = [NSString stringWithFormat:#"%#", [start child:#"post"]];
NSLog(#"Post = %#", [start child:#"post"]);
[newArr addObject:newPost];
}];
but I am unable to do it or enter in the iterate loop.
Please help me to over come this by sample code, suggestions and tutorials.
Thanks in Advance....
- (void)xmlparserurl{
//xml data url calling....
tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:#"http://karen.webmascot.com/iapi/orders.php?max_count=10"]] retain];
records = [NSMutableArray array];
[records retain];
//retrieving all data from the xml
if (tbxml.rootXMLElement){
NSLog(#"inserting");
[self traverseElement:tbxml.rootXMLElement];
}
[tbxml release];
}
- (void)traverseElement:(TBXMLElement *)element {
do {
//NSLog(#"%#",[TBXML elementName:element]);
if (element->firstChild)
[self traverseElement:element->firstChild];
if ([[TBXML elementName:element] isEqualToString:#"start"]) {
//NSLog(#"xml element checking");
TBXMLElement *id = [TBXML childElementNamed:#"attribute name" parentElement:element];
//inserting into the mutable array
[records addObject:[NSArray arrayWithObjects:
[TBXML textForElement:elementname],
[TBXML textForElement:elementname],
[TBXML textForElement:elementname],nil]];
}
} while ((element = element->nextSibling));
[self.tableview reloadData];
}
i am only sending the code of retrieving the xml data. i am also sending the tutorial where you will get the answer of your further question.
http://www.tbxml.co.uk/TBXML/API.html
Basically, i want to save NSData as a file using NSSaver. what do i add into my if statement to save it/what corrections should i make to this method?
- (IBAction)saveAs:(id)sender{
NSMutableArray *storage = [NSMutableArray arrayWithCapacity:[[arrayController arrangedObjects] count]];
for (int i = 0; i < [[arrayController arrangedObjects] count]; i++) {
Stores *object = [[arrayController arrangedObjects] objectAtIndex:i];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:object.Website, #"website",object.Street_Address, #"streetAddress", object.City_Address, #"cityAddress", object.Name, #"storeName", object.Latitude, #"latidude", object.Longitude, #"longitude", object.Phone_Number, #"phoneNumber", nil];
[storage addObject:dict];
}
NSData *data = [NSKeyedArchiver archiveDataWithRootObject:storage];
NSSavePanel *saver = [NSSavePanel savePanel];
saver.requiredFileType = #"yogurtstorelocations";
[saver runModal];
if ([saver runModal] == NSOKButton){
}
}
Error handling code omitted.
[data writeToURL:[saver URL] atomically:YES];