【问题标题】:iOS - parse xml DATASETiOS - 解析 xml 数据集
【发布时间】:2015-01-01 05:52:20
【问题描述】:

我要解析的 Xml 数据

<DataTable xmlns="http://tempuri.org/“>

<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-     microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns=“">

<Products diffgr:id="Products1" msdata:rowOrder="0">
<ItemCode>001G1-R25111-1701</ItemCode>
<ItemName>Autocad 2015</ItemName>
<Price>160714.000000</Price>
</Products>
<Products diffgr:id="Products2" msdata:rowOrder="1">
<ItemCode>011-0656</ItemCode>
<ItemName>"SVC, HARDWARE TEST 1"</ItemName>
<Price>0.000000</Price>
</Products>
<Products diffgr:id="Products3" msdata:rowOrder="2">
<ItemCode>011-0657</ItemCode>
<ItemName>"SVC, HARDWARE TEST 2"</ItemName>
<Price>0.000000</Price>
</Products>

</NewDataSet>
</diffgr:diffgram>
</DataTable>

This is my view.h
#import <Foundation/Foundation.h>

@interface view : NSObject
{
int *itemcode;
NSString *itemname;
NSString *price;
}


@property (nonatomic, assign) int *itemcode;
@property (nonatomic, retain) NSString *itemname;
@property (nonatomic, retain) NSString *price;
@end

view.m

#import "view.h"

@implementation view
@synthesize itemcode,itemname,price;

@end

下面是我用来解析数据的代码:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
if( [elementName isEqualToString:@"DataTable"])
{
    if(!soapResults)
        soapResults = [[NSMutableString alloc] init];
    recordResults = TRUE;
}

if([elementName isEqualToString:@"NewDataSet"]) {

    messages = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Products"]) {

    aMessage = [[view alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if( recordResults )
    [soapResults appendString: string];

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

if([elementName isEqualToString:@"Products"]) {

    [messages addObject:aMessage];

    [aMessage setValue: soapResults forKey:elementName];
    NSLog(@"MESSAGES COUNT: %d",messages.count);
    NSLog(@"MESSAGE: %@",aMessage);

    aMessage = nil;
}
// as this is the last element
if( [elementName isEqualToString:@"NewDataSet"])
{
    recordResults = FALSE;
}
}

我没有得到解析的数据,它在 didendelement 中崩溃了。 'NSUnknownKeyException',原因:'[setValue:forUndefinedKey:]:这个类不符合键 ItemCode 的键值编码。'

【问题讨论】:

    标签: ios xml parsing dataset nsxmlparser


    【解决方案1】:

    在 view.h 文件中你应该将变量声明为

     int * ItemCode;
     NSString * ItemName;
     NSString * Price;
    

    变量应该等于标签名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 2012-04-10
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      相关资源
      最近更新 更多