XML Document Node Names

Discussion in 'Javascript' started by Dante, Dec 13, 2003.

  1. Dante

    Dante Guest

    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:
    */
    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.
     
    Dante, Dec 13, 2003
    #1
    1. Advertising

  2. Dante

    VK Guest

    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?
     
    VK, Dec 13, 2003
    #2
    1. Advertising

  3. Dante

    Paul Guest


    > 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
     
    Paul, Dec 13, 2003
    #3
  4. Dante

    Dante Guest

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

    Thanks anyway.
     
    Dante, Dec 14, 2003
    #4
  5. Dante wrote:

    > 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
     
    Thomas 'PointedEars' Lahn, Dec 14, 2003
    #5
    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:
    1,670
  2. Lewis G. Pringle, Jr.
    Replies:
    0
    Views:
    632
    Lewis G. Pringle, Jr.
    Sep 30, 2003
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,521
    Dimitre Novatchev
    Aug 24, 2006
  4. thecolour
    Replies:
    0
    Views:
    395
    thecolour
    Jun 26, 2007
  5. thecolour
    Replies:
    2
    Views:
    447
    thecolour
    Jun 27, 2007
Loading...

Share This Page