Simple Java/XML question

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.
 
K

kjc

Stevey said:
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.
Make your life easier and use JDOM http://www.jdom.org
 
M

Martin Honnen

Stevey said:
Also, how do I access the index attribute from each "question"?

If you have an Element object then you can call
elementObject.getAttribute("index")
to tet the attribute value as a string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top