05-26-2011, 03:34 PM
#1 (permalink )
Registered Member
Join Date: Jan 2011
Posts: 118
XML parsing done, load from URL.
Hello guys!
Okay, so I finally managed to get my little XML parsing script to work! Fantastic! But I ain't satisfied.. not yet!
I've Googled around a bit, trying to find a thread that could teach me how to load my XML file from a specified URL adress - but without luck.
So now I'm a bit lost.
Do any of you have any luck loading XML data from a URL adress?
Thanks for your help!
Rail.
__________________
05-26-2011, 04:01 PM
#2 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
just download the xml with NSURLConnection or ASIHTTPRequest, and then use stringResponde as xml.......
see section 4, there are some examples:
http://www.iphonedevsdk.com/forum/ip...rvice-how.html
__________________
05-26-2011, 04:44 PM
#3 (permalink )
Registered Member
Join Date: Jan 2011
Posts: 118
Quote:
Originally Posted by
dany_dev
My problem is more; how do I combine the XML parser and the NSURLCon.?
I think I'm getting this wrong
__________________
05-26-2011, 04:55 PM
#4 (permalink )
Senior Member
iPhone Dev SDK Supporter
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
1. Download XML file using NSURL Connection or ASIHTTP
2. On Download Complete, create xml parser and parse as normal
3. Everything else is the same
05-26-2011, 06:58 PM
#5 (permalink )
Registered Member
Join Date: Jan 2011
Posts: 118
Quote:
Originally Posted by
smithdale87
1. Download XML file using NSURL Connection or ASIHTTP
2. On Download Complete, create xml parser and parse as normal
3. Everything else is the same
At this time, to locate my XML file (local in my project) I'm using:
Code:
NSString * path = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"xml"];
Just to be sure I get this right. The only thing I've to do is to point my path to a URL using the URLConnection?
__________________
05-27-2011, 02:30 AM
#6 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
Quote:
Originally Posted by
railion
At this time, to locate my XML file (local in my project) I'm using:
Code:
NSString * path = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"xml"];
Just to be sure I get this right. The only thing I've to do is to point my path to a URL using the URLConnection?
can you open the link posted above and read code in section 4 or is too effort for you?
You just need to download the file and use the stringResponse (that will be the xml).
you could also do that
Code:
NSString *responseString = [[NSString alloc] initWithContentsOfURL:yourURL encoding:anEncoding error:nil];
//use responseString
[responseString release];
but it is a synchronous request, so used on main thread freeze the UI during download....
__________________
Last edited by dany_dev; 05-27-2011 at 02:33 AM .
05-27-2011, 09:51 AM
#7 (permalink )
Registered Member
Join Date: Jan 2011
Posts: 118
Quote:
Originally Posted by
dany_dev
can you open the link posted above and read code in section 4 or is too effort for you?
You just need to download the file and use the stringResponse (that will be the xml).
you could also do that
Code:
NSString *responseString = [[NSString alloc] initWithContentsOfURL:yourURL encoding:anEncoding error:nil];
//use responseString
[responseString release];
but it is a synchronous request, so used on main thread freeze the UI during download....
dany_dev: I've taken a look at your post and it looks great - I'm also starting to figure it out, I'm just a little bit slow in learning, ehh
Sorry!
Anyway, I'm just having a little problem putting the bricks in the wall together, but I guess I'll figure it out sooner or later.
I really appreciate the help you're giving me - a huge thanks for that
__________________
05-27-2011, 12:53 PM
#8 (permalink )
Registered Member
Join Date: Jan 2011
Posts: 118
Okay, a little update.
I think I'm getting closer
Using a little different way though.
Code:
NSString *filePath = @"URL";
NSURL *url=[[NSURL alloc] initWithString:filePath];
NSString *fileContent = [[NSString alloc] dataWithContentsOfFile:url]; NSXMLParser* parser = [[NSXMLParser alloc] initWithData:fileContent];
[parser setDelegate:self];
[parser parse];
[parser release];
[super viewDidLoad];
Still not working 100%.
__________________
05-27-2011, 01:04 PM
#9 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
there are many weird things on your code
1-this line don't make sense for 2 reason:
NSString don't have a method called "dataWithContentsOfFile". Second reason, also supposing that this method exist, you are using alloc and then a method that return an autoreleased object, don't make sense, should be [[NSString alloc] initWithData:aDataObject];
Quote:
NSString *fileContent = [[NSString alloc] dataWithContentsOfFile:url];
2-Why you are using initWithData passing a string? if you want to use initWithData: you need to pass a NSData*, fileContent is a NSString*
Quote:
NSXMLParser* parser = [[NSXMLParser alloc] initWithData:fileContent];
3-I advice you to use a delegate for NSXMLParser that is not self, to avoid too confusion on your view controller (I assume that self is your view controller)
The right code should be:
Code:
NSString *urlString = @"insert your url";
NSURL *url=[[NSURL alloc] initWithString:urlString];
NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
[parser release];
__________________
Last edited by dany_dev; 05-27-2011 at 01:21 PM .
05-27-2011, 01:06 PM
#10 (permalink )
Registered Member
Join Date: Jan 2011
Posts: 118
Quote:
Originally Posted by
dany_dev
there are many weird things on your code
Code:
NSString *urlString = @"insert your url";
NSURL *url=[[NSURL alloc] initWithString:urlString];
NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
[parser release];
FYI, the I removed the URL because I didn't wanted to share it with the whole internet :-)
Anyway, thanks for a quick answer, I'll test it right away.
__________________
05-27-2011, 01:19 PM
#11 (permalink )
Registered Member
Join Date: Jan 2011
Posts: 118
Argh, SIGABRT and this line marked with green:
Code:
int retVal = UIApplicationMain(argc, argv, nil, nil);
I think I've taking on a too big project. I should read a bit more, before trying to make this.
***UPDATE***
I made a clean start; trying all over and apparently it works now!
Again, dany_dev, thank you so much for your patience and your help
I've bookmarked your link, and I can tell I'm going to do some reading now
Have a nice day, there.
Cheers from Denmark!
__________________
Last edited by railion; 05-27-2011 at 01:25 PM .
05-27-2011, 01:19 PM
#12 (permalink )
Nuisance Developer
Join Date: Jul 2009
Location: Italy
Posts: 4,691
Quote:
Originally Posted by
railion
FYI, the I removed the URL because I didn't wanted to share it with the whole internet :-)
Anyway, thanks for a quick answer, I'll test it right away.
yes i know, but as you see in my post, is not the url the problem
__________________
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44