![I tried to read xml file using Xcode but it give response in String and xml will show like in following image and it give < and > behalf of < and >][1]
I am trying to read mywebservice from server. i wrote web service which create file in xml formate in website.
when i check that file on website uing internet it shows like following:
<NewDataSet>
<Table>
<Column1>Audi</Column1>
</Table>
<Table>
<Column1>BMW</Column1>
</Table>
<Table>
<Column1>MINI</Column1>
</Table>
</NewDataSet>
but when i call that file via soap it gives response in following:
<NewDataSet>
<Table>
<Column1>Audi</Column1>
</Table>
<Table>
<Column1>BMW</Column1>
</Table>
<Table>
<Column1>MINI</Column1>
</Table>
</NewDataSet>
but i can't read the tag like 'NewDataSet' because it give back response in String and i am new in XML so please help me.. i use Xcode and nsmutabledata for that..!!
i tried stringbyReplaceofOccurance but it did not replace < and > with < >.
Thanks in Advance.
Thanks in advance for helping.
I think, try NSXMLParser
this might be helpful for you...
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
you use best way of read xml file with TouchXML ....and for TouchXML use this bellow link
http://dblog.com.au/general/iphone-sdk-tutorial-building-an-advanced-rss...
also here when you get response then other way you can read like bellow using TouchXML..
ie..
- (void) fetchDataSuccessforEvent:(NSMutableData *)data
{
CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:data options:0 error:nil] autorelease];
NSArray *nodes = [doc nodesForXPath:#"//Table" error:nil];
for (CXMLElement *node in nodes)
{
for(int counter = 0; counter < [node childCount]; counter++)
{
if ([[[node childAtIndex:counter] name] isEqualToString:#"Column1"])
{
NSString *string = [[node childAtIndex:counter] stringValue];
NSLog(#"\n\n Title %#",string);
}
}
}
}
so easy...
hope,this help you....
:)
This is not a parsing problem. Check the web service and make sure that xml header is: <?xml version="1.0" encoding="utf-8"?> or something like this.
Related
I'm using Youtube API, I'd like to have a search auto-complete feature, just like int the site, when you type into the search input box for iPhone App, it gives you terms suggestions. I've read the docs, but still missing, Is this possible using the API?
Well, i know it's too late to answer here, but i will post this answer because it's something that drove me crazy for a couple of days!!! and hope it will save to others...
So... i'm using this API :
http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&alt=json&q=%#
(q is the query for the autocomplete search).
Now, if you try to open a browser, paste this API and change q=%# to (lets say): q=br, you will notice that some file with the suffix .js is downloaded to your computer.
For some reason, i couldn't parse the JSON like that, so i did that trick:
#property(strong, nonatomic) NSMutableArray *ParsingArray // Put that in .h file or after #interface in your .m file
-(void)autocompleteSegesstions : (NSString *)searchWish{
//searchWish is the text from your search bar (self.searchBar.text)
NSString *jsonString = [NSString stringWithFormat:#"http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&alt=json&q=%#", searchWish];
NSString *URLString = [jsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // Encoding to identify where, for example, there are spaces in your query.
NSLog(#"%#", URLString);
NSData *allVideosData = [[NSData alloc]initWithContentsOfURL:[[NSURL alloc]initWithString:URLString]];
NSString *str = [[NSString alloc]initWithData:allVideosData encoding:NSUTF8StringEncoding];
NSLog(#"%#", str); //Now you have NSString contain JSON.
NSString *json = nil;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanUpToString:#"[[" intoString:NULL]; // Scan to where the JSON begins
[scanner scanUpToString:#"]]" intoString:&json];
//The idea is to identify where the "real" JSON begins and ends.
json = [NSString stringWithFormat:#"%#%#", json, #"]]"];
NSLog(#"json = %#", json);
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] //Push all the JSON autocomplete detail in to jsonObject array.
options:0 error:NULL];
self.ParsingArray = [[NSMutableArray alloc]init]; //array that contains the objects.
for (int i=0; i != [jsonObject count]; i++) {
for (int j=0; j != 1; j++) {
NSLog(#"%#", [[jsonObject objectAtIndex:i] objectAtIndex:j]);
[self.ParsingArray addObject:[[jsonObject objectAtIndex:i] objectAtIndex:j]];
//Parse the JSON here...
}
}}
That's it. now ParsingArray is the array that contains all autocomplete information from youTube! to be able to change it every time the user clicks another character on the searchBar, use this function:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self autocompleteSegesstions:self.searchBar.text];}
Now, this is the main code you should have. to make this code faster (because you can now see that you have a writing delay on the keyboard), use another thread to download ParsingArray or use Asynchronous block. (just insert the content of the first method to Async block...)
Remember- maybe there is another way to implement autocomplete youTube search much better then this, but i just did not found it, and i searched a lot! if anyone know a better way, i'll be more then glad if he post it here.
Have fun!!!
Not the Youtube API -- but you can use the Google Suggest API. Make calls to this URL:
http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&q=QUERY
Which will return a json response of the suggest terms that your app can parse and display. If you prefer XML to json, change
client=youtube
to
output=toolbar
(leave the rest of the parameters the same).
I am using GDataXML to parse may XML, but i have a probleme with this :
<....
<images xmlns:a="http://.../Arrays">
<a:string>http://images...233/Detail.jpg</a:string>
<a:string>http://images....233/Detail2.jpg</a:string>
</images>
.../>
i would like to have all the URL of my images and put it in an NSArray,i am doing like this :
NSError *error = nil;
GDataXMLDocument *xmlResult = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
if (error) {
NSLog(#"%#",error);
}
.......
NSArray *array = [... nodesForXPath:#"images" namespaces:a error:&error];
my array it's not null
Now i would like to access my url of images but i can not, i am doing like this:
arrar = [array elementsForName:#"a"];
byt my array is null, i think that the problemm is with namespaces wut i don't konw how to resolve it
Thanks for your answer
I think this question is related to this: parse xml with namespaces with gdata xml. Checkout my answer. Hope it helps! :)
I am trying to upload images to yFrog which is working just fine, but I want to grab just the URL from the response. When I use the NSURLConnection method
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString* responseString = [[NSString alloc] initWithData:webData
encoding:NSUTF8StringEncoding];
// NSString *url = [webData valueForKey:#"mediaurl"];
NSLog(#"result: %#", responseString);
}
I get this as my response string
result: <?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
<mediaid>hszuhsp</mediaid>
<mediaurl>http://yfrog.com/hszuhsp</mediaurl>
</rsp>
As you can see in my blocked out code I tried to give my NSMutableData to give me the value of the key #"mediaurl" which just crashes. I think this should be relatively easy but for some reason I just can not figure out how to just grab the URL out of the response. Any help would be greatly appreciated. Thank you
This is a valid xml response.
If you need to parse only this response, you can iterate the NSString.
But if you have other response in xml format then the best approach is you are going to parse it using any XML parser.. :)
Here is how to choose your xml parser, and then you will search for tutorial accordingly.
you can't use the valueForKey on the instance of NSMutableData class., you should use the XML parser to extract the value.
I'm trying to parse the HTML presented below with TouchXML but it keeps crashing when I try to extract certain attributes. I'm totally new to the parser world so I apologize for being a complete idiot. I need help to parse this HTML. What I'm trying to accomplish is to parse each attribute and value or what not and copy them to a string. I've been trying to find a good parser to parse HTML and I believe TouchXML is the best I've seen because of Tidy. Speaking of Tidy, How could I run this HTML through Tidy first then parse it? I'm not sure how to do this. Here is the code that I have so far that doesn't work due to it's not pulling everything I need from the HTML. Any help or advice would be much appreciated. Thanks
My current code:
NSMutableArray *res = [[NSMutableArray alloc] init];
// using local resource file
NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"example.html"];
NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath];
CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:XMLData options:0 error:nil] autorelease];
NSArray *nodes = NULL;
nodes = [doc nodesForXPath:#"//div" error:nil];
for (CXMLElement *node in nodes) {
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:[[node attributeForName:#"id"] stringValue] forKey:#"id"];
[res addObject:item];
[item release];
}
NSLog(#"%#", res);
[res release];
HTML file that needs to be parsed:
<html>
<head>
<base target="_blank" />
</head>
<body style="margin:2;">
<div id="group">
<div id="groupURL">Group URL</div>
<img id="grouplogo" src="http://images.example.com/groups/image.png" />
<div id="groupcomputer">Group title this would be here</div>
<div id="groupinfos">
<div id="groupinfo-l">Person</div><div id="groupinfo-r">Ralph</div>
<div id="groupinfo-l">Years</div><div id="groupinfo-r">4 years</div>
<div id="groupinfo-l">Salary</div><div id="groupinfo-r">100K</div>
<div id="groupinfo-l">Other</div><div id="groupoth" style="width:15px">other info</div>
</body>
</html>
EDIT: I could use Element Parser but I need to know how to extract the Person's Name from the following example which would be Ralph in this case.
<div id="groupinfo-l">Person</div><div id="groupinfo-r">Ralph</div>
I don't know if you are doing something wrong, but I recommend you to use element parser, the best parser for XML and HTML i've found. Hope this helps.
I'm trying to parse a Stack Overflow RSS feed of a specific question:
https://stackoverflow.com/feeds/question/2110875
For this I'm using the TouchXML library. There seems to be a problem in the following code:
CXMLDocument *parser = [[CXMLDocument alloc] initWithData:sourceData options:0 error:nil];
NSArray *allEntries = [parser nodesForXPath:#"//entry" error:nil];
NSLog(#"Found entries: %d",[allEntries count]); //Returns 0
The NSLog statement should return the count of all entries in the feed. In this case it should be '3', problem is that it returns 0.
I found that this piece of code does work:
CXMLDocument *preParser = [[CXMLDocument alloc] initWithData:sourceData options:0 error:nil];
NSString *sourceStringUTF8 = [preParser XMLString];
[preParser release];
CXMLDocument *parser = [[CXMLDocument alloc] initWithData:[sourceStringUTF8 dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSArray *allEntries = [parser nodesForXPath:#"//entry" error:nil];
NSLog(#"Found entries: %d",[allEntries count]); //Returns 3, which is ok
But using this seems hacky (it probably is) and introduces a few other sporadic bugs.
As far as I know the Xpath expression is correct. I've checked it using this page as well.
Can anyone help me with this problem, or point me in the right direction.
Thanks.
I had a very similar problem. This has something to do with the xml namespace, which TouchXML doesn't support very well (a known issue).
I believe that in your hack, the namespace wasn't passed into the second parser, that's why it works.
A easier way is just to change
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
replaced with simply
<html>
and xPath now works.
Maybe start by actually using that error argument to nodesForXPath:error to see if it returns an error? And check if allEntries is not nil after making that call?