S
Stevey
I have the following XML file...
<?xml version="1.0"?>
<animals>
<animal>
<name>Tiger</name>
<questions>
<question index="0">true</question>
<question index="1">true</question>
</questions>
</animal>
<animal>
<name>Leopard</name>
<questions>
<question index="0">true</question>
<question index="1">false</question>
</questions>
</animal>
</animals>
.... and I have the following java file which reads this in...
private static void readAnimals() {
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("animals.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfAnimals = doc.getElementsByTagName("animal");
int totalPersons = listOfAnimals.getLength();
System.out.println("Total no of animals : " + totalPersons);
for(int s=0; s<listOfAnimals.getLength() ; s++){
Node firstAnimalNode = listOfAnimals.item(s);
if(firstAnimalNode.getNodeType() == Node.ELEMENT_NODE){
Element firstAnimalElement = (Element)firstAnimalNode;
//-------
NodeList nameList = firstAnimalElement.getElementsByTagName("name");
Element nameElement = (Element)nameList.item(0);
NodeList textNList = nameElement.getChildNodes();
System.out.println("Name : " +
((Node)textNList.item(0)).getNodeValue().trim());
Animal animal = new
Animal(((Node)textNList.item(0)).getNodeValue().trim());
//THE FOLLOWING IS THE PART IM UNSURE OF...
// I'M TRYING TO READ IN 'question' ELEMENTS
NodeList questionsList =
firstAnimalElement.getElementsByTagName("questions");
Element questionElement = (Element)questionsList.item(0);
NodeList listOfQuestions = questionElement.getChildNodes();
int totalQuestions = listOfQuestions.getLength();
System.out.println("Total no of questions : " + totalQuestions);
for(int i=0; i<listOfQuestions.getLength() ; i++){
Node questionTempNode = listOfQuestions.item(s);
System.out.println("Name : " +
((Node)textNList.item(0)).getNodeValue().trim());
}
}//end of if clause
}
I've marked the part of the code I'm unsure of. Basically I can get it to
create a new Animal object for each animal it encounters from the XML file,
but I then want to loop round each "question" in the XML file and use the
true/false values from that. How do I do that?
Also, how do I access the index attribute from each "question"?
Sorry for the basic question but tonight is the first time I've used XML
with Java.
<?xml version="1.0"?>
<animals>
<animal>
<name>Tiger</name>
<questions>
<question index="0">true</question>
<question index="1">true</question>
</questions>
</animal>
<animal>
<name>Leopard</name>
<questions>
<question index="0">true</question>
<question index="1">false</question>
</questions>
</animal>
</animals>
.... and I have the following java file which reads this in...
private static void readAnimals() {
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("animals.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfAnimals = doc.getElementsByTagName("animal");
int totalPersons = listOfAnimals.getLength();
System.out.println("Total no of animals : " + totalPersons);
for(int s=0; s<listOfAnimals.getLength() ; s++){
Node firstAnimalNode = listOfAnimals.item(s);
if(firstAnimalNode.getNodeType() == Node.ELEMENT_NODE){
Element firstAnimalElement = (Element)firstAnimalNode;
//-------
NodeList nameList = firstAnimalElement.getElementsByTagName("name");
Element nameElement = (Element)nameList.item(0);
NodeList textNList = nameElement.getChildNodes();
System.out.println("Name : " +
((Node)textNList.item(0)).getNodeValue().trim());
Animal animal = new
Animal(((Node)textNList.item(0)).getNodeValue().trim());
//THE FOLLOWING IS THE PART IM UNSURE OF...
// I'M TRYING TO READ IN 'question' ELEMENTS
NodeList questionsList =
firstAnimalElement.getElementsByTagName("questions");
Element questionElement = (Element)questionsList.item(0);
NodeList listOfQuestions = questionElement.getChildNodes();
int totalQuestions = listOfQuestions.getLength();
System.out.println("Total no of questions : " + totalQuestions);
for(int i=0; i<listOfQuestions.getLength() ; i++){
Node questionTempNode = listOfQuestions.item(s);
System.out.println("Name : " +
((Node)textNList.item(0)).getNodeValue().trim());
}
}//end of if clause
}
I've marked the part of the code I'm unsure of. Basically I can get it to
create a new Animal object for each animal it encounters from the XML file,
but I then want to loop round each "question" in the XML file and use the
true/false values from that. How do I do that?
Also, how do I access the index attribute from each "question"?
Sorry for the basic question but tonight is the first time I've used XML
with Java.