/    Sign up×
Community /Pin to ProfileBookmark

Displaying XML data in Accordion panels

I want to extract data from an xml file, then display it in expandable and collapsable accordion panels. Getting an object expected error at line 111.

[CODE]

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>Displaying XML data using Accordion</title>
<style type=”text/css”>
body {
font-size: 12pt;
font-family: Arial, Tahoma, Verdana;
}

.accordionItem h2 {
color: white;
font-size: 16pt;
background-color: #0000AA;
border: 1px solid #00EE00;
}

.accordionItem div {
padding: 1em 1em;
background-color: #FFFFFF;
color: #000000;
}

.accordionItem.hide h2 { color: white;
background-color: #CC66FF;
}

accordionItem.hide div { display:none; }

.showIt {
font-size: 14pt;
color: green;
font-family: Arial, Tahoma, Verdana;
border: thick solid;
padding: 10px;

}

b { font-size: 16pt;
color:#000000;
}

</style>

<script type=”text/javascript” language=”javascript”>
<!–The object detection code –>
var req = false;
// Is there support for native XHR object?: IE7+, Firefox, Safari, Opera
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
//create an XMLHttpRequest object
}

else if (window.ActiveXObject)
//check for Version 6
{
req = new ActiveXObject(‘MSXML2.XMLHTTP.6.0’);
//create an ActiveX XMLHTTP component
}

if (!req)
{
req = new ActiveXObject(‘MSXML2.XMLHTTP’);
//fallback to version 3
}

function runIt()
{
if (req)
{
//Request data to be retrieved from the server

req.onreadystatechange = function()
{
if (req.readyState == 4 && req.status == 200)
{
var response = req.responseXML;
readXML(response);
}

}
req.open(“GET”, “MusicianList.xml”, true);
req.send(null);
}
}

function readXML(response)
{
var myResponse = response.documentElement;
var myMusician = myResponse.getElementsByTagName(“musician”);
var place1 = document.getElementById(“musicians”);
var place2 = document.getElementById(“genres”);
var place3 = document.getElementById(“hitsongs”);
for (var i=0; i < myMusician.length; i++)
{
place1.innerHTML += myMusician[i].getElementsByTagName(“name”)[0].firstChild.nodeValue + “<br>”;
place2.innerHTML += myMusician[i].getElementsByTagName(“genre”)[0].firstChild.nodeValue + “<br>”;
place3.innerHTML += myMusician[i].getElementsByTagName(“hitsong”)[0].firstChild.nodeValue + “<br>”;
}

//Assign onclick events to the accordion headings

var h2 = getElementsByTagName(“h2”);
h2.onclick = toggleItem;

//Hide all accordion item bodies except the first

var accordionItem = getElementById(“accordionItem”);
for ( var i=1; i < accordionItem.length; i++ ) {
accordionItem[i].className = ‘accordionItem hide’;
}

}

function toggleItem() {

var itemClass = this.parentNode.className;

//Hide all items

var accordionItem = getElementById(“accordionItem”);
for ( var i=0; i < accordionItem.length; i++ ) {
accordionItem[i].className = ‘accordionItem hide’;
}

//Show this item if it was previously hidden
if ( itemClass == ‘accordionItem hidden’) {
this.parentNode.className = ‘accordionItem hide’;
}

}

//–>
</script>
</head>
<body onload=”runIt()”>
<h1>Click on a Panel to get more information on the musician</h1>

<div class=”accordionItem” id=”accordionItem”>
<h2>Musicians</h2>
<div id=”musicians”> </div>
</div>

<div class=”accordionItem” id=”accordionItem”>
<h2>Genres</h2>
<div id=”genres”> </div>
</div>

<div class=”accordionItem” id=”accordionItem”>
<h2>Their Hitsongs</h2>
<div id=”hitsongs”> </div>
</div>

</body>
</html>

[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@animalj7Jun 30.2009 — Line 111:
[CODE]var h2 = getElementsByTagName("h2");[/CODE]

You aren't referencing an object whether the document or the xml from which to get the elements.

e.g.
[CODE]document.getElementsByTagName("h2"); [/CODE]
or
[CODE]myResponse.getElementsByTagName("h2"); [/CODE]
Copy linkTweet thisAlerts:
@MichiKenauthorJun 30.2009 — Made those corrections.

No errors now, however it's not doing what it's suppose to. When displayed in a web browser, the xml data appears under each of the headings like it's suppose to, but they are not expandable and collapsable, which was the whole purpose. My web browser is IE 6

[CODE]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Displaying XML data using Accordion</title>
<style type="text/css">
body {
font-size: 12pt;
font-family: Arial, Tahoma, Verdana;
}

.accordionItem h2 {
color: white;
font-size: 16pt;
background-color: #0000AA;
border: 1px solid #00EE00;
}

.accordionItem div {
padding: 1em 1em;
background-color: #FFFFFF;
color: #000000;
}

.accordionItem.hide h2 { color: white;
background-color: #CC66FF;
}

accordionItem.hide div { display:none; }




.showIt {
font-size: 14pt;
color: green;
font-family: Arial, Tahoma, Verdana;
border: thick solid;
padding: 10px;

}

b { font-size: 16pt;
color:#000000;
}

</style>

<script type="text/javascript" language="javascript">
<!--The object detection code -->
var req = false;
// Is there support for native XHR object?: IE7+, Firefox, Safari, Opera
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
//create an XMLHttpRequest object
}

else if (window.ActiveXObject)
//check for Version 6
{
req = new ActiveXObject('MSXML2.XMLHTTP.6.0');
//create an ActiveX XMLHTTP component
}

if (!req)
{
req = new ActiveXObject('MSXML2.XMLHTTP');
//fallback to version 3
}



function runIt()
{
if (req)
{
//Request data to be retrieved from the server

req.onreadystatechange = function()
{
if (req.readyState == 4 && req.status == 200)
{
var response = req.responseXML;
readXML(response);
}

}
req.open("GET", "MusicianList.xml", true);
req.send(null);
}
}


function readXML(response)
{
var myResponse = response.documentElement;
var myMusician = myResponse.getElementsByTagName("musician");
var place1 = document.getElementById("musicians");
var place2 = document.getElementById("genres");
var place3 = document.getElementById("hitsongs");
for (var i=0; i < myMusician.length; i++)
{
place1.innerHTML += myMusician[i].getElementsByTagName("name")[0].firstChild.nodeValue + "<br>";
place2.innerHTML += myMusician[i].getElementsByTagName("genre")[0].firstChild.nodeValue + "<br>";
place3.innerHTML += myMusician[i].getElementsByTagName("hitsong")[0].firstChild.nodeValue + "<br>";

}

//Assign onclick events to the accordion headings

var h2 = document.getElementsByTagName("h2");
h2.onclick = toggleItem;


//Hide all accordion item bodies except the first

var accordionItem = document.getElementById("accordionItem");
for ( var i=1; i < accordionItem.length; i++ ) {
accordionItem[i].className = 'accordionItem hide';
}

}

function toggleItem() {

var itemClass = this.parentNode.className;

//Hide all items

var accordionItem = document.getElementById("accordionItem");
for ( var i=0; i < accordionItem.length; i++ ) {
accordionItem[i].className = 'accordionItem hide';
}

//Show this item if it was previously hidden
if ( itemClass == 'accordionItem hidden') {
this.parentNode.className = 'accordionItem hide';
}

}


//-->
</script>
</head>
<body onload="runIt()">
<h1>Click on a Panel to get more information on the musician</h1>

<div class="accordionItem" id="accordionItem">
<h2>Musicians</h2>
<div id="musicians"> </div>
</div>

<div class="accordionItem" id="accordionItem">
<h2>Genres</h2>
<div id="genres"> </div>
</div>

<div class="accordionItem" id="accordionItem">
<h2>Their Hitsongs</h2>
<div id="hitsongs"> </div>
</div>




</body>
</html>

[/CODE]


XML file:

[CODE]

<?xml version="1.0" encoding="utf-8" ?>
- <musicians>
- <musician>
<name>Bruce Springsteen</name>
<genre>Rock</genre>
<hitsong>Born in the USA</hitsong>
</musician>
- <musician>
<name>B.B. King</name>
<genre>Blues</genre>
<hitsong>The Thrill Is Gone</hitsong>
</musician>
- <musician>
<name>Tim McGraw</name>
<genre>Country</genre>
<hitsong>Live Like You Were Dying</hitsong>
</musician>
- <musician>
<name>Gordon Lightfoot</name>
<genre>Folk</genre>
<hitsong>Carefree Highway</hitsong>
</musician>
- <musician>
<name>Glenn Miller</name>
<genre>Big Band</genre>
<hitsong>In The Mood</hitsong>
</musician>
</musicians>

[/CODE]
Copy linkTweet thisAlerts:
@MichiKenauthorJul 02.2009 — Anyone have some more feedback they could provide?
Copy linkTweet thisAlerts:
@MichiKenauthorJul 06.2009 — Made some modifications but still not working. Again, no error messages, but the panels don't expand and contract when clicking on the h2 headingss.

[CODE]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="prototype-1.6.0.3.js"></script>
<title>Displaying XML data using Accordion</title>
<style type="text/css">
body {
font-size: 12pt;
font-family: Arial, Tahoma, Verdana;
}

.accordionItem h2 {
color: white;
font-size: 16pt;
background-color: #0000AA;
border: 1px solid #00EE00;
}

.accordionItem div {
padding: 1em 1em;
background-color: #FFFFFF;
color: #000000;
}

.accordionItem.hide h2 { color: white;
background-color: #CC66FF;
}

accordionItem.hide div { display:none; }




.showIt {
font-size: 14pt;
color: green;
font-family: Arial, Tahoma, Verdana;
border: thick solid;
padding: 10px;

}

b { font-size: 16pt;
color:#000000;
}

</style>

<script type="text/javascript" language="javascript">
<!--The object detection code -->
var req = false;
// Is there support for native XHR object?: IE7+, Firefox, Safari, Opera
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
//create an XMLHttpRequest object
}

else if (window.ActiveXObject)
//check for Version 6
{
req = new ActiveXObject('MSXML2.XMLHTTP.6.0');
//create an ActiveX XMLHTTP component
}

if (!req)
{
req = new ActiveXObject('MSXML2.XMLHTTP');
//fallback to version 3
}



function runIt()
{
if (req)
{
//Request data to be retrieved from the server

req.onreadystatechange = function()
{
if (req.readyState == 4 && req.status == 200)
{
var response = req.responseXML;
readXML(response);
}

}
req.open("GET", "MusicianList.xml", true);
req.send(null);
}
}


function readXML(response)
{
var myResponse = response.documentElement;
var myMusician = myResponse.getElementsByTagName("musician");
var place1 = document.getElementById("musicians");
var place2 = document.getElementById("genres");
var place3 = document.getElementById("hitsongs");
for (var i=0; i < myMusician.length; i++)
{
place1.innerHTML += myMusician[i].getElementsByTagName("name")[0].firstChild.nodeValue + "<br>";
place2.innerHTML += myMusician[i].getElementsByTagName("genre")[0].firstChild.nodeValue + "<br>";
place3.innerHTML += myMusician[i].getElementsByTagName("hitsong")[0].firstChild.nodeValue + "<br>";

}

//Assign onclick events to the accordion headings

var h2 = document.getElementsByTagName("h2");
for (var i=0; i < h2[i].length; i++) {
h2[i].onclick = toggleItem;
}


//Hide all accordion item bodies except the first

var accordionItem = document.getElementById("accordionItem");
for ( var i=1; i < accordionItem.length; i++ ) {
accordionItem[i].className = 'accordionItem hide';
}

}

function toggleItem() {

var itemClass = this.parentNode.className;

//Hide all items

var accordionItem = document.getElementById("accordionItem");
for ( var i=0; i < accordionItem.length; i++ ) {
accordionItem[i].className = 'accordionItem hide';
}

//Show this item if it was previously hidden
if ( itemClass == 'accordionItem hidden') {
this.parentNode.className = 'accordionItem hide';
}

}


//-->
</script>
</head>
<body onload="runIt()">
<h1>Click on a Panel to get more information on the musician</h1>

<div id="accordionItem">
<div class="accordionItem">
<h2>Musicians</h2>
<div id="musicians"> </div>
</div>

<div class="accordionItem">
<h2>Genres</h2>
<div id="genres"> </div>
</div>

<div class="accordionItem">
<h2>Their Hitsongs</h2>
<div id="hitsongs"> </div>
</div>

</div>





</body>
</html>


[/CODE]
Copy linkTweet thisAlerts:
@criterion9Jul 06.2009 — It looks like your code will always target elements with a class of accordionItem. You might need to give each element you wish to hide an id and then select that id or add an onclick to the h2 that references the desired id as you build the list.
×

Success!

Help @MichiKen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...