Read XML and show it at an TableView - iphone

I want to do something very simple - read a XML from the web and then show (in a TableView) the contents of the titulo in the xml.
Here's what it looks like:
<?xml version="1.0" ?>
<lista>
<edicao>
<ano>2011</ano>
<mes>1</mes>
<titulo>01/2011</titulo>
<file>01-2011.pdf</file>
</edicao>
<edicao>
<ano>2010</ano>
<mes>4</mes>
<titulo>04/2010</titulo>
<file>04-2010.pdf</file>
</edicao>
</lista>
I've searched a lot, but I was unable to find how to do it. Most of the examples already had a class named XMLParser that I don't know how to create. I looked at the libraries too, but I just can't find out how to do it. If anyone can advise me how to do this it would be great. Thank you!

Here is a tutorial that shows XML parsing using GDataXMLParser.
how-to-read-and-write-xml-documents-with-gdataxml
GDataXMLParser is better than NSXMLParser since latter is slower.

The Cocoa framework provides an XML parser class called NSXMLParser. You can find out more about it at http://developer.apple.com/library/ios/#documentation/cocoa/reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html#//apple_ref/occ/cl/NSXMLParser.

There is already a great library out there that makes it really easy to parse XML files.
It is called TouchXML and you can find it on github here.
There are a bunch of tutorials online but is is very easy to use.
This is the one I learned with.

http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/
This helped me...Crate .h and .m from the XMLParser.h and .m then just copy paste the code in it. and use the cell creation function as given in example.

Use the NSXMLParser class with the NSXMLParserDelegate methods. You must be init the parser:
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.site.com/example.xml"];
parser.delegate = self;
[parser parse];
And use the delegate methods.

Related

Display data on UI from XML in project iPhone

I need to know that i have a XML file, in my project I have some data in it.
How can i display that data in TableView, Tutorials given on internet serve the purpose to display XML data that is through some URL, i just want to display from my local file in project.
I assume you are using Objective-C, am I correct?
Have you tried using NSXMLParser. I would assume you would be able to just use the contents of the file in your project and parse it with NSXMLParser.
You might want to have a look at this NSXMLParser example
NSString *xmlFilePath=[[NSBundle mainBundle] pathForResource:#"TesingSampleXml" ofType:#"xml"];
NSString *xmlString=[[NSString alloc]initWithContentsOfFile:xmlFilePath];
NSData* xmlData=[xmlString dataUsingEncoding:NSUTF8StringEncoding];
xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
[xmlParser setDelegate:self];
[xmlParser parse];
Now Parse the data using the delegates of the NSXMLParserDelegates and then reload the tableview.
Best XML Parser tutorial
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
To display data from xml file you need to parse the xml file. To parse file you can use any parser like - NSXMLParser, tbxml and so on
*I recommend TBXML * it is fast and light
go through this link for more details

Parsing XML by TBXML works on demo project, but it doesnt work in my real project

I'm really need help with parsing XML by TBXML.
I created a demo project with a view has a button,when users press this button, I just call TBXML parsing function from another class.
Im doing this to test my TBXMLParsing function works correctly or not. And it works pretty well in my demo project.
Finally, after I tested it, I copy and paste the function into my real project which has a button in first view, when I click on that button then it calls TBXMLParsing function. In other words, it is basically same as demo project. However, TBXMLParsing function doesn't work. It cant traverse thru all elements.
Do you have any ideas? Thanks for helping me.
Update:
It is working if I simulate my app on real device, but it does not traverse thru the XML if I simulate it on simulator. It is so weird. However it does work on simulator if I create new project. Thus, there must be something that in my app that prevent parser XML.
Any ideas? Thank you
Update2:
NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:#"http://dl.dropbox.com/u/37064335/ex.xml"]];
TBXML *tbxml = [[TBXML alloc] initWithXMLData:data];
[data release];
TBXMLElement *root = tbxml.rootXMLElement;
// if root element is valid
if (root) {
NSLog(#"root =%s",root->name);
This is the part messed me up. I tested in demo app, Nslog root=GetUserAccountsRespond
However in my app Nslog root= (nil-nothing printed out). Both projects are basically the same.
Ok Here is solution for anyone that might has same problem as me. The problem was because when I Analyzed(shift+command+B) my project, xcode detected some "Semantic Issue" in TBXML.m. Then I just followed xcode's guide to change "=" to "==" in 3 places where it said "Process XML"(line258,311,and458) in TBXML.m. Thus TBXML does not work, because it cant process input XML. This problem drove me crazy for 2days. Thanks Tom Bradley for TBXML
I would check that the XML document you are trying to parse is loaded correctly before passing it onto TBXML to decode. It sound to me like there is a problem with the loading code on the sim. Try outputting the file contents to the log to verify its correct.

undeclared object but everything implemented

i implemented first three20 (first, with the python scrip) and then restkit (second,manual).
i started adding some stuff to my app delegate, but then i get an error [RKObjectLoaderTTModel undeclared. Other Objects RKClient or RKObjectManager work fine.
// work
[RKClient clientWithBaseURL:APP_BASE_URL username:USER_EMAIL password:USER_PASSWORD];
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:APP_BASE_URL];
//fail
[RKObjectLoaderTTModel setDefaultRefreshRate:1];
looking into RestKit/Build/RestKit/Three20 i can see RKObjectLoaderTTModel header.
i added all header search paths, etc , pp.
i think you get a better view, when looking into my project, sohere is it (have nothing done yet, so no problem with sharing): http://dl.dropbox.com/u/80699/project_for_stack.zip
hopefully somebody can help.
#import <RestKit/Three20/Three20.h>
had to import explicit the restkit three20 stuff.
hope this helps somebody. now everything runs fine.

Identify Html tags on Xml parsing

I am doing an xml parsing and the response i am getting is a html code ..is it possible to load that response in a uiwebview..?
Thanks in advance
The following methods are probably going to be what you're looking for:
[webView loadHTMLString:NSString baseURL:NSURL];
[webView loadData:NSData MIMEType:NSString textEncodingName:NSString baseURL:NSURL];
You can pass an NSString or NSData object to the appropriate method and have it loaded into your webView easily.
Good luck,
Aurum Aquila
P.S. Those method signatures are hyperlinked.

is there anyway to display HTML content in textview?

i tried the following one from iphone cook book .i displayed HTML content through accessing private API of UITextview like
#interface UITextView (extended)
- (void)setContentToHTMLString:(NSString *) contentText;
#end
it works FINE.but Apple wont allow accessing private API.any solution pls?
Do you want to display actual HTML elements or just a string you took off the web? For the latter, you would do the following:
NSURL *stringURL = [NSURL URLWithString:#"http://yoursite.com/page.html"];
NSString *responseString = [NSString stringWithContentsOfURL:stringURL encoding: NSUTF8StringEncoding error:nil];
myTextView.text = responseString;
You could try using the Three20 library, which includes a styled text label:
http://github.com/facebook/three20/
The class TTStyledTextLabel of the Three20 framework has a text property "text" of type TTStyledText. This TTStyledText class has a textFromXHTML: static method which takes an NSString with any kind of HTML text inside, and might help you do what you want. However, it does not allow editing of the text, at least not as far as I know.
Hope this helps!
If you want HTML, your best bet is to use a dedicated UIWebView.
You can try this one.This is uses core text framework
https://github.com/aryaxt/iOS-Rich-Text-Editor