Suggestions for Javascript/XML flexibility?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • letsgetsilly
    New Member
    • Jul 2006
    • 9

    Suggestions for Javascript/XML flexibility?

    Hello all,

    A website I am working on uses javascript to read 6 headlines from an XML file and then outputs them to a <p> element by simply referring to the element's ID and filling it with the XML content. I need to have the ability to easily support 12 headlines.

    The requirement is for a vice president here (who knows little of html/javascript/XML) simply add headlines to the XML file and have them automatically show up in the web page.

    Currently I am just stepping through the XML file and outputting which won't do the job. I need to have some type of loop and if statement, but I have limited skills with java/xml. My code below will help illustrate whats going on. Thanks so much for any help!!

    Here is my html code, followed by Javascript, followed by XML:
    HTML:
    <p class="headline s" id="headline1"> </p>
    <br class="altheigh t">
    <p class="headline s" id="headline2"> </p>
    <br class="altheigh t">
    ...etc through "headline 6"

    Javascript:

    var xmlDoc
    function loadXML()
    {
    //load xml file
    // code for IE
    if (window.ActiveX Object)
    {
    xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
    xmlDoc.async=fa lse;
    xmlDoc.load("KD XHeadlines.xml" );
    getmessage()
    }
    // code for Mozilla, etc.
    else if (document.imple mentation && document.implem entation.create Document)
    {
    xmlDoc= document.implem entation.create Document("","", null);
    xmlDoc.load("KD XHeadlines.xml" );
    xmlDoc.onload=g etmessage
    }
    else
    {
    alert('Your browser cannot handle this script');
    }
    }

    function getmessage()
    {
    document.getEle mentById("headl ine1").innerHTM L=xmlDoc.getEle mentsByTagName( "headline1" )[0].firstChild.nod eValue
    document.getEle mentById("headl ine2").innerHTM L=xmlDoc.getEle mentsByTagName( "headline2" )[0].firstChild.nod eValue
    ...etc through "headline 6"
    }

    XML

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <headlines>
    <headline1>Ou r team wins</headline1>

    <headline2>Yadd a yadda</headline2>
    ...etc through <headline 6>
    </headlines>

    To summarize, I need the ability to simply add to the xml file and have it output to the html file. I have attempted to create blank XML tags (i.e.
    <headline7></headline7> but they provoke an error.

    Any help is much appreciated! I don't know where to go from here.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Try this

    Code:
    var xmlTag = xmlDoc.getElementsByTagName("headline2");
    
    if (xmlTag)
    {
        document.getElementById("headline2").innerHTML = xmlTag[0].firstChild.nodeValue;
    }
    Or something similar, that is check that the xmlTag exists before trying to write it's contents to the html

    Comment

    Working...