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

I can't access array/obj outside jQuery event function

javascript jquery arrays object global

So I've got an object, or array, declared at the beginning of anything, outside everything:

How to get the rendered DIV's width and height?

javascript jquery html css

I have a &lt;div&gt;, which is dynamically controlled by the JS, for example, some children are added dynamically. Is it possible to know the width &amp; height of the rendered DIV after the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.