我是iPhone开发的新手。我想像这样解析XML文件以获取属性值。
<Root>
<element att_1 ="value1" att_2 ="value2" />
<element att_1 ="value1" att_2 ="value2" />
.
.
.
<Root/>
通常是XML文件
<element att="..."> content <element/>
我在didEndElement上使用它
if([elementName isEqualToString:@"element"]){
// Do something
}
如何查看空标签?请告诉我。 谢谢。
抱歉我的英语不好。=============================================== ======================
现在我遇到了新问题。当我运行应用程序(模拟器)时它会卡住并在didStartElement中显示消息“Thread1:Stopped at breakpoint”。我想我做错了。
StudyList.xml
<StudyList>
<study description="20040311181130" modsInStudy="" pat_birth="19760822" pat_id="47-3450" pat_name="Ekachai Chaichanasiri" refPhy="" study_date="Sun Jan 11 00:03:00 ICT 2004" study_id="696122224" study_iuid="1.2.392.200036.9123.100.200.20040311181130"/>
<study description="XoranCAT Study" modsInStudy="" pat_birth="19821117" pat_id="63" pat_name="Wantana Areeprayolkij" refPhy="" study_date="Fri Jan 28 00:04:00 ICT 2005" study_id="" study_iuid="1.2.826.0.1.3680043.2.594.4041.16900.9840.32723.30648"/>
</StudyList>
代表
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"StudyList"]) {
//Initialize the array.
appDelegate.studyList = [[NSMutableArray alloc] init];
} else if ([elementName isEqualToString:@"study"]) {
//Initialize the study.
aStudy.patientBrith = [attributeDict objectForKey:@"pat_birth"] ;
aStudy.patientID = [attributeDict objectForKey:@"pat_id"];
aStudy.patientName = [attributeDict objectForKey:@"pat_name"];
aStudy.studyDate = [attributeDict objectForKey:@"study_date"];
NSLog(@"Patient name: %@",aStudy.patientName);
NSLog(@"Patient ID: %@",aStudy.patientID);
NSLog(@"Patient Birth: %@" ,aStudy.patientBrith);
NSLog(@"Study Date: %@" ,aStudy.studyDate);
}
NSLog(@"Process Element");
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//Nothing to do because XML file dose not have text node
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//End if XML file nothing to do.
if ([elementName isEqualToString:@"StudyList"]) {
return;
//Add study object in to array and release the object.
if ([elementName isEqualToString:@""]||elementName == nil) {
[appDelegate.studyList addObject:aStudy];
}
}
}
NSLog(@“患者姓名:%@”,aStudy.patientName);
NSLog(@“患者ID:%@”,aStudy.patientID);
NSLog(@“患者出生:%@”,aStudy.patientBrith);
NSLog(@“研究日期:%@”,aStudy.studyDate);
***上面的输出为空。 怎么了,请以正确的方式告诉我。
答案 0 :(得分:0)
在此函数中保留字符串的值
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
检查
if ([retainedString isEqualToString:@"" ] || [retainString == nil)
然后该值为空。