the firsttag state will switch to childnodewhich is responsible for deciding which of the other three states to switch towhen those states are finishedthey'll switch back to childnode the following state-transition diagram shows the available state changesopentag firsttag childnode text closetag the states are responsible for taking "what' left of the string"processing as much of it as they know what to do withand then telling the parser to take care of the rest of it let' construct the parser class firstclass parserdef __init__(selfparse_string)self parse_string parse_string self root none self current_node none self state firsttag(def process(selfremaining_string)remaining self state process(remaining_stringselfif remainingself process(remainingdef start(self)self process(self parse_stringthe initializer sets up few variables on the class that the individual states will access the parse_string instance variable is the text that we are trying to parse the root node is the "topnode in the xml structure the current_node instance variable is the one that we are currently adding children to