objective c creating app ui from xml

I am trying to parse xml (attached) and create a ui from it dynamically - with no success. any ideas?

the XML looks like this:

<root>
 <view id="1" color="0.8 0.7 0.9 1.0" x="0" y="0" width="320" height="480">
  <view id="2" color="0.9 0.1 0.3 1.0" x="20" y="20" width="280" height="200">
     <textfield id="5" x="10" y="90" width="180" height="40" placeholder="enter text" color="1.0 1.0 1.0 1.0" target_id="6"></textfield>
     <button id="4" color="0.2 0.2 0.2 1.0" x="220" y="90" width="40" height="40" title="push"></button>
  </view>
  <view id="3" color="0.1 0.8 0.3 1.0" x="20" y="240" width="280" height="100">
     <label id="6" color="0.5 0.5 0.5 1.0" x="20" y="20" title="some label..." width="200" height="80"></label>
  </view>
</view>
</root>

Thanks!

Answers


In the absence of any more information, I'm going to assume you're asking for the best way to parse XML in Objective-C. And the answer to that question is to use the NSXMLParser class.

The class that you use the NSXMLParser in has to be flagged as being an NSXMLParserDelegate, so your .h file will look something like:

#import <UIKit/UIKit.h>

@interface MyParsingController : UIViewController<NSXMLParserDelegate>

@end

Now, assuming you've got all of the XML you posted in your question in an NSString called XMLString, you can then turn the string into an XMLData object, initialize your NSXMLParser, and set it going by calling its parse method:

NSData * XMLData = [XMLString dataUsingEncoding:NSUnicodeStringEncoding];
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:XMLData];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES];
[parser parse];

Once the parser is running, it'll call its delegate methods as it goes through your XML data, and you then need to interpret that data to construct a data model which you can then use to build your GUI. I'm not going to write that bit for you, but as an example of how to parse some XML, here's the code from one of my recent projects:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict{

    if([elementName isEqualToString:@"device"]){
        self.currentDeviceID = [[SRDeviceID alloc]init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if([elementName isEqualToString:@"deviceList"]){
        [self.pairingTableView reloadData];
    }


    if([elementName isEqualToString:@"device"]){
        [self.deviceIDListFromServer addObject:self.currentDeviceID];
    }

    NSString* finalParsedString = [self.currentParseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


    if([elementName isEqualToString:@"deviceName"]){
        self.currentDeviceID.deviceName = finalParsedString;
    }

    if([elementName isEqualToString:@"UUID"]){
        self.currentDeviceID.uuid = finalParsedString;
    }

    if([elementName isEqualToString:@"deviceLocation"]){
        self.currentDeviceID.location = finalParsedString;
    }

    if([elementName isEqualToString:@"deviceLastSeenTime"]){
        self.currentDeviceID.lastSeenTime = finalParsedString;
    }

    self.currentParseString = [NSMutableString stringWithString:@""];

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    [self.currentParseString appendString:string];
}

Hope that helps.


Need Your Help

Creating table with the same columns as in a csv

sql sql-server-2005 tsql

I am writing a stored procedure which is supposed to take data from a csv file and insert into a table. My problem is that the number of columns in the csv file are not fixed(ie number of columns is

What is the typical interrupt latency for WinCE 6.0 on an ARM target?

windows-ce arm latency interrupt

What is a typical interrupt latency for WinCE 6.0 on an ARM target?

Cross browser issue with offset() jquery function

javascript jquery internet-explorer-7 cross-browser offset

I am having a cross browser issue with the offset() function in jQuery. For example, I am looking for the offset of an anchor tag