472,782 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Simple Java/XML question

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().tr im());

//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.
Jul 20 '05 #1
3 4856
kjc
Stevey wrote:
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().tr im());

//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
Jul 20 '05 #2
kjc wrote:
Stevey wrote:
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().tr im());

//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


i'll second that, JDOM's great.
Jul 20 '05 #3


Stevey wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: martin | last post by:
Hello, I just got the SUN Java IDE. (Netbeans IDE 3.5.1) Very very nice, and I worked myself through the tutorial (about making a colorswitch). Now, When I compile it gives no errors at all. So...
13
by: aum | last post by:
Hi, I'm a Python programmer, just starting to get into javascript. On reading some of the js guides, and not liking any of the OO usage patterns I saw, I've cooked up something which python...
7
by: James Harris | last post by:
My Googling has let me down here - maybe because when it comes to Java I don't really know which type I need! If someone could point me in the right direction I'd appreciate it. My requirement...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
11
by: Olaf Krumnow | last post by:
Hi, I have a requirement that, I thought, was very simple, but in fact turned out as impossible for me. I read some posts here and the language specs from MS, but couldn't get a solution. ...
1
by: epilogue | last post by:
Hey guys Im pretty new to Java and while I am finding it enjoyable i am getting several errors!!! Do you think you could help me on this particular question of an Assignment im doing. I have to...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
5
by: sayeo87 | last post by:
Hi, I am quite new to JSP so please forgive me if I ask really simple things... I am trying to run system commands on the server and display the output on a webpage. This is what I've got: <%@...
5
by: Ibys | last post by:
Hi, i am just starting to learn javascript, so i am probably doing something very simple wrong. i have read a lot of articles on maths in java, but cant find anything simple enough for my problem. I...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.