Ios 目标c:从xml创建应用程序ui

Ios 目标c:从xml创建应用程序ui,ios,objective-c,xml,user-interface,Ios,Objective C,Xml,User Interface,我正在尝试解析xml(附件)并从中动态创建ui,但没有成功。 有什么想法吗 XML如下所示: <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="

我正在尝试解析xml(附件)并从中动态创建ui,但没有成功。 有什么想法吗

XML如下所示:

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


谢谢

在没有更多信息的情况下,我假设您正在询问Objective-C中解析XML的最佳方法。这个问题的答案是使用
NSXMLParser

在中使用
NSXMLParser
的类必须标记为
NSXMLParserDelegate
,因此.h文件看起来类似于:

#import <UIKit/UIKit.h>

@interface MyParsingController : UIViewController<NSXMLParserDelegate>

@end
一旦解析器运行,它将在遍历XML数据时调用其委托方法,然后您需要解释该数据以构建数据模型,然后您可以使用该模型构建GUI。我不打算为您编写这些内容,但作为如何解析某些XML的示例,以下是我最近一个项目中的代码:

- (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];
}

希望能有帮助。

你试过什么?从您的问题中,无法知道您是否在问“我如何解析XML?”到“我如何布局一组视图?”到“button id 4为什么不起作用?”也许他想回答您所有的问题,这是一个完整的应用程序!:-)我尝试过用NSXMLParser将xml解析到NSDictionary中,但我不确定这是最好的方法,因为我不知道如何从NSDictionary创建ui。哦,所以我的答案是浪费时间。这就是为什么你应该在你的问题中尽可能多地提供信息的原因:我本来可以花时间帮助你的。谢谢你的帮助。我是新来的,我会尽我所能在进一步的问题中更具体一些。
- (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];
}