2
<scxml 
xmlns="http://www.w3.org/2005/07/scxml"
version="1.0"
profile="ecmascript"
initial="idle">

<state id="idle">
    <transition event="mousedown" target="dragging"/>
</state>

<state id="dragging">
    <transition event="mouseup" target="idle"/>
    <transition event="mousemove" target="dragging"/>
</state>

</scxml>

I want to parse this xml when i start the application i enter the initial state that is "idle" then when my event completed i want to move the next state "dragging". how can i do this?

I am using NSXmlparser in the following method.

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"scxml"])

{
    stateID = [attributeDict objectForKey:@"initital"];
    stateInfo = [[StateInfo alloc]initWithStateName:stateID];
    return;

}
if ([elementName isEqualToString: @"state"] ){

    if ([[attributeDict objectForKey:@"transition"] isEqualToString:@"idle"])  {

    }
    else {

    }
}
//    if ([elementName isEqualToString:@"state"]) {
////        stateID = [attributeDict objectForKey:@"id"];
////        stateInfo = [[StateInfo alloc]initWithStateName:stateID];
////        NSLog(@"stateID = %@",stateID);
//        return;
//    }
if([elementName isEqualToString:@"transition"]){
    //aList=[[List alloc]init];
    stateID = [attributeDict objectForKey:@"target"];
    targetStateInfo = [[StateInfo alloc]initWithStateName:stateID];
    [stateInfo registerNextState:targetStateInfo withCondition:[attributeDict objectForKey:@"cond" ]];
    return;
}
if ([elementName isEqualToString:@"scxml"])

    {
    stateID = [attributeDict objectForKey:@"initital"];
    stateInfo = [[StateInfo alloc]initWithStateName:stateID];
    return;

  }
if ([elementName isEqualToString: @"state"] ){

    if ([[attributeDict objectForKey:@"Name"] isEqualToString:@"Child2"])  {
        // searchDone=YES;
    }
    else {
        // searchDone=NO;

    }
}
//    if ([elementName isEqualToString:@"state"]) {
////        stateID = [attributeDict objectForKey:@"id"];
////        stateInfo = [[StateInfo alloc]initWithStateName:stateID];
////        NSLog(@"stateID = %@",stateID);
//        return;
//    }
if([elementName isEqualToString:@"transition"]){
    //aList=[[List alloc]init];
    stateID = [attributeDict objectForKey:@"target"];
    targetStateInfo = [[StateInfo alloc]initWithStateName:stateID];
    [stateInfo registerNextState:targetStateInfo withCondition:[attributeDict objectForKey:@"cond" ]];
    return;
  }

}

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

if(!currentElementValue){
    currentElementValue=[[NSMutableString alloc]init];
}
[currentElementValue appendString:string];
//currentElementValue = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 /* if(!currentElementValue)
 currentElementValue = [[NSMutableString alloc] init];
 else
 [currentElementValue appendString:string];
 */

/* if(self.currentElementValue) { [self.currentElementValue appendString:string]; } */ }

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"scxml"])
    return;
if([elementName isEqualToString:@"state"]){
    //[aList setTitle:currentElementValue];
    //NSLog(@"alist.title = %@",aList.title);
}
    currentElementValue = nil;
 }
4

1 回答 1

0

您评论的这段代码似乎很好:

if ([elementName isEqualToString:@"state"]) {
        stateID = [attributeDict objectForKey:@"id"];
        stateInfo = [[StateInfo alloc]initWithStateName:stateID];
        NSLog(@"stateID = %@",stateID);
}

stateID 第一次显示空闲,第二次显示拖动。

xml从上到下读取

<state>您必须使用的或类似的标签if ([elementName isEqualToString:@"state"])

对于诸如 target=""、event="" 或 id="" 等标签内的属性,您必须使用[attributeDict objectForKey:@"target"];

您不必做任何特别的事情,当解析器完成第一个</state>时,它将继续下一个<state>

于 2012-10-08T07:23:38.727 回答