Simple Java/XML question

Discussion in 'XML' started by Stevey, Apr 16, 2005.

  1. Stevey

    Stevey Guest

    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.
     
    Stevey, Apr 16, 2005
    #1
    1. Advertising

  2. Stevey

    kjc Guest

    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().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
     
    kjc, Apr 17, 2005
    #2
    1. Advertising

  3. Stevey

    john martin Guest

    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().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


    i'll second that, JDOM's great.
     
    john martin, Apr 17, 2005
    #3
  4. 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/
     
    Martin Honnen, Apr 17, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    565
  2. Stevey

    Simple Java/XML question

    Stevey, Apr 16, 2005, in forum: Java
    Replies:
    3
    Views:
    496
    john martin
    Apr 17, 2005
  3. mathieu
    Replies:
    3
    Views:
    526
    mathieu
    Jan 6, 2007
  4. Replies:
    1
    Views:
    407
    Joseph Kesselman
    Jun 7, 2007
  5. Erik Wasser
    Replies:
    5
    Views:
    468
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page