473,507 Members | 13,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4996
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
3187
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
2011
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
1937
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
3472
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
1529
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
2394
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
5788
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
7042
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
1745
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...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.