Parsing partial XML created with SudzC in objective-c - objective-c

Hello to all in this lovely community,
I am trying to get the contents of the data that is returned with a SudzC method from my web service. The webservice sends the data in a kind of partial XML like this:
<gUser>
<user USERID="224" USERNAME="name" USERSURNAME="surname" PASSWORD="123" TELEPHONE="+123" EMAIL="" USERTYPE="2"></user>
</gUser>
This is returned as (id) in the SudzC method. It can be assigned to a dictionary, string and it can be printed with NSLog etc. However, I was not able to get what I need; the attributes of USERID, USERNAME and so on.
EDIT
The result is of type NSCFString. It contains the whole XML data I wrote above in one string object. I tried to call a parser method that takes an NSString* obj(in this case, the result) as parameter, it gives the unrecognized selector exception. How am I going to parse/use this data if I can't divide the whole into pieces.
How should I proceed? Any help is much appreciated.
Regards,
Besel

According to the documentation for sudzc each element returned has a method called -attributes that contains all of that element's attributes as an array of nodes. You'll then have to iterate through that array to find the attributes you want.

Related

Objective C - JSON to CoreData with category relation [duplicate]

I'm a nwebie in Core Data, i have designed a navigation based application and some of the data i use are created on run time(come from a URL via JSON). I took a few tutorials an searched for almost a day but haven't still realized how to save the incoming JSON data to the Entity (or event?) in my Core Data model. I fetch the data in the DetailViewController class and i need to save this data to Core Data(I have prepared an Entity with 7 properties). Can anyone please help?(If you know a good tutorial or sample code i will be pleased)
EDIT This may be a little specific but i really have trouble with and need just a little help.
My data comes to the app from a kind of restful server(i wrote it in PHP), firstly user enters his/her login informations(which i have saved to the database on server before) and when the response data comes i will use different elements of it in differen views(for example the user_id will be used on a view and the buttonData etc on other views). My question is, how will i save JSON data into my core data model(has tree Entities for the moment). Thanks in advance
Note: I lokked arround a lot but couldn't find any answer&tutorial about an app like mine
The best way to do that would be to create entities corresponding to JSON structure. Easiest was is when each JSON object becomes an entity, and arrays become arrays of entities. Be reasonable, however, and don't introduce too much overkill for JSON subobjects that are essentially part of its superobject.
When you have created entities, you can start off with the parsing and translation. Use some JSON framework (starting from iOS5 there's one from Apple) and parse JSON string into object tree, where root item is either an NSArray or NSDictionary, and subelements will be NSArray, NSDictionary, NSNumber, NSString or NSNull.
Go over them one by one in iterational loops and assign according values to your core data entity attributes. You can make use of NSKeyValueCoding here and avoid too much manual mapping of the attribute names. If your JSON attributes are of the same name as entity attributes, you'll be able to just go over all dictionary elements and parse them into attributes of the same name.
Example
My parsing code in the similar situation was as follows:
NSDictionary *parsedFeed = /* your way to get a dictionary */;
for (NSString *key in parsedFeed) {
id value = [parsedFeed objectForKey:key];
// Don't assign NSNull, it will break assignments to NSString, etc.
if (value && [value isKindOfClass:[NSNull class]])
value = nil;
#try {
[yourCreatedEntity setValue:value forKey:property];
} #catch (NSException *exception) {
// Exception means such attribute is not defined in the class or some other error.
}
}
This code will work in trivial situation, however, it may need to be expanded, depending on your needs:
With some kinds of custom mappings in case you want your JSON value be placed in differently named attribute.
If your JSON has sub-objects or arrays of sub-objects, you will need to detect those cases, for example in setters, and initiate new parsing one level deeper. Otherwise with my example you will face the situation that assigns NSDictionary object to an NSManagedObject.
I don't think it is reasonable to dive into these, more advanced matters in scope of this answer, as it will expand it too much.
I suggest you to use this library : https://github.com/TouchCode/TouchJSON
And then if you want to make a factory to parse json and feed your code data, you can use selectors to call methods to fill all your attributes.
Chances are your JSON data gets converted to an NSDictionary or NSArray (or some combination of the two). Simply extract the key/values from the JSON structure and add them to your entity class.
This lib helps me lot
Features
Attribute and relationship mapping to JSON key paths.
Value transformation using named NSValueTransformer objects.
Object graph preservation.
Support for entity inheritance
Works vice-versa

How can I read data from the general pasteboard for all classes?

At the moment I'm using readObjectsForClasses: to retrieve a list of objects from the general pasteboard in cocoa, which works whenever I want to only retrieve objects of a certain class e.g. NSString or NSImage etc.
However, I would like to be able to read every single object in it without having to make an array of all of their classes, such that the array returned would contain every single item in the pasteboard.
Is this possible?
Have you tried using the types method of the NSPasteboard object? I haven't tried it but the documentation says:
Return Value
An array of NSString objects containing the union of
the types of data declared for all the pasteboard items on the
receiver. The returned types are listed in the order they were
declared.
With this method you can probably query the pasteboard types, even if you won't be able to read them.

Comparing items using number codes (JSON)

in my app, the users can create groups. When a user creates a group, a new folder is uploaded on dropbox, and inside there is a JSON file like this:
{"group":"0864798478"}
The code you see is generated randomly in the following way:
NSString *randomKey = [NSString stringWithFormat:#"%0.10u", arc4random()];
Then I have a table view which displays all the folders (groups). But I would like the UITableView to display only the groups which mach with the codes the iPad has saved on.
So if I have:
{"group a":"0864797073"}
{"group b":"0764898478"}
{"group c":"2864758479"}
And on the iPad there is a file containing the code:
0864797073 (group a)
The table View displays only that group, not all.
How can I do this??
Thanks in advance for the help!!
You can use JSONKit or SBJson for parse JSON files and obtain NSDictionaries with the data. Then you can create a NSDictionary in wich you could save the result of comparing previous dictionaries, an then show the result in the table with method tableView:cellForRowAtIndexPath. OR, make the comparison in the method tableView:cellForRowAtIndexPath. Good Luck!
EDIT:
If the file reads:
{"group a":"0864797073"}
You parse it and get a dictionary like in wich "group a" will be the KEY and the NSString "0864797073" will be the value. So to compare the dictionary wich holds group a with wich holds group b you have to do:
if([[firstParsedJson objectForKey:#"group a"] isEqualToString:[secondParsedJson objectForKey:#"group b"]])
{
//Do some stuff...
}
Where firstParsedJson and secondParsedJason are NSDictionaries that represent parsed json with, for example, SBJson.
As Luis has mentioned, SBJson is the easiest framework that you allows you to parse JSON using objective-C. You can parse the JSON and go through the CellForRowAtIndexPath delegate method of the UITableView to show all the values.
If you have having any troubles with visulizing the JSON then this link will help you to get a better idea of how your JSON is being created.

How can i save JSON objects to Core Data?

I'm a nwebie in Core Data, i have designed a navigation based application and some of the data i use are created on run time(come from a URL via JSON). I took a few tutorials an searched for almost a day but haven't still realized how to save the incoming JSON data to the Entity (or event?) in my Core Data model. I fetch the data in the DetailViewController class and i need to save this data to Core Data(I have prepared an Entity with 7 properties). Can anyone please help?(If you know a good tutorial or sample code i will be pleased)
EDIT This may be a little specific but i really have trouble with and need just a little help.
My data comes to the app from a kind of restful server(i wrote it in PHP), firstly user enters his/her login informations(which i have saved to the database on server before) and when the response data comes i will use different elements of it in differen views(for example the user_id will be used on a view and the buttonData etc on other views). My question is, how will i save JSON data into my core data model(has tree Entities for the moment). Thanks in advance
Note: I lokked arround a lot but couldn't find any answer&tutorial about an app like mine
The best way to do that would be to create entities corresponding to JSON structure. Easiest was is when each JSON object becomes an entity, and arrays become arrays of entities. Be reasonable, however, and don't introduce too much overkill for JSON subobjects that are essentially part of its superobject.
When you have created entities, you can start off with the parsing and translation. Use some JSON framework (starting from iOS5 there's one from Apple) and parse JSON string into object tree, where root item is either an NSArray or NSDictionary, and subelements will be NSArray, NSDictionary, NSNumber, NSString or NSNull.
Go over them one by one in iterational loops and assign according values to your core data entity attributes. You can make use of NSKeyValueCoding here and avoid too much manual mapping of the attribute names. If your JSON attributes are of the same name as entity attributes, you'll be able to just go over all dictionary elements and parse them into attributes of the same name.
Example
My parsing code in the similar situation was as follows:
NSDictionary *parsedFeed = /* your way to get a dictionary */;
for (NSString *key in parsedFeed) {
id value = [parsedFeed objectForKey:key];
// Don't assign NSNull, it will break assignments to NSString, etc.
if (value && [value isKindOfClass:[NSNull class]])
value = nil;
#try {
[yourCreatedEntity setValue:value forKey:property];
} #catch (NSException *exception) {
// Exception means such attribute is not defined in the class or some other error.
}
}
This code will work in trivial situation, however, it may need to be expanded, depending on your needs:
With some kinds of custom mappings in case you want your JSON value be placed in differently named attribute.
If your JSON has sub-objects or arrays of sub-objects, you will need to detect those cases, for example in setters, and initiate new parsing one level deeper. Otherwise with my example you will face the situation that assigns NSDictionary object to an NSManagedObject.
I don't think it is reasonable to dive into these, more advanced matters in scope of this answer, as it will expand it too much.
I suggest you to use this library : https://github.com/TouchCode/TouchJSON
And then if you want to make a factory to parse json and feed your code data, you can use selectors to call methods to fill all your attributes.
Chances are your JSON data gets converted to an NSDictionary or NSArray (or some combination of the two). Simply extract the key/values from the JSON structure and add them to your entity class.
This lib helps me lot
Features
Attribute and relationship mapping to JSON key paths.
Value transformation using named NSValueTransformer objects.
Object graph preservation.
Support for entity inheritance
Works vice-versa

How to do string manipulation in an NSValueTransformer?

I have a table column that I'm binding to a value in an NSArrayController. What I'm trying to do is display only a substring of the actual value in the array controller. The way I've been trying to do that so far is by creating an NSValueTransformer subclass, and then doing the string manipulation in the transformedValue method. However, I can't figure out how to get the incoming value turned into a string (it's of type NSConcreteValue), and maybe there's an easier way to do this without value transformers.
Sounds like you're doing presentation-side formatting, in which case you should be using a formatter instead.
Then again, if this is a string containing multiple values (e.g., something like “from 42 to 100”), you should make a model object from it instead, and store those in the array controller. Then you can bind your table columns to specific properties of the model objects, and not have to worry about picking the string apart and then reassembling it (except when you load and later save the model).
Edit: Never mind; I didn't see that the object values are NSValues, not NSStrings.
You can get a string representation of any object using the -description method, but for instances of NSValue it's unlikely to print anything especially meaningful. In other words, it's up to your value transformer to interpret the passed-in object and produce a string. If it's an NSValue instance, the question is what type of data that instance contains. Once you know that, you can write code to represent it as a string (similar to NSStringFromRect()).