XML Parsing Only Specific Child Elements

This may be the main question, but I have a game that is in xml format. I want to capture the speaker and the lines that the speaker has in the dictionary to add it to the 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 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> 

Therefore, I want to capture Narrator as a speaker and the lines that he / she has and add to the dictionary. After that, I want to add the dictionary to the array and then delete the dictionary.

How can i do this?

thanks

0
source share
2 answers

I exit one of the source tags in your question, .

So, suppose you have (a) a mutable array of speeches ; (b) a mutable dictionary for the current speech ; (c) a variable array of lines for each speech; and (d) a mutable value string that will capture characters found between the beginning of the element name and the end of the name of this element.

Then you must implement the NSXMLParserDelegate methods. For example, when analyzing in didStartElement , if you come across the name of a speech element, you create a dictionary:

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

When you come across characters in foundCharacters , you add them to value :

 [value appendString:string]; 

And in didEndElement , if you came across a speaker, set it, if you came across a line, add it, and if you came across a speech closing tag, add speech (with its SPEAKER and lines to your speech array:

 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-Based Programming Guide or google NSXMLParser Tutorial.

+2
source

If you use C #, and if each SPEECH has only 1 SPEAKER , you can do the following

 XDocument xdoc = XDocument.Load("XMLFile1.xml"); List<string> lines = xdoc.Descendants("SPEECH").Where(e => e.Element("SPEAKER").Value.ToUpper() == "NARRATOR").Elements("LINE").Select(e => e.Value).ToList(); 
0
source

Source: https://habr.com/ru/post/952497/


All Articles