I can't seem to get my code to work. What am I missing? I want to be able to call the xml information and put it into a div I created in an html page. I was able to get one of them to show up with similar code to below, but now I want to create it so I can click on the next button and it goes to the next image and description.
When using Chrome, I get an error that it is undefined. I can't figure out why.
THIS IS THE JAVASCRIPT
// JavaScript Document
//used W3Schools for code resources
window.onload = function ShowPictures(){ //start of ShowPictures function
//test for browser type
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
}
xmlhttp.open("GET","filename.animal.xml",false); //gets my xml document
//removed this because it was getting caught up xmlhttp.send();
xmlDoc=xmlhttp.responseXML
firstImage() //shows the first image function which starts below
} //end of ShowPictures function
function firstImage() {
i = 0;
var AnimalsRoar = xmlDoc.getElementsByTagName("WILD")[0];
var Imagepicturet = AnimalsRoar[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue;
var Commonnamet = AnimalsRoar[i].getElementsByTagName("COMMONNAME")[0].firstChild.data;
var Scientificnamet = AnimalsRoar[i].getElementsByTagName("SCIENTIFICNAME")[0].firstChild.data;
var Diett = AnimalsRoar[i].getElementsByTagName("DIET")[0].firstChild.data;
var Habitat = AnimalsRoar[i].getElementsByTagName("HABITAT")[0].firstChild.data;
var Factst = AnimalsRoar[i].getElementsByTagName("FACTS")[0].firstChild.data;
//the first words in parenthesis tells which div I am putting it in - the name after the equal sign is the var name and the var name grabs it from the xml document by the tag name
document.getElementById("imagehereInfo").innerHTML = Imagepicturet;
document.getElementById("CommonNameInfo").innerHTML = Commonnamet;
document.getElementById("ScientificNameInfo").innerHTML = Scientificnamet;
document.getElementById("DietInfo").innerHTML = Diett;
document.getElementById("HabitatInfo").innerHTML = Habitat;
document.getElementById("FactsInfo").innerHTML = Factst;
}
function nextImage() {
i++;
if(i<10) {
var AnimalsRoar = xmlDocument.getElementsByTagName("WILD")[0];
var Imagepicturet = AnimalsRoar[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue;
var Commonnamet = AnimalsRoar[i].getElementsByTagName("COMMONNAME")[0].firstChild.data;
var Scientificnamet = AnimalsRoar[i].getElementsByTagName("SCIENTIFICNAME")[0].firstChild.data;
var Diett = AnimalsRoar[i].getElementsByTagName("DIET")[0].firstChild.data;
var Habitat = AnimalsRoar[i].getElementsByTagName("HABITAT")[0].firstChild.data;
var Factst = AnimalsRoar[i].getElementsByTagName("FACTS")[0].firstChild.data;
//the first words in parenthesis tells which div I am putting it in - the name after the equal sign is the var name and the var name grabs it from the xml document by the tag name
document.getElementById("imagehereInfo").innerHTML = Imagepicturet;
document.getElementById("CommonNameInfo").innerHTML = Commonnamet;
document.getElementById("ScientificNameInfo").innerHTML = Scientificnamet;
document.getElementById("DietInfo").innerHTML = Diett;
document.getElementById("HabitatInfo").innerHTML = Habitat;
document.getElementById("FactsInfo").innerHTML = Factst;
}
else
{
firstImage(); //by adding this in, it will go to the first image if it's cycled through all 10
}
}
THIS IS THE XML FILE INFORMATION
<?xml version="1.0" encoding="utf-8"?>
<ANIMALGALLERY>
<WILD>
<IMAGE>fruitbat.jpg</IMAGE>
<COMMONNAME>Giant Fruit Bat or Flying Fox</COMMONNAME>
<SCIENTIFICNAME>Pteropus scapulatus</SCIENTIFICNAME>
<DIET>Fruit</DIET>
<HABITAT>Southern and Southeast Asia</HABITAT>
<FACTS>The giant fruit bat has a wing span of up to 5 feet (1.5m). It is the largest bat in the world. It has a very keen sense of smell and a very long muzzle. They roost upside downin the trees and wrap their wings around their bodies. Feeding time is at dusk when they set off to eat fruit. They squish fruit in their jaws and drink the juice but let the seeds and the rest of the fruit fall to the ground. The females give birth to one baby at a time. They will carry their baby around with them for two months.</FACTS>
</WILD>
<WILD>
<IMAGE>africanelephant.jpg</IMAGE>
<COMMONNAME> African Elephant </COMMONNAME>
<SCIENTIFICNAME> Loxodonta africana </SCIENTIFICNAME>
<DIET> Leaves, branches and fruit </DIET>
<HABITAT> Africa, south of the Sahara Desert </HABITAT>
<FACTS> The African Elephant is the world's largest land animal. Adult males can weigh as much as six tons. They drink more than 25 gallons (100 liters) of water each day and eat up to 650 lbs. (300kg) of food. The trunks of African elephants have two tips which they use like fingertips to pick things up. African elephant calves feed on milk for up to 18 months, and do not breed until at least 11 years old.
</FACTS>
</WILD>
<WILD>
<IMAGE>whiterhinoceros.jpg</IMAGE>
<COMMONNAME> White Rhinoceros </COMMONNAME>
<SCIENTIFICNAME> Ceratotherium simum </SCIENTIFICNAME>
<DIET> Grass </DIET>
<HABITAT> Africa </HABITAT>
<FACTS> White rhinoceroses are very large. In fact, they are the second largest land animal after elephants and can weigh as much as 3.5 tons. The DIET of the White Rhino is mainly made up of grass, which they find growing in large open plains. These magnificent animals have poor eyesight but exceptional hearing and a keen sense of smell. Scientists estimate that there are currently less than 7,000 white rhinos left in the WILD.
FACTS>
</WILD>
<WILD>
<IMAGE>meerkat.jpg</IMAGE>
<COMMONNAME> Meerkat </COMMONNAME>
<SCIENTIFICNAME> Suricata suricatta </SCIENTIFICNAME>
<DIET> Insects and birds </DIET>
<HABITAT> Southern Africa </HABITAT>
<FACTS> Meerkats feed together during the day in groups of 20-30 individuals in dry open country. This makes them a target for predators. While some members of a meerkat group look for insects, others act as lookouts popping up on their hind legs and tails. Living in large groups helps meerkats to survive. They shelter in underground burrows.</FACTS>
</WILD>
<WILD>
<IMAGE>koala.jpg</IMAGE>
<COMMONNAME> Koala </COMMONNAME>
<SCIENTIFICNAME> Phascolarctos cinereus </SCIENTIFICNAME>
<DIET> Eucalyptus leaves </DIET>
<HABITAT> Eastern Australia </HABITAT>
<FACTS> Koalas are very popular animals. Often called koala bears, koalas are actually not bears at all but marsupials. They have sharp claws that allow them to climb trees to find their food. Koalas only eat eucalyptus leaves. They never need to drink water because eucalyptus leaves provide all the water they need. Female koalas have one baby at a time. The female's pouch open near the rear of her body.</FACTS>
</WILD>
<WILD>
<IMAGE>alpaca.jpg</IMAGE>
<COMMONNAME> Alpaca </COMMONNAME>
<SCIENTIFICNAME> Vicugna pacos </SCIENTIFICNAME>
<DIET> Grass </DIET>
<HABITAT> South America </HABITAT>
<FACTS> Alpacas are a domesticated descendant of guanacos. Instead of working as a pack animal, alpacas are bred and raised for their high quality wool, which is used to make yarn and other products. Some alpacas' wool grows so long that it will almost touch the ground. Alpacas feed on grass, unlike its other South American relatives which feed on bushes.
</FACTS>
</WILD>
<WILD>
<IMAGE>platypus.jpg</IMAGE>
<COMMONNAME> Platypus </COMMONNAME>
<SCIENTIFICNAME> Ornithorhynchus anatinus </SCIENTIFICNAME>
<DIET> Insects and crustaceans </DIET>
<HABITAT> Eastern Australia and Tasmania, Eastern Australia </HABITAT>
<FACTS> The platypus is one of only three species of mammals that lay eggs. It has a fur-covered body, webbed feet, and a wide beak. It looks sort of like a beaver with a bird's beak. Platypuses find their food at the bottom of the lakes and rivers where they live. They use their sensitive beak to feel for and find food. Male platypuses have a defense against predators: Their hind legs have poisonous spurs, which can leave a painful wound.
</FACTS>
</WILD>
<WILD>
<IMAGE>greaterroadrunner.jpg</IMAGE>
<COMMONNAME> Greater Roadrunner </COMMONNAME>
<SCIENTIFICNAME> Geococcyx californianus </SCIENTIFICNAME>
<DIET> Lizards and snakes, insects, scorpions to small rodents and small birds, hummingbirds, some fruit </DIET>
<HABITAT> Southwestern United States and central Mexico </HABITAT>
<FACTS> The Greater Roadrunner is one of the few animals that will attack a rattlesnake. To kill this snake, the roadrunner grabs the snake just behind the head, then it hits it against the ground or rocks. Roadrunners live in pairs, defending their area all year.
</FACTS>
</WILD>
<WILD>
<IMAGE>gorilla.jpg</IMAGE>
<COMMONNAME> Western Gorilla </COMMONNAME>
<SCIENTIFICNAME> Gorilla Gorilla </SCIENTIFICNAME>
<DIET> Leaves </DIET>
<HABITAT> Western and Central Africa </HABITAT>
<FACTS> Gorillas are the largest of the great apes. Great apes can stand on their hind legs. They are also very good with their hands and can use wooden sticks like tools. Gorillas live in groups of up to 20 animals. They eat in the trees and on the ground.
</FACTS>
</WILD>
</ANIMALGALLERY>
HTML PAGE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Animal Gallery</title>
<link rel="stylesheet" type="text/css" href="filename.animal.css" /> <!--links to my external css file-->
<script type="text/javascript" src="filename.animal.js"> </script> <!--links to my external javascript file-->
<table border="0" style="border:1px solid #000; width:600px; margin:auto; text-align:center; font-family:Arial, Helvetica, sans-serif;" align="center">
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td>Ms.Mumford's Gallery</td>
</tr>
<tr>
<td><div id="imagehereInfo"></div>
</td>
</tr>
<tr>
<td><div id="CommonNameInfo"></div>
</td>
</tr>
<tr>
<td><div id="ScientificNameInfo"></div>
</td>
</tr>
<tr>
<td><div id="DietInfo"></div>
</td>
</tr>
<tr>
<td><div id="HabitatInfo"></div>
</td>
</tr>
<tr>
<td><div id="FactsInfo"></div>
</td>
</tr>
<tr>
<tr>
<td> </td>
</tr>
<td style="background-color:#000"><input type="button" id="nextBtn" value="Forward" onclick="nextImage();" /></td>
</tr>
</table>
</body>
</html>