Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Simple Java/XML question

Reply
Thread Tools

Simple Java/XML question

 
 
Stevey
Guest
Posts: n/a
 
      04-16-2005
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.


 
Reply With Quote
 
 
 
 
kjc
Guest
Posts: n/a
 
      04-16-2005
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
 
Reply With Quote
 
 
 
 
john martin
Guest
Posts: n/a
 
      04-17-2005
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.
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      04-17-2005


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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments