Solved

Showing XML gracefully.

Posted on 2002-05-16
6
527 Views
Last Modified: 2008-02-01
There is a stirng which is a xml file. and I want to show it on a new page gracefully.
I tried like below. unfortunately it won't work.

Thanks.

var res = "<?xml version ...> <bla><bla>... bla bla bla~ ";
var newWindow = window.open("","new");
newWindow.document.open("text/xml");
newWindow.document.write(res);
0
Comment
Question by:iamjhkang
  • 4
  • 2
6 Comments
 
LVL 23

Expert Comment

by:b1xml2
Comment Utility
what exactly do you mean by showing xml gracefully???
0
 
LVL 1

Author Comment

by:iamjhkang
Comment Utility
What I mean is showing res on a new window.
and basically any pure XML is shown in a IE collapse-expand fashion.
0
 
LVL 23

Expert Comment

by:b1xml2
Comment Utility
ahhhhhhhh =)

first use a HTA file locally
=============================
displayXml.hta
=============================
<html>
<head>
<script language="javascript">
var oXML = new ActiveXObject("Microsoft.XMLDOM");
var oXSLT = new ActiveXObject("Microsoft.XMLDOM");
oXML.async = false;
oXML.loadXML("<?xml version='1.0'?><root><users total='10' /></root>");
oXSLT.async = true;
oXSLT.load("res://msxml.dll/defaultss.xsl");
window.onload = displayXml;

function displayXml() {
 if (oXSLT.readyState == 4) {
  var wWin = window.open("about:blank","displayXml");
  wWin.document.write(oXML.transformNode(oXSLT));
  wWin.document.close();
 } else {
  window.setTimeout("displayXml();",50);
 }
}
</script>
</head>
<body>Displaying XML Syntax</body>
</html>

Notes
=====
1. With the new changes in MSIE via the latest security packages, it may not be possible to load the defaultss.xsl from within the browser. This can be circumvented only by the user when the users settings have allow "Access Data Sources Across Domains".

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 23

Expert Comment

by:b1xml2
Comment Utility
You can even change the displayXml():

function displayXml() {
 if (oXSLT.readyState == 4) {
  var wWin = window.open("about:blank","displayXml","height=300,width=300,resizable=yes");
  wWin.document.write(oXML.transformNode(oXSLT));
  wWin.document.title = "XML Document";
  wWin.document.close();
 } else {
  window.setTimeout("displayXml();",50);
 }
}

Notes
=====
I forgot to mention that you need the Microsoft XML Parser.
0
 
LVL 1

Author Comment

by:iamjhkang
Comment Utility
Thanks.

In my case xml(variable res) is just String type, so I like my way.
With some modification of the code below, it will work I think.

var res = "<?xml version ...> <bla><bla>... bla bla bla~ ";
var newWindow = window.open("","new");
newWindow.document.open("text/xml");
newWindow.document.write(res);

Again thanks.
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 50 total points
Comment Utility
I think you totally misunderstand how MSIE displays XML in such a contraction/expansion module.

And it has nothing to do with XML per se. Your code WILL NEVER WORK.

1. The code presented actually makes use of the defaultss.xsl which MSIE uses whenever it finds the HTTP Header's content type to be either text/xml or text/xsl. You can't set this via the document.open(), that will fail.

2. The code presented wraps the string into the XML DOM si that we can use the defaultss.xsl to perform the transformation to HTML in which case, the data is seen in a collapsible format.

3. The long and short of it is that if you want to open a new window and view any XML as the form that MSIE displays it and you are writing data to the window, you will have to use it this way, or write you own javascript modules that do it.

Thus, the display of XML in MSIE is due to the transform done via the defaultss.xsl document. On the client-side, writing code and expecting it to be displayed in the same format will require a transform.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now