PDA

View Full Version : How to use TouchXML




SqueegyX
Jul 21, 2008, 12:01 PM
I have an app that queries a web service, and need to parse the XML response. I got this all up and working with NSXMLDocument, until I installed to my device and find out that the iPhone OS doesnt actually have NSXMLDocument at all. Boo. Since I am more or less screenscraping HTML, NSXMLParser's event based parsing model seem like HUGE pain in the butt.

I read some people we're having good luck with TouchXML, however the documentation leaves a bit to be desired.

http://code.google.com/p/touchcode/wiki/TouchXMLHowTo

I can't figure out how to use it at all. I simply do a

#import "CXMLDocument.h"

And I get 254 build errors. I have added the source to the project, and its finding the files, but it cant seem to find huge amounts of constants and files like "libxml/tree.h"

It says to import "TouchXML.h" but there is no such file in the source. It also says to "Add the following lines to your project configuration"

OTHER_LDFLAGS = -lxml2
HEADER_SEARCH_PATHS = /usr/include/libxml2

But I'm not exactly sure where to do that in xcode? I'm sure this is a simple thing, but I can't quite figure it out. Thanks for the help.

[EDIT:] Ok, I figured out how to declare those config variables using the search field of the project info > Build screen. But sadly, it still seems libxml cant be found.



DaveGee
Jul 21, 2008, 07:36 PM
I can't help you with TouchXML but I was very much in the same boat as you and found the following thread supremely helpful!

http://forums.macrumors.com/showthread.php?t=499671

It references NSXMLParse and its the one (only?) part of NSXML* that made it over to the iPhone... It's really very simple once you get the hang of it. I consider myself a 'just above entry level' cocoa programmer and I took to it in a flash.

Sure I had to rework my program but it really wasn't that bad...

Dave

SqueegyX
Jul 21, 2008, 11:17 PM
Informative link, but this will be very hard for what I want to do. Since I am parsing HTML, I really need Xpath selection. Here's a snippet I have working in NSXMLDocument

NSArray *postElements = [[xml rootElement] nodesForXPath:@"//div[@class='threads']/div[@class='root']" error:nil];

In order to do this with NSXMLParser, I have to test to see if I have a div with a class of "threads", and store that in some sort of variable. From then on I have to look for divs with a class of root, if this variable states we are in the right element.

And sadly, attributes are not passed to the didEndElement so I don't know if the div that just ended is the div I reall want, or just a div inside my element. In order to track this I would have to create another instance variable to track the number of opens and closes of div that occured so I know when to stop caring about the content.

This requires 2 more instances variables, purely for temporary values, and is a lot more code to setup and make work than a simple XPath query.

Lastly, I have a few model classes. My NSXMLDocument pattern was to send large chunks of XML the initializers of those classes to construct objects from. Those initializers then initialized other clases based on elements it encounters by parsing in xml elements chunks. This keeps things clean and simple because each class is only parsing XML that directly pertains to it. Using the event based XML parser means that a single class has to parse all XML. This is messier and, once again, a lot more work. This wouldnt be bad for simple xml config files, but parsing complex HTML where elements of the same type meaning different things depending on where they are nested in the content are much harder to parse using this method.

Please, someone tell I am wrong and that there is an easy way to go about this. All I know is that my XPath based approach is working great in the simulator with a very small amount of code, and I quite sad the iPhone won't run it.

So, if I can get TouchXML running, it would be great since it supports almost the same API as NSXMLDocument does. I would only have to change the name of the classes I am using, and everything else should just work, I think.

Hmm...

meclizine50mg
Jul 22, 2008, 01:14 AM
Ok, I figured out how to declare those config variables using the search field of the project info > Build screen. But sadly, it still seems libxml cant be found.

I haven't yet tried TouchXML, but have you tried directly adding /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.0.sdk/usr/lib/libxml2.2.dylib to the resources folder in your XCode project? Does that help? I have to do the same thing when using SQLite (libsqlite3.0.dylib) and other external libraries to my project.

SqueegyX
Jul 22, 2008, 02:28 PM
Still got nowhere :(

My Objective_C noobness is showing, I'm sure.

TonyHoyle
Jul 22, 2008, 03:15 PM
In the iphone SDK libxml is in /usr/include/libxml2 so you'll have to either find a way to add that to the search path or change the #include lines to stick the libxml2/ in the front.

That's a big library to pull in though if you just want to parse an HTML page (which doesn't conform to XML standards anyway so probably won't load into the parser in the first place). You'd be better off just searching for the bit of page you want and parsing it directly IMO.

SqueegyX
Jul 22, 2008, 04:24 PM
I'm a complete retard. When I added the search path, I was adding it the "Release" configuration, not the debug config. I didn't realize they were separate.

It found libxml now and I can use TouchXML it seems.

Thanks for the help ful posts anyway, they expanded my education a bit :)

yickhong-hong
Nov 18, 2008, 08:55 PM
I'm a complete retard. When I added the search path, I was adding it the "Release" configuration, not the debug config. I didn't realize they were separate.

It found libxml now and I can use TouchXML it seems.

Thanks for the help ful posts anyway, they expanded my education a bit :)

I'm having the exact same problem, but unfortunately it seems not causing as yours....the configuration i have changed is fro "All Configuration"......any more ideas?

XuishX
Nov 19, 2008, 03:36 AM
Go to this link -> http://code.google.com/p/touchcode/wiki/TouchXMLHowTo


this link helped me alot when i was doing my touchxml too.


download the necessary files from here -> http://code.google.com/p/touchcode/downloads/list


Look at the comments people gave and hope it will help you ;)


u also need to download some touchxml files too , to be added into your own project.

Isorinu'
Nov 27, 2008, 03:47 AM
In this tutorial is a small guide for setting up the touchXML and parsing xml files with it..

http://dblog.com.au/general/iphone-sdk-tutorial-building-an-advanced-rss-reader-using-touchxml-part-1/

hope it helps..

fenrus110
Nov 27, 2008, 11:54 AM
since TouchXML basically is a replica of NSXML, just use the Tree-Based XML Guide

http://developer.apple.com/documentation/Cocoa/Conceptual/NSXML_Concepts/NSXML.html#//apple_ref/doc/uid/TP40001263