본문 바로가기

프로그램/iPhone

TouchXML installation guide

TouchXML installation guide

TouchXML is a libxml API wrapper written in Objective-C and usually helps with all your project XML needs. While writing my post about parsing XML element attributes and putting up demo project I realized that I tend to forget how to add TouchXML to new project, so here goes step-by-step of that procedure:

1. Get TouchXML

You can find archives to download in touchcode project downloads . Download TouchXML archive and extract it anywhere you like. It’s common practice to keep such libraries and classes in Developer/ExtraLibs directory.

2. Enable libxml2 library

First things first, before we actually add TouchXML files, we need to do some project configuration changes, so our project could use libxml library.

1. Go to “Project -> Edit project settings”

2. Activate “Build” tab

3. Search for “Header search paths” setting and add /usr/include/libxml2 value to it

libxml header search paths

4. Search for “Other linker flags” setting and add -lxml2 value

touchxml other linker flags

P.s. notice that search function is really useful for finding settings you need faster.

3. Add TouchXML classes

1. Right click (option click) on your projects “Classes” folder and go to “Add -> Existing files…”

2. Navigate to the directory where extracted TouchXML is kept and browse  deeper to “Common -> Source”. Select everything! And click “Add” obviously.

touchxml classes

3. Confirm.

Now you should see a bunch of new files in your project. I usually group them by selecting files I wish to group and then selecting “Group” in context (right click/option click) menu.

4. Import TouchXML to your project

1 #import "TouchXML.h"

That is all the “magic” and you’re good to go. Since, I am not going to write about actually using TouchXML, you can see a nice working example in my previous post.

5. Common errors

Error: libxml/tree.h: No such file or directory

… and hundreds of something missing errors. It means that something went wrong with “Header search paths”. Maybe you didn’t added /usr/include/libxml2
or added incorrectly? Check it.

Error: “_xmlDocDumpFormatMemory”, referenced from:- [CXMLDocument description] in CXMLDocument.o

… and tens of errors like this. While errors by them selfs aren’t very expressive, they wish to inform you, that you did not added -lxml2 flag to “Other linker flags”

And that is all for now!

Tags: , ,





Example
========================================================================================================================================================================

Parsing XML element attributes with TouchXML

Assuming that you already familiar with parsing XML with a little help from TouchXML we will go straight to the topic: parsing an attribute! I’ll just put some “sample” XML here so it would be easier to understand what’s happening in code later.

<pigletlist>
<piglet id="1">
    <name>Nifnif</name>
</piglet>
<piglet id="2">
    <name>Nufnuf</name>
</piglet>
<piglet id="3">
    <name>Nafnaf</name>
</piglet>
</pigletlist>

Well, here we have our tree little piglets put in a single XML file and bellow lies the magic code which parses piglet list with their id attributes. If you are too lazy to look trough all the code (I would be), key point is attributeForName method of CXMLElement object used like this: [[node attributeForName:@"id"] stringValue].

//  we will put parsed data in an a array
NSMutableArray *res = [[NSMutableArray alloc] init];
 
//  using local resource file
NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"piglets.xml"];
NSData *XMLData   = [NSData dataWithContentsOfFile:XMLPath];
  CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:XMLData options:0 error:nil] autorelease];
 
NSArray *nodes = NULL;
//  searching for piglet nodes
nodes = [doc nodesForXPath:@"//piglet" error:nil];
 
for (CXMLElement *node in nodes) {
  NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  int counter;
  for(counter = 0; counter < [node childCount]; counter++) {
    //  common procedure: dictionary with keys/values from XML node
    [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]];
  }
 
  //  and here it is - attributeForName! Simple as that.
  [item setObject:[[node attributeForName:@"id"] stringValue] forKey:@"id"];  // <------ this magical arrow is pointing to the area of interest
 
  [res addObject:item];
  [item release];
}
 
//  and we print our results
NSLog(@"%@", res);
[res release];

Our results:

2010-02-05 09:54:01.078 demo[1901:207] (
        {
        id = 1;
        name = Nifnif;
    },
        {
        id = 2;
        name = Nufnuf;
    },
        {
        id = 3;
        name = Nafnaf;
    }
)

Mysterious "text = \n"

Sorry, that my example caused frustration to everyone, I used cleaner XML than the posted one and let it be a lesson for me that I should post everything as it is. Anyway, cleaning all the white spaces and new lines from XML file is not an option for everyone, so the only solution I can think of would be to check if value for some key is empty.

1 NSString * value = [[[node childAtIndex:counter] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
2  if ([value length] != 0){
3 [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] localName]];
4  
5 }

Of course if your XML has "text" key and it can be empty - you should check for that too.

I hope this clears some things.

Tags: , , , ,