Android - How to extract data between tags in xml

tmh32293

New Member
I have a res/xml/myxmlfile which looks something like this (sorry for the screenshot, I am unsure how to display the xml file properly in the stackoverflow editor):\[code\]<Food> <Pizza> <type>Salami</type> <type>Pepperoni</type> <type>Hawaiian</type> </Pizza> <Burger> <type>Chicken</type> <type>Bacon</type> <type>Cheese</type> </Burger> <Soup> <type>Pumpkin</type> <type>Sweet Corn</type> <type>Vegetarian</type> </Soup></Food>\[/code\]I want to write a function that takes the type of food as a parameter (e.g. Burger) and loads all the items between the tags into a string.So function would be something like this:\[code\]public string[] GetAllSubFoodTypes (string foodtype){ string[] contents; //--- pseudocode as I don't know how to do this Loadxmlfile Find the <foodtype> tag in file Load all data between <type> and </type> into the string[] contents return contents; }\[/code\]Example of how you'd call the function from main:\[code\]string[] subFoodType;subFoodType = GetAllSubFoodTypes("Burger")\[/code\]Contents of subFoodType will then be:\[code\]subFoodType[0]\[/code\] will be "Chicken", \[code\]subFoodType[1]\[/code\] will be "Bacon" and so on.
 
Back
Top