我是新手机编程。我有xml数据,我已经显示如下。使用该数据我想显示具有2个表的特定日期数据如何在单行中显示特定日期数据。在下一个数据2012 -06-20有3个表如何在第二行显示3tables数据。如何解析数据,告诉我逻辑
<Result>
<Table diffgr:id="Table1" msdata:rowOrder="2">
<CafeDateTime>2012-09-19T09:01:54-06:00</CafeDateTime>
<CreatedBy>1</CreatedBy>
<Email>YOURMAIL@CAFE.COM</Email>
<AdminPassword>admin1</AdminPassword>
<SalesDailyAmount>100.0000</SalesDailyAmount>
<PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="2">
<CafeDateTime>2012-09-19T10:01:54-06:00</CafeDateTime>
<CreatedBy>1</CreatedBy>
<Email>YOURMAIL@CAFE.COM</Email>
<AdminPassword>admin1</AdminPassword>
<SalesDailyAmount>100.0000</SalesDailyAmount>
<PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
</Table>
</Table> <Table diffgr:id="Table3" msdata:rowOrder="2">
<CafeDateTime>2012-09-19T09:01:54-06:00</CafeDateTime>
<CreatedBy>2</CreatedBy>
<Email>YOURMAIL@CAFE.COM</Email>
<AdminPassword>admin1</AdminPassword>
<SalesDailyAmount>200.0000</SalesDailyAmount>
<PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
</Table>
</Table> <Table diffgr:id="Table4" msdata:rowOrder="2">
<CafeDateTime>2012-09-19T10:01:54-06:00</CafeDateTime>
<CreatedBy>2</CreatedBy>
<Email>YOURMAIL@CAFE.COM</Email>
<AdminPassword>admin1</AdminPassword>
<SalesDailyAmount>200.0000</SalesDailyAmount>
<PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
</Table>
</Table> <Table diffgr:id="Table5" msdata:rowOrder="2">
<CafeDateTime>2012-09-19T11:01:54-06:00</CafeDateTime>
<CreatedBy>2</CreatedBy>
<Email>YOURMAIL@CAFE.COM</Email>
<AdminPassword>admin1</AdminPassword>
<SalesDailyAmount>200.0000</SalesDailyAmount>
<PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
</Table>
</Table> <Table diffgr:id="Table6" msdata:rowOrder="2">
<CafeDateTime>2012-09-19T09:01:54-06:00</CafeDateTime>
<CreatedBy>3</CreatedBy>
<Email>YOURMAIL@CAFE.COM</Email>
<AdminPassword>admin1</AdminPassword>
<SalesDailyAmount>150.0000</SalesDailyAmount>
<PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
</Table>
</Table> <Table diffgr:id="Table7" msdata:rowOrder="2">
<CafeDateTime>2012-09-19T10:01:54-06:00</CafeDateTime>
<CreatedBy>3</CreatedBy>
<Email>YOURMAIL@CAFE.COM</Email>
<AdminPassword>admin1</AdminPassword>
<SalesDailyAmount>150.0000</SalesDailyAmount>
<PurchasesDailyAmount>0.0000</PurchasesDailyAmount>
</Table>
</Result>
2012-09-19 ->Having 2 tables(i.e Table1,Table2)
2013-09-20 ->Having 3 tables(i.e Table3,Table4,Table5)
2013-09-21 ->Having 2 tables(i.e Table6,Table7)
现在我的问题是在上面的xml数据中我在该table1中共有7个表,table2数据属于2012-09-19 date.I想要解析数据并在下面显示如下所示
CreateBy SalesDailyAmount PurchasesDailyAmount
2012-09-19 ->Want to display 1 100 0.0000
使用以下代码
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
if( [elementName isEqualToString:@"CreateBy"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
xmlResults = YES;
}
if( [elementName isEqualToString:@"SalesDailyAmount"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
xmlResults = YES;
}
if( [elementName isEqualToString:@"PurchasesDailyAmount"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
xmlResults = YES;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if( xmlResults )
{
[soapResults appendString: string];
NSLog(@"soap result %@",soapResults);
[arr_detail addObject:soapResults];
NSLog(@"gg %@",arr_detail);
NSLog(@"gcvxg %@",arr_info);
}
[tableView reloadData];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if( [elementName isEqualToString:@"CreateBy"])
{
xmlResults = FALSE;
[aslam4 addObject:soapResults];
NSLog(@"hello %@",aslam4);
soapResults = nil;
}
if( [elementName isEqualToString:@"SalesDailyAmount"])
{
xmlResults = FALSE;
[aslam5 addObject:soapResults];
NSLog(@"hello %@",aslam5);
soapResults = nil;
}
if( [elementName isEqualToString:@"PurchasesDailyAmount"])
{
xmlResults = FALSE;
[aslam6 addObject:soapResults];
NSLog(@"hello %@",aslam6);
soapResults = nil;
}
}
}
使用上面的代码我可以获得如下所示的数据
OutPut
CreateBy SalesDailyAmount PurchasesDailyAmount
1 100 0.0000
1 100 0.0000
2 200 0.0000
2 200 0.0000
2 200 0.0000
3 150 0.0000
3 150 0.0000
但我想在下面显示如下
CreateBy SalesDailyAmount PurchasesDailyAmount
2012-09-19 ->Want to display 1 100 0.0000
2012-09-20 ->Want to display 2 200 0.0000
2012-09-21 ->Want to display 3 150 0.0000
我不想显示多个时间。在2012-09-19有2个表但两个都有相同的数据,但我不想多次显示。 同样2012-09-20在这里有3个表我也不想显示3次我想在一行中的3个表数据中显示。 总的来说我想说 现在在数组中它的存储数据如下所示 数组= {100,100,200,200,200,150,150},但我不想这样我想存储像这样的数组= {100,200,150} 请告诉我如何为此编写逻辑。
答案 0 :(得分:1)
为了从数组中获取唯一值,请使用以下方法。
// Your array containing all values
NSArray *dataListArray = [[NSArray alloc] initWithObjects:@"100", @"100", @"200", @"200", @"200", @"300", @"300", @"300", nil];
// Array containing unique data.
NSArray *uniqueDataArray = [[NSSet setWithArray:dataListArray] allObjects];
答案 1 :(得分:0)
根据您的要求,我认为 happle 是解决您问题的好方法。要使用 happle 类,您必须从github下载。重要的部分是xpath的概念。 所以一定要使用xpath。
检索日期
NSString *cafeDateTime = @"//table";
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
cafeDateTime = [element objectForKey:@"cafeDateTime"];
}
等等......
如需了解更多信息,请访问此网站http://www.raywenderlich.com/14172/how-to-parse-html-on-ios
答案 2 :(得分:0)
我遵循解析xml甚至复杂xml数据的简单方法:
传递xml数据或xml文件路径,将xml数据转换为NSdata。
NSString * path = [[NSBundle mainBundle] pathForResource:@“xmlExample”ofType:@“xml”];
NSData * xmlData = [NSData dataWithContentsOfFile:path];
使用库函数获取正确的数据,分别标记为NSDictionary。
NSDictionary * dict = [XMLReader dictionaryForXMLData:xmldata 选项:XMLReaderOptionsProcessNamespaces 误差:&安培;错误];
现在使用NSDictionary为你工作
NSArray * arr = [dict valueforkey:@“xtag.ytag.z.tag”];
你将获得ztag下的数组数组/数组。
NSArray * arr = [dict valueforkey:@“xtag.ytag”];
你将获得ytag下的数组数组/数组。
在很多项目中,它对我很有用,问题是“逻辑中的运行时错误”如果存在单行或数组的数组,则键的NSDictionary值可能会返回数组。
我使用特殊处理来解决这个问题。
总体而言,只需15分钟就可以顺利地读取xml数据,甚至是复杂的嵌套xml数据。
此外,如果想要编写XML数据,我使用XMLWriter