I created a Java application which opens an xml file that looks something like this:
<AnimalTree>
<animal>
<mammal>canine</mammal>
<color>blue</color>
</animal>
<!-- ... -->
</AnimalTree>
And I can open it using:
File fXmlFile = getResources.getXml("res/xml/data.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList animalNodes = doc.getElementsByTagName("animal");
Then I can simply create a node, push the object into a ListArray, then do what I want with the objects as I loop through the ListArray.
for (int temp = 0; temp < animalNodes.getLength(); temp++) {
Node nNode = animalNodes.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
question thisAnimal = new animal();
thisAnimal.mammal = getTagValue("mammal",eElement);
// ...
Plain and simple! Now only, in Android I cannot simply read the file "res/xml/data.xml
" because "File();
" requires a String
not an integer
(id). This is where I am lost. Is there some way I can make "File();
" open the file, or is this impossible without using SAXparser
or XPP
? (both of which I really cannot understand, no matter how hard I try.)
If I am forced to use those methods, can someone show me some simple code analogous to my example?