www.webdeveloper.com
Results 1 to 2 of 2

Thread: Displaying HTML inside XML tags

  1. #1
    Join Date
    Apr 2009
    Posts
    1

    Displaying HTML inside XML tags

    Hi I'm new to the development world and have bumped into a tough problem for myself.

    I'm attempting to display external RSS feeds on my site using AJAX. The 'title' and 'link' tags from the XML file appear on my page without any problem. However, unlike the 'title' and 'link' tags, some of the 'description' tags contain HTML.

    RSS link: Jonathon's BlogEntry at 1Up.com


    This seems to be an issue when I'm trying to display the content within the description tags.

    Code:
    function getXMLHTTPRequest() {
    try {
    req = new XMLHttpRequest(); /* e.g. Firefox */
    } catch(e) {
      try {
      req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
      } catch (e) {
        try {
        req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
        } catch (E) {
          req = false;
        }
      }
    }
    return req;
    }
    
    var ro2 = getXMLHTTPRequest();
      
    function getGRblogRSS() {
      var mygrurl = 'rssfeedproxy.php?blogfeed=';
      var mygrfeed = 'http://www.1up.com/journalRSS?publicUserId=5641199';
        mygrRand = parseInt(Math.random()*999999999999999); // cache buster
      var modgrurl = mygrurl+escape(mygrfeed)+"&rand="+mygrRand;
      ro2.open("GET", modgrurl, true);
      ro2.onreadystatechange = useBlogResponse;
      ro2.send(null);
    }
        
    
    function useBlogResponse() {
       if (ro2.readyState == 4) {
        if(ro2.status == 200) {
           // first remove the childnodes presently in the DM
           while (document.getElementById('grblognews').hasChildNodes())
           {
    document.getElementById('grblognews').removeChild(document.getElementById('grblognews').firstChild);
           }
           var titleNodes = ro2.responseXML.getElementsByTagName("title");
           var descriptionNodes= ro2.responseXML.getElementsByTagName("description");
           var linkNodes = ro2.responseXML.getElementsByTagName("link");
           for(var i =2;i<7;i++)
           {
             var newtext = document.createTextNode(titleNodes[i].childNodes[0].nodeValue);
             var newpara = document.createElement('p');
             newpara.className = "grblogtitle";
             var para = document.getElementById('grblognews').appendChild(newpara);        
             newpara.appendChild(newtext);
            
             var linkstring = linkNodes[i].childNodes[0].nodeValue;
             var newpara3 = document.createElement('p');
             newpara3.className = "grbloglink";
             var newsdiv = document.getElementById('grblognews');
             newpara3.innerHTML = '<a href="'+linkstring+'">'+linkstring+'</a>';
             var para3 = newsdiv.appendChild(newpara3);
        
              var newtext2= document.createTextNode(descriptionNodes[i].childNodes[0].nodeValue);
             var newnontagtext= '<![CDATA['+newtext2+']]>';
             var newpara2 = document.createElement('p');
             newpara2.className = "grblogdescription";
             var para2 = document.getElementById('grblognews').appendChild(newpara2);
             newpara2.appendChild(newnontagtext);
             }
        }
      }
    }
    As you can see, I embarassingly tried enclosing the variable newtext2 in CDATA tags to no avail. Since I do not directly have access to this RSS file (other than asking my friend if I can edit it), is there a way for me to display the HTML content within the 'description' tags strictly via JavaScript?

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    once i was looking for something similar, to parse the HTML code contained on a XML file and i'm not sure if DOM have an HTML Parser. however, the solution i has seen before is to create an element and then insert the string in innerHTML. be aware that some browser doesn't parse certain tags.
    Code:
    var newtext2= document.createElement('p');
    p.innerHTML = descriptionNodes[i].firstChild.data;
    then append the element or element's nodes to document.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles