【问题标题】:IPhone Development - Generate XML FileiPhone 开发 - 生成 XML 文件
【发布时间】:2010-09-23 00:38:29
【问题描述】:

这里有人知道如何在 iPhone 中生成 XML 文件或字符串吗?

【问题讨论】:

  • 你到底想达到什么目的?如果您只想将简单的字典/数组转换为 XML 格式字符串,那么只需几行代码。如果您需要更复杂的东西,那么我相信有一些开源库可以做到这一点。如果您想将自己的对象转换为特定的 XML,那么我想您将不得不手动构建 XML 字符串...
  • -1 含糊不清。您想要任何 XML 文件、通用 XML 编写器还是特定的 XML 格式?
  • 在我的问题中,我说过“一个 XML 文件或字符串”,它表示任何类型的 XML 文件或字符串。
  • 请注意我的answer

标签: objective-c ios xml cocoa-touch


【解决方案1】:

试试开源XML stream writer for iOS:

  • 用 Objective-C 编写,单个 .h。和 .m 文件
  • 一个@protocol 用于命名空间支持,一个用于不支持

例子:

// allocate serializer
XMLWriter* xmlWriter = [[XMLWriter alloc]init];

// start writing XML elements
[xmlWriter writeStartElement:@"Root"];
[xmlWriter writeCharacters:@"Text content for root element"];
[xmlWriter writeEndElement];

// get the resulting XML string
NSString* xml = [xmlWriter toString];

这会产生以下 XML 字符串:

<Root>Text content for root element</Root>

【讨论】:

    【解决方案2】:
    <?xml version="1.0" encoding="UTF-8"?> <parent>
    <child name="James"> <age>10</age> <sex>male</sex>
    </child> </parent>
    </xml>
    

    就像我使用这个 xml 写入.h文件............

    #import <UIKit/UIKit.h>
    @interface RootViewController : UIViewController <NSXMLParserDelegate> {
     @public
    }
    NSXMLParser *myXMLParser;
    @property (nonatomic, retain) NSXMLParser *myXMLParser;
    @end
    write in .m file
    - (void)viewDidLoad { [super viewDidLoad];
    NSBundle *mainBundle = [NSBundle mainBundle];
     NSString *xmlFilePath = [mainBundle pathForResource:@"MyXML" ofType:@"xml"];
    if ([xmlFilePath length] == 0){ /* The file could not be found in the resources folder.
    }
     return;
    /* Let's see if the XML file exists... */
     NSFileManager *fileManager = [[NSFileManager alloc] init];
    if ([fileManager fileExistsAtPath:xmlFilePath] == NO){
    /* The XML file doesn't exist, we double checked this just to make sure we don't leave any stones unturned. You can now throw an error/exception here or do other appropriate processing */
    NSLog(@"The file doesn't exist.");
     [fileManager release]; 
    return;
    [fileManager release];
    /* Load the contents of the XML file into an NSData */ NSData *xmlData = [NSData dataWithContentsOfFile:xmlFilePath];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
    self.myXMLParser = xmlParser; [xmlParser release];
    [self.myXMLParser setDelegate:self];
    if ([self.myXMLParser parse] == YES){
    /* Successfully started to parse */ } else {
    }
    /* Failed to parse. Do something with this error */ 
    NSError *parsingError = [self.myXMLParser parserError]; 
    NSLog(@"%@", parsingError);
    
    - (void)    parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
    
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict
    {
    NSLog(@"Element Name = %@", elementName); 
    NSLog(@"Number of attributes = %lu",(unsigned long)[attributeDict count]); 
    if ([attributeDict count] > 0)
    {
    NSLog(@"Attributes dictionary = %@", attributeDict); 
    NSLog(@"========================");
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-11
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 2012-08-13
      • 1970-01-01
      • 2020-09-20
      • 2011-11-29
      相关资源
      最近更新 更多