2 votes

Comment vérifier la fin d'un élément avec une balise vide sur l'iPhone ?

Je veux analyser un fichier XML comme ceci pour obtenir la valeur d'un attribut.

   <Root>
         <element att_1 ="value1" att_2 ="value2" />
         <element att_1 ="value1" att_2 ="value2" />
                             .
                             .
                             .

   <Root/>

Normalement, fichier XML

   <element att="..."> content <element/>   

Je l'utilise à didEndElement

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

   // Do something

   }

Comment vérifier une balise vide ? Conseillez-moi s'il vous plaît. Je vous remercie.

Désolé pour mon mauvais anglais.

\=====================================================================

Maintenant j'ai un nouveau problème.quand je lance l'application(simulateur) elle se bloque et affiche le message "Thread1:Stopped at breakpoint" dans didStartElement.je suppose que j'ai fait quelque chose de mal.

Liste d'études.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>

Délégué

    -(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(@"Nom du patient : %@",aStudy.patientName) ;

NSLog(@"Patient ID : %@",aStudy.patientID) ;

NSLog(@"Naissance du patient : %@" ,aStudy.patientBrith) ;

NSLog(@"Date de l'étude : %@" ,aStudy.studyDate) ;

***La sortie de ce qui précède est nulle. Qu'est ce qui ne va pas, merci de m'indiquer la bonne marche à suivre.

0voto

Praveen S Points 7978

Conserver la valeur de la chaîne dans cette fonction

 - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

En - (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName Vérifier

if ([retainedString isEqualToString:@"" ] || [retainString == nil)

La valeur est alors vide.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X