Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Set ContentType to text/xml with Javascript

Posted on 2006-06-08
8
Medium Priority
?
2,663 Views
Last Modified: 2008-01-09
Hi: I have the following Javascript function that's working but only writing the data out as a regular webpage. How do I set the content type to "text/xml" so that it displays as a real xml file?

function openRSS() {
      //var html = '';
      var str = '<?xml version=""1.0"" ?>\n';
            str += '<rss version="2.0">\n';
            str += '<channel>\n';
            //str += '<title></title>\n';
            //str += '<link></link>\n';
            str += '<description>CNIC News</description>\n';
            str += '<language>en-us</language>\n';

         var showTextElem = document.getElementById("txtDiv");

         if (showTextElem != null) {
               str += showTextElem.innerHTML;
         }
         else
         {
            alert("Could not find the text section in the HTML!");
          return;
      }

         //html += '\n</bo' + 'dy>\n</ht' + 'ml>';
      str += '</channel>\n';
      str += '</rss>\n';

         var textWin = window.open("","rss");
         textWin.document.open();
         textWin.document.write(str);
         textWin.document.close();
}
0
Comment
Question by:greddin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
8 Comments
 
LVL 10

Expert Comment

by:ClickCentric
ID: 16867293
I don't think the content-type can be changed after the fact.  If the file starts off as html, you can't just change it to xml.
0
 
LVL 30

Accepted Solution

by:
callrs earned 2000 total points
ID: 16867631
<html>
<script type=text/javascript>
var str='&#60;?xml version=""1.0"" ?&#62;\n'
str += '&#60rss version="2.0">\n';
str += '&#60channel>\n';
//str += '&#60title></title>\n';
//str += '&#60link></link>\n';
str += '&#60description>CNIC News&#60/description>\n';
str += '&#60language>en-us&#60/language>\n';
var textWin = window.open("","rss");
textWin.document.open();
textWin.document.write(str);
textWin.document.close();
</script>
</html>

Save the above text as a new .htm file & open it in your browser.
Solution is:
Replace all < with &#60;
You can also replace < with &#62; but it doesn't seem to make a difference
0
 
LVL 5

Expert Comment

by:gksinghiet
ID: 16867632
Set the content type in your original page. Like if you are using JSP then you can set the content type by using page directive as:
<%@ page language="java" contentType="text/xml; charset=UTF-8" %>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:callrs
ID: 16867634
You can also replace < with &#62; --> I meant: You can also replace > with &#62;
0
 
LVL 5

Expert Comment

by:gksinghiet
ID: 16867656
Or simply use meta tag as:
<meta http-equiv="Content-Type" content="text/xml; charset="utf-8" />
0
 
LVL 30

Expert Comment

by:callrs
ID: 16867781
Re: meta tags
Meta tags don't affect the output here in IE 6 (I tried it, no luck)....
0
 
LVL 10

Expert Comment

by:ClickCentric
ID: 16867827
Has it ever been determined why you'd want to open a popup with an rss xml feed in it?  
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question