views:

849

answers:

3

Hello

I have to parse an XML file using NSXMLParser. There are so many HTML tags in them, so when I am trying to parse it, it will store the string up to that and then again go to found character method and starts to append it. my code is:

if (Bio_CResults) {
 [BioResults appendString: string];
 [Info appendString:string];

 [stringarr addobject:Info];
 NSLog(@"bio==%@",BioResults);
 NSLog(@"string==%@",string);
}

and I want to add it in string array, but here it will make create extra object of array. i.e.

stringarr objectAtIndex 0 = abc
stringarr objectAtIndex 1 = def
stringarr objectAtIndex 2 = ghi

but actually I want all of them together in one object because they are actually one string only.. plz help me for that

+3  A: 

If you don't even need multiple string objects you can use an NSMutableString instead of an array. Just use the appendString: method to add to the end of the string:

NSMutableString *string = [NSMutableString string];
[string appendString:@"abc"];
[string appendString:@"def"];
NSLog(@"New string is %@", string);

This will log "New string is abcdef".


If you really want an array, use an NSMutableArray instead of an NSArray. That way you can change an object in-place (replace a string with a new string created by appending another string). So for example:

// First create an array with @"abc"
NSMutableArray *mArray = [NSMutableArray arrayWithObject:@"abc"];

// Next get the first object as a string and append @"def" to it in-place
NSString *string = (NSString *)[mArray objectAtIndex:0];
[mArray replaceObjectAtIndex:0 withObject:[string stringByAppendingString:@"def"]];

// Now get the new first object
NSLog(@"First object is %@", (NSString *)[mArray objectAtIndex:0]);

This will log the message "First object is abcdef".

Tim
I think he's trying to use a mutable array already, and I'm guessing that "addobject:" is just a typo in his question. And he also uses "appendString:", so I'm not sure what he was trying to accomplish with the mutable array at all. :-)
Quinn Taylor
Maybe the issue is that he's adding the object rather than doing a call to replaceObjectAtIndex:withObject:? I think those two mutable strings that he's calling appendString: on are the strings he's using to store the data from the XML parser (from the parser:foundCharacters: method), and he's just adding them to the array rather than replacing their old counterparts. Either way, the answer stands :)
Tim
+1  A: 

I really dont like NSXmlParser. You might want to read my blog post on using RegxKitLite http://blog.bluespark.co.nz/?p=51

It might be of some help. Hopefully it wont lead you in the wrong direction.

Cheers, John.

John Ballinger
+1  A: 

There are so many HTML tags in them…

Be aware that HTML is usually not valid XML. If you're parsing HTML, an XML parser will throw an error some part of the way through the document.

You may be better off creating a WebView, loading the HTML content into it, and then getting a DOMDocument from the view's main frame and traversing the DOM hierarchy. You don't have to put the view into a window; you can just use it to get a DOMDocument.

If you are parsing XML and meant to say “XML tags”, you can disregard this answer.

Peter Hosey