XML Document Node Names

D

Dante

Hello. I have a Javascript that gets data from an XML document and
displays it through javascript. The problem is that when I do
dcfile.getElementsByTagName("subhead")[0].firstChild.nodeName
all I get is #text. Can you tell me why?

Here is my code:
/*
The Dante-Cubed Standardized Markup Language Javascript API
---[Powered by DCScript]---
Written by Sean M. Hall, Dante, and Peter-Paul Koch
This is the only script we request you ask for permission to use
Permission: (e-mail address removed)
*/
if (location.search.indexOf("=") > 2) {
file = location.search.substring(location.search.indexOf("=")+1);
}
else {
file="apple.xml";
}
function getDCSML() {
if (document.implementation && document.implementation.createDocument)
{
dcfile = document.implementation.createDocument("", "", null);
dcfile.onload = createDisplay;
}
else if (window.ActiveXObject)
{
dcfile = new ActiveXObject("Microsoft.XMLDOM");
dcfile.onreadystatechange = function () {
if (dcfile.readyState == 4) createDisplay()
};
}
else
{
alert('Your browser can\'t handle DCSML. Too bad.');
return;
}
dcfile.load(file);
}
function createDisplay()
{
var x = dcfile.getElementsByTagName("DCSML")[0];
var atext = dcfile.getElementsByTagName("Link")[0];
var htext = dcfile.getElementsByTagName("Header")[0];
var ahtext = dcfile.getElementsByTagName("AltHeader")[0];
var pic = dcfile.getElementsByTagName("pic")[0];
var sub = dcfile.getElementsByTagName("subhead")[0];
/*what= new Array();
for (var c=0;x<x.childNodes.length;c++)
{
what[c] = x.childNodes[1].firstChild.nodeValue;
alert(what[c]);
}*/
var othertags = x.childNodes;
var url = atext.getAttribute("URL");
//Below is a list of valid DCSML tags
var tags = new Array("DCSML","Header","AltHeader","Link","pic","subhead");
var makeLink = document.createElement("A");
makeLink.href = url;
var makeHeader = document.createElement("H1");
var makeAH = document.createElement("H2");
makeHeader.style.color="#CD853f";
makeAH.style.color="#CD853F";
var mahtext = document.createTextNode(ahtext.firstChild.nodeValue);
makeAH.appendChild(mahtext);
var mhtext = document.createTextNode(htext.firstChild.nodeValue);
makeHeader.appendChild(mhtext);
var txt = document.createTextNode(atext.firstChild.nodeValue);
makeLink.appendChild(txt);
var img = document.createElement("IMG");
img.src = pic.getAttribute("url");
var p = document.createElement("P");
var mtxt = sub.firstChild.nodeName;
/*
How odd is this? A script inside a script? I'm branding this a
seperate script, because of its importance. It sorts the DCSML
document tags, and creates arrays for existent and non-existant tags.
*/
//Begin DCScript Tag-Sorter
not_tags= new Array();
is_tag= new Array();
for (var i=0;i<tags.length;i++)
{
var checktags = dcfile.getElementsByTagName(tags);
if (checktags[0] == null) {
not_tags = tags
}
else {
is_tag = tags;
}
}
//End tag-sorter

//Ugh it's gonna be a bitch sorting this out...
document.body.appendChild(makeHeader);
document.body.appendChild(makeAH);
document.body.appendChild(makeLink);
document.body.innerHTML+="<p></p>";
document.body.appendChild(img);
document.body.appendChild(p);
document.body.innerHTML+='<span style="font-size: 13pt; font-family:
verdana; font-weight:

normal; text-align: left; color: #666666; letter-spacing:
1.2px;">'+mtxt+'</span><br>';
document.body.innerHTML+=dcfile.getElementsByTagName("subhead")[0].nextSibling.firstChild.nodeValue;
}

Thank you, Dante.
 
V

VK

I think more important to see the source document your are scripting
with. Can you post <subhead> block's foo with a couple of lines above
and below?
 
P

Paul

dcfile.getElementsByTagName("subhead")[0].firstChild.nodeName

the name of the node is #text (because the first child is a
text-node), I think you want the value, try .nodeValue :)

Paul
 
D

Dante

nope. nodeValue gives the text inside the tag. I solved the problem:
dcfile.getElementsByTagName("subhead")[0].nodeName gives what I want.

Thanks anyway.
 
T

Thomas 'PointedEars' Lahn

Dante said:
nope. nodeValue gives the text inside the tag.

You meant it retrieves the text content of the element. Inside a <tag>
are the tag name, optional attribute identifiers and (depending on the
markup language) optional values, and special punctuators like `=' and
`/'.


PointedEars
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top