iphonexmlxml-parsingnsxmlparser

XML Parse only certain child elements


This may be a basic question but, I have a play that is in xml format. I want to grab the speaker and the lines the speaker has in a dictionary in order to add it to an array. Here is the format

 <SPEECH>
    <SPEAKER>Narrator</SPEAKER>
    <LINE>Two households, both alike in dignity,</LINE>
    <LINE>In fair Verona, where we lay our scene,</LINE>
    <LINE>From ancient grudge break to new mutiny,</LINE>
    <LINE>Where civil blood makes civil hands unclean.</LINE>
    <LINE>From forth the fatal loins of these two foes</LINE>
    <LINE>A pair of star-cross'd lovers take their life;</LINE>
    <LINE>Whole misadventured piteous overthrows</LINE>
    <LINE>Do with their death bury their parents' strife.</LINE>
    <LINE>The fearful passage of their death-mark'd love,</LINE>
    <LINE>And the continuance of their parents' rage,</LINE>
    <LINE>Which, but their children's end, nought could remove,</LINE>
    <LINE>Is now the two hours' traffic of our stage;</LINE>
    <LINE>The which if you with patient ears attend,</LINE>
    <LINE>What here shall miss, our toil shall strive to mend.</LINE>
</SPEECH>

So I want to grab the Narrator as the speaker and the lines he/she has and add it to the dictionary. After that I want to add the dictionary to the array and then clear the dictionary.

How can I do this?

Thanks


Solution

  • I'm inferring from one of the original tags in your question, , that you're doing in this in Objective-C. I'll further assume that you wanted to use NSXMLParser.

    So, let's assume you have (a) a mutable array of speeches; (b) a mutable dictionary for the current speech; (c) a mutable array of lines for each speech; and (d) a mutable string, value, which will capture the characters found between the start of an element name and the end of that element name.

    Then, you have to implement the NSXMLParserDelegate methods. For example, as you're parsing, in your didStartElement, if you encounter a speech element name, you create a dictionary:

    if ([elementName isEqualToString:@"SPEECH"]) {
        speech = [[NSMutableDictionary alloc] init];
        lines  = [[NSMutableArray alloc] init];
    }
    else 
    {
        value = [[NSMutableString alloc] init];
    }
    

    As you encounter characters in foundCharacters, you'd append these to the value:

    [value appendString:string];
    

    And, in your didEndElement, if you encounter speaker, set it, if you encounter a line, add it, and if you encounter the SPEECH closing tag, go ahead and add the speech (with its SPEAKER and LINES to your array of speeches:

    if ([elementName isEqualToString:@"SPEAKER"]) {
        [speech setObject:value forKey:@"SPEAKER"];
    }
    else if ([elementName isEqualToString:@"LINE"]) {
        [lines addObject:value];
    }
    else if ([elementName isEqualToString:@"SPEECH"]) {
        [speech setObject:lines forKey:@"LINES"];
        [speeches addObject:speech];
        speech = nil;
        lines = nil;
    }
    value = nil;
    

    For more information, see the Event-Driven XML Programming Guide or google "NSXMLParser tutorial".