I'm not sure about parsing an xml with attributes. have seen a similar question here
But it shows to get an attribute of intValue. But i need to get the attributes of string type,How to do that?? Images of xml and the relevant portions are given in the following links
Click here for xml and here for required data
This answer to the linked question should work for you as well. The contents of attributeDict are already NStrings. All that is going on extra in the linked answer that they are calling the intValue method on the returned NSString to parse that string into an int. In your case, you don't need this little bit of an extra step. If you just do this:
NSString * stringValue = [attributeDict objectForKey:#"attribute"];
you'll have the value of the attribute called "attribute" in a string.
Related
In my app Web services are created in dot net and i am consuming those and I am getting response.In that all the fields like company,type,location everything are strings and there is no problem with this..And there is one more field called Exhibit number actually it is a Integer but they are created as string only.While I am displaying this it is showing zero instead of that number.. Here is my code...
//Storing into Array
[SurveyFilesArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[dic objectForKey:#"FileName"],#"FileName",[dic objectForKey:#"ExibhitNumber"],#"ExhibitNumber",[dic objectForKey:#"Description"],#"Description",[dic objectForKey:#"FileQuality"],#"FileQuality",nil]];
//Retrieving from Array..
NSLog(#"???%#???",[NSString stringWithFormat:#"%d",[[[SurveyFilesArray objectAtIndex:indexPath.row]objectForKey:#"ExibhitNumber"]intValue]]);
NSLog(#"%d",[[[SurveyFilesArray objectAtIndex:indexPath.row]objectForKey:#"ExibhitNumber"]intValue]);
You have typos in your code.
In the order of your code:
Ex-ib-hit-Number
Ex-hib-it-Number
These are 2 different strings.
In your example code you save it correctly written as Ex-hib-it. But you try to access it with ex-ib-hit afterwards. This cannot work.
I've combed the questions on here hoping to find a similar situation that I am in, but couldn't find one. My question deals with the NSJSONSerialization class in iOS 5, and how to handle parsing a single value returned from the JSONObjectWithData:options:error: method. The JSON data returned is a single value in an array that looks like this:
[1]
The data will either be 1 (as seen above) or 0, depending on the logic being done in the web service I'm using. As a side note, the web service is a WCF REST service. In XCode, the debugger displays the following after the deserialized JSON is assigned to a temporary NSArray object:
po parsedData
(NSArray *) $43 = 0x06e2ee70 <__NSCFArray 0x6e2ee70>( 1 )
When I try to get the value from the array using ObjectAtIndex:, I get this:
po [parsedData objectAtIndex:0]
(id) $44 = 0x06b1dad0 1
My question is: what is the hex value (0x06b1dad0) before the 1? Maybe a key or index that I am not accessing (I was thinking that I just didn't go far enough into the array to get the real value)? Does it have something to do with how the JSON is formatted? As I've been thinking about it, I have a feeling that the formatting is off.
The problem I am having is that I cannot get the actual value that is stored in the JSON message--when I access the element in the array parsedData and assign it to a variable, it is returning just the hex value and not 1.
Casting to NSInteger gives this:
po (NSInteger)[parsedData objectAtIndex:0]
(NSInteger) $45 = 112319184 1
Any insight into this would be greatly appreciated. I apologize if this has been addressed elsewhere in the forum.
Thanks.
Its likely that the object is an NSNumber try this:
[[parsedData objectAtIndex:0] intValue];
A long time ago I had to extract data from a string, and I went with a while loop that went through the whole string char by char extracting bits of data that I need. It wasn't very efficient but it worked.
In my latest app I would like to try and do it in the way that a good engineer would do it. Are there ways to search the string for an expression? or a sub string maybe?
For example out of the html in the string, there is a line that will contain a team name.
<td width="25%"><span class="teamname">Blue Bombers</span></td>
Is there a call I can do that would find the "teamname" and then extract the teamname from between the > <.
I could go char by char saving the last 10 chars to a string until the string equals "teamname", then keep going until i hit the > save everything i get until i again hit a <. but i guess thats taking the easy inefficient way.
Many Thanks
-Code
You can get the range of string "class" using NSRange, then do your logic... it will probably reduce the character searching..
Your code should be like follows,
if ([substring rangeOfString:#"class"].location != NSNotFound) {
// "class" was found
else {
// "class" was not found
}
If that's the only part of the string you're interested in and then just find a starting point like "teamname" via -rangeOfString:. If there's more than one occurrence then make repeated calls with -rageOfString:options:range:.
If you need more comprehensive parsing, however..
If this string is actual XHTML then you may be able to use one of the various XML parsers, e.g. TouchXML, and then find what you need via DOM lookups. However if (as seems likely) it's not pure XHTML then this is unlikely to help. In that case you might try loading up the HTML in an offscreen UIWebView and using JavaScript calls to find specific elements.
Say I have a property list that I store into an NSDictionary. An example output from NSLog follows:
2010-12-05 15:26:26.631 TestApp[598:207] Test contents: {
Address = "";
Name = "Test Dictionary";
What exactly would the value for Address be considered? I have heard many possibilities, but I'm not sure exactly. Is it NSNull, nil, what? What I would ultimately like to do is to create a second NSDictionary that filters out all of these blank values, as seen here. But first, I need to figure out what the blank value is counted as, so then I can parse my plist for them, and then discount the keys associated with them.
An empty string is a string, not NSNull or nil. So one way to test would be [#"" isEqualToString: thevalue].
I have some XML which looks like this:
<?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><array><data>
<value><array><data>
<value><dateTime.iso8601>20100508T14:49:56</dateTime.iso8601></value>
<value><string></string></value>
<value><string>comment</string></value>
<value><string></string></value>
<value><string>Milestone milestone1 deleted</string></value>
<value><int>1</int></value>
</data></array></value>
</data></array></value>
</param>
</params>
</methodRepsonse>
NSXMLParser seems to not be giving any data back for the blank values resulting in an array with 4 items in it instead of 6.
Is there anything I can do to NSXMLParser to make it return an empty string for the blank values so that I can maintain the order of the data when it is returned?
So after whipping up a quick sample with a delegate that just prints out what's happening during parsing I don't see anything at all wrong with what's being parsed.
I suspect however that you're relying on an incorrect expectation that between calls to didStartElement... and didEndElement... you should get a foundCharacters... call with an empty string? My question is based on the way you phrased the title of your question because there's no such thing as a "blank value." Either there is a value, or there isn't.
Imagine instead your XML contained <string/> instead of the exactly equivalent <string></string>. You still get start/end notifications.
You should be creating your NSMutableString (presumably the type that you're using for your <string> elements) in didStartElement... when <string> is found, appending to that string IF foundCharacters... is called (it can get called more than once with the value in chunks), and tossing it into your array when it's done on didEndElement....
If you really want to be more robust, you'll also be wanting detect an error condition if you find the start of a new element before your string ends, assuming that it is in fact an error for you.
Not quite sure I understand your problem here. NSXMLParser would at least report the beginning and end of the elements. Would that not be enough the get them in the right order?