How to use TouchXML

Discussion in 'iOS Programming' started by SqueegyX, Jul 21, 2008.

  1. SqueegyX macrumors regular

    Mar 24, 2008
    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.

    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.
  2. DaveGee macrumors 6502a

    Jul 25, 2001
    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!

    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...

  3. SqueegyX thread starter macrumors regular

    Mar 24, 2008
    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.

  4. meclizine50mg macrumors newbie

    Jun 11, 2008
    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.
  5. SqueegyX thread starter macrumors regular

    Mar 24, 2008
    Still got nowhere :(

    My Objective_C noobness is showing, I'm sure.

    Attached Files:

  6. TonyHoyle macrumors 6502a

    Sep 14, 2007
    Manchester, UK
    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.
  7. SqueegyX thread starter macrumors regular

    Mar 24, 2008
    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 :)
  8. yickhong-hong macrumors newbie

    Nov 18, 2008
    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?
  9. XuishX macrumors newbie

    Oct 26, 2008
  10. Isorinu' macrumors newbie

    Oct 16, 2008
  11. fenrus110 macrumors regular

    Mar 24, 2008

Share This Page