Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Hello,
I have a problem with downloading data and save it as a plist, i.e. in the app's sandbox. I get the data downloaded but cannot save it into the plist...

This is how I do:
I use NSURLConnection to download the data. The data is available at an url and has the format like this:
Code:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <array>
    <dict>
        <key>date</key>
        <string>a timestamp here</string>
        <key>info</key>
        <string>some info here</string>
    </dict>
  </array>
</plist>

in the
Code:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
i try to serialize the downloaded data and save it into the plist. I use the NSPropertyListSerialization which obviously does not work. The code lookes somthing like this:
Code:
NSFileManager *fileManager = [[NSFileManager alloc]init];
NSString *error;//apple documentation says this may be nil
NSPropertyListFormat format;//apple documentation says this may be nil
NSPropertyListSerialization *p  = [NSPropertyListSerialization propertyListFromData:[[self.connectionToData objectForKey:connectionKey] objectForKey:@"data"]
                     mutabilityOption:NSPropertyListImmutable
                     format:&format
                     errorDescription:&error];
	
BOOL create_plist_success = [fileManager createFileAtPath:[[self.connectionToData objectForKey:connectionKey]objectForKey:@"current_plist_path"] contents:p attributes:nil];
if(create_plist_success == NO) {//could not create file
	//fire error method here
}
else {
	//fire method in delegate. The path to the currently created plist is sent to the delegate which may use the file
}
Is this not the right way to do this?
The data is just data and not a plist file - is that a problem?

Thanks in advance!
MACloop
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
When you say it doesn't work could you expand? What, exactly, was the result?

sorry - I forgot that ;-)
Ok - the fiel exists on the path but does not contain anything.... I did test prints with both the data and the plist and the data (converted to a string) contains the correct information, and the plist i empty. From the code above I tried to print out the description for the object p, like this:
NSLog(@"p description "%@, [p description]);
and it returns p description (null)

MACloop
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
If you can turn the plist into a string to print it why don't you just write that string to a file?

Well, I can turn the data which I have downloaded... I thought I have to define somehow a file to be a plist file... Perhaps I am making it all complicated? I will try to take the data, convert it to a string and then write it to a plist. I need the plist in my app as I in the class get the data from a plist, saved/downloaded at app start.... like this:
Code:
NSString *path = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"plist_info"] objectForKey:name];
NSArray *content = [[NSArray alloc]initWithContentsOfFile:path];

Thanks for your help!
MACloop
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Well, I can turn the data which I have downloaded... I thought I have to define somehow a file to be a plist file... Perhaps I am making it all complicated? I will try to take the data, convert it to a string and then write it to a plist. I need the plist in my app as I in the class get the data from a plist, saved/downloaded at app start.... like this:
Code:
NSString *path = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"plist_info"] objectForKey:name];
NSArray *content = [[NSArray alloc]initWithContentsOfFile:path];

Thanks for your help!
MACloop

ok I have tried to use a string, but it did not work, because I need the plist later on in the app. I have to, somehow save the downloded data into a plist and as I can see it must be possible. Perhaps is the data I am retrieving false formatted? It is a simple php site, sending the textdata... no file, nothing fancy at all...

MACloop
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
ok I have tried to use a string, but it did not work, because I need the plist later on in the app. I have to, somehow save the downloded data into a plist and as I can see it must be possible. Perhaps is the data I am retrieving false formatted? It is a simple php site, sending the textdata... no file, nothing fancy at all...

MACloop

When you print the plist string you have downloaded what do you get? If you copy/paste that into TextEdit (plain text mode) and save as a .plist file can you open it with the plist editor?
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
When you print the plist string you have downloaded what do you get? If you copy/paste that into TextEdit (plain text mode) and save as a .plist file can you open it with the plist editor?

Thanks for the answer - I will try that!
MACloop
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
When you print the plist string you have downloaded what do you get? If you copy/paste that into TextEdit (plain text mode) and save as a .plist file can you open it with the plist editor?

ups - you where right - it did not work. It seem to be somthing wrong with the data i get. I have to analyze that a bit and talk to the developer who is sending me the data. Thanks for the help, again :)
MACloop
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
When you print the plist string you have downloaded what do you get? If you copy/paste that into TextEdit (plain text mode) and save as a .plist file can you open it with the plist editor?

Hello again,
so I have talked to the web developer and the problem seems to be some html-tags send with the data and the also CDATA elements. I did as you suggested and removed one after antoher from those elements and the file was not "accepted" as a plist before all those html parts were removed.

So my question now (which might be a bit off topic):
The web developer did not have a good solution for this, i.e. he said one way might be to trim the data and remove those tags, which is not a robuste solution. So, does anyone have any idea about how to do this? Is there a nice php script or somthing like that, creating valid plists? Or should I go for xml-parsing instead?

Any ideas?
Thanks in advance,
MACloop
 

Sykte

macrumors regular
Aug 26, 2010
223
0
Hello again,
so I have talked to the web developer and the problem seems to be some html-tags send with the data and the also CDATA elements. I did as you suggested and removed one after antoher from those elements and the file was not "accepted" as a plist before all those html parts were removed.

So my question now (which might be a bit off topic):
The web developer did not have a good solution for this, i.e. he said one way might be to trim the data and remove those tags, which is not a robuste solution. So, does anyone have any idea about how to do this? Is there a nice php script or somthing like that, creating valid plists? Or should I go for xml-parsing instead?

Any ideas?
Thanks in advance,
MACloop


Depends on how big the file is, what's important and what's not. I would also look into NSScanner once set it works very well, however read the documentation or you will become frustrated quickly.

Remember you can always setup a timer and compare different methods or use instruments \ activity monitor to watch memory usage if that's a concern.
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Depends on how big the file is, what's important and what's not. I would also look into NSScanner once set it works very well, however read the documentation or you will become frustrated quickly.

Remember you can always setup a timer and compare different methods or use instruments \ activity monitor to watch memory usage if that's a concern.

Thanks for the answer! I have used NSScanner in my parser classes before and know somehow how it works. So I could let the data run through the scanner and after that I could make a plist out of the data. I don't know if that is what you mean? I guess I have to define what tags are valid and remove all tags not being in the collection of valid tags.... or something like that... What I do not like with a solution with that is that I do not have any control over the data sent to the app. So if the web developer or the customer makes any structure changes, the app will not be able to read the data anymore...

Again, thanks for your help - I will try something like that to see how it works!
MACloop
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
...so I have talked to the web developer and the problem seems to be some html-tags send with the data and the also CDATA elements.
If there is data in the XML that is not part of the structure of the XML, that data should be "escaped". So, for example:
Code:
<xmlTag><div>asdf</div></xmlTag>
could become:
Code:
<xmlTag><div>asdf</div></xmlTag>
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
If there is data in the XML that is not part of the structure of the XML, that data should be "escaped". So, for example:
Code:
<xmlTag><div>asdf</div></xmlTag>
could become:
Code:
<xmlTag><div>asdf</div></xmlTag>

Thanks for the answer! Ok, that is interesting! I guess it has to be done in the web programming part and sent into the app with those escaped tags?
Thanks for the help!
MACloop
 

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
If there is data in the XML that is not part of the structure of the XML, that data should be "escaped". So, for example:
Code:
<xmlTag><div>asdf</div></xmlTag>
could become:
Code:
<xmlTag><div>asdf</div></xmlTag>

So I tried it out on some dummy plist data and it work, almost as I want it to work ;-)
The parsing is working but those tags are printed out with the text... I found a solution where I can use a NSScanner to remove the f.i. <div> tag from a text. Is this the right way to go? link
MACLoop
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
The parsing is working but those tags are printed out with the text... I found a solution where I can use a NSScanner to remove the f.i. <div> tag from a text. Is this the right way to go? link
MACLoop
What do you mean "printed out with the text"? How is the text printed out? UILabel? UIWebView? So, you have HTML-snippets that are part of the data and you want to ignore the HTML-tagging? If so, why is the HTML-tagging being sent with the XML at all? Maybe elaborate on what kind of data you are retrieving and what you are needing to do with it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.