论坛首页 Java版

求教:关于xml数据的显示

浏览 2617 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2004-04-02
有一个DomDocument的对象xmldoc,想把它显示到一个页面上,但如果用
document.write(xmldoc.xml)的方式,ie会把xml的标签理解为html的标签,
结果是什么也显示不出来,如何能把它显示出来呢?就像ie直接打开一个xml文件那样
   
时间:2004-04-02
可以用xmldoc.transformNode(xsldoc)来实现dom的html展示,其中xsldoc是XSL文件,负责xml数据到html的转换
   
0 请登录后投票
时间:2004-04-02
高兴看到有人回帖,不过可能曲解了提问的本意,对于已知结构的DomDocument来讲,总有办法显示出来。但我关心的是仅仅xml数据,换言之,对于任何结构的DomDocument,都可以显示到页面上,这当然不包括alert(xmldoc.xml)的方法.
望继续给予提示!(上帖所用的xmldoc.transformNode(xsldoc)方式没有尝试过,或许已经解决我的疑问,可否给予example)
   
0 请登录后投票
时间:2004-04-05
你遇到的问题我也考虑过,好像现在还没有什么好的方法。我是用下面两种方法处理这个问题的:
1.自己写一个formatXML函数,再把format后的结果alert出来。对于那种数据量比较少的,基本能满足,用起来也是比较方便的。
2.用InterDev调试,把XML Document中的内容拷出来。通常我的桌面上有一个display.xml文件。把上面的内容拷到这个文件中,再用IE打开。
呵呵,也许这两种方法有点笨:)不知道有不有更好的方法?
   
0 请登录后投票
时间:2004-04-06
Wolverene 写道
:D 高兴看到有人回帖,不过可能曲解了提问的本意,对于已知结构的DomDocument来讲,总有办法显示出来。但我关心的是仅仅xml数据,换言之,对于任何结构的DomDocument,都可以显示到页面上,这当然不包括alert(xmldoc.xml)的方法.
望继续给予提示!(上帖所用的xmldoc.transformNode(xsldoc)方式没有尝试过,或许已经解决我的疑问,可否给予example)


呵呵!楼上的说的是正解呀!你都没试试怎么就说曲解了你的意思了。:D

给你个例子,从msdn里拷贝的:

[code:1]
<HTML>
<HEAD>
<TITLE>Simple demo of Microsoft XSL Processor</TITLE>
</HEAD>
<XML id="source" src="simple.xml"></XML>
<XML id="style" src="xml.xsl"></XML>

<SCRIPT FOR="window" EVENT="onload">
//xslTarget.innerHTML = source.transformNode(style.XMLDocument);
document.write(source.transformNode(style.XMLDocument));
</SCRIPT>
<BODY>
<P STYLE="font-size:10pt; font-family:Verdana; color:gray">
<B>This demo shows the use of data islands for loading XML source and
XSL style sheets and inserting the transformed result into the Web page.</B>
</P>
<DIV id="xslTarget"></DIV>
</BODY>
</HTML>
[/code:1]
   
0 请登录后投票
时间:2004-04-06
这里用到的xml.xsl如下:

[code:1]
<?xml version="1.0"?>

<!-- Generic stylesheet for viewing XML -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<!-- This template will always be executed, even if this stylesheet is not run on the document root -->
<xsl:template>
<DIV STYLE="font-family:Courier; font-size:10pt; margin-bottom:2em">
<!-- Scoped templates are used so they don't interfere with the "kick-off" template. -->
<xsl:apply-templates select=".">
<xsl:template><xsl:apply-templates/></xsl:template>

<xsl:template match="*">
<DIV STYLE="margin-left:1em; color:gray">
&<xsl:node-name/><xsl:apply-templates select="@*"/>/&
</DIV>
</xsl:template>

<xsl:template match="*[node()]">
<DIV STYLE="margin-left:1em">
<SPAN STYLE="color:gray">&<xsl:node-name/><xsl:apply-templates select="@*"/>&</SPAN><xsl:apply-templates select="node()"/><SPAN STYLE="color:gray">&/<xsl:node-name/>&</SPAN>
</DIV>
</xsl:template>

<xsl:template match="@*">
<SPAN STYLE="color:navy"> <xsl:node-name/>="<SPAN STYLE="color:black"><xsl:value-of /></SPAN>"</SPAN>
</xsl:template>

<xsl:template match="pi()">
<DIV STYLE="margin-left:1em; color:maroon">&?<xsl:node-name/><xsl:apply-templates select="@*"/>?&</DIV>
</xsl:template>

<xsl:template match="cdata()"><pre>&![CDATA[<xsl:value-of />]]&</pre></xsl:template>

<xsl:template match="textNode()"><xsl:value-of /></xsl:template>
</xsl:apply-templates>
</DIV>
</xsl:template>
</xsl:stylesheet>
[/code:1]
   
0 请登录后投票
时间:2004-04-06
如果想实现ie里的树状结构。
可以试试这个xsl。
唉~

[code:1]
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xmldisplay.xsl"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"
xmlns:dt="urn:schemas-microsoft-com:datatypes"
xmlns:d2="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; CHARSET=windows-1252" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.rsac.org/ratingsv01.html" l comment "RSACi North America Server" by "inet@microsoft.com" r (n 0 s 0 v 0 l 0))' />
<meta http-equiv="MSThemeCompatible" content="Yes" />
<style type="text/css">
body
{
font-family:Tahoma;
font-size:8pt;
padding:3px;
}

.c
{
cursor:hand;
}

.b
{
color:red;
font-family:Courier New;
font-weight:bold;
text-decoration:none;
}

.e
{
margin-left:1em;
text-indent:-1em;
margin-right:1em;
}

.k
{
margin-left:1em;
text-indent:-1em;
margin-right:1em;
}

.t
{
color:#990000;
}

.xt
{
xcolor:#990099;
}

.ns
{
color:red;
}

.dt
{
color:green;
}

.tx
{
font-weight:bold;
}

.db
{
text-indent:0px;
margin: 0em 1em 0em 1em;
padding:0em .3em 0em .3em;
border-left:1px solid #CCCCCC;
font:small Courier
}

.di
{
font:small Courier;
}

.d, .pi, .m
{
color:blue;
}

.cb
{
text-indent:0px;
margin: 0,1em,0,1em;
padding-left:.3em;
font:small Courier;
color:#888888;
}

.ci
{
font:small Courier;
color:#888888;
}

pre
{
margin:0px;
display:inline;
}

h5
{
margin:0px 13px 0px 13px;
}

button#eBtnClose
{
position:absolute;
bottom:5px;
padding-left:10px;
padding-right:10px;
font:messagebox;
}

</style>
<script type="text/jscript">
<xsl:comment>
<![CDATA[
function f( e )
{
if( e.className == "ci" )
{
if ( e.children(0).innerText.indexOf("\n") > 0 )
{
fix(e, "cb");
}
}
if( e.className == "di" )
{
if( e.children(0).innerText.indexOf("\n") > 0 )
{
fix(e, "db");
}
}
e.id = "";
}

function fix( e, cl )
{
e.className = cl;
e.style.display = "block";
j = e.parentElement.children(0);
j.className = "c";
k = j.children(0);
k.style.visibility = "visible";
k.href = "javascript:void(0);";
}

function ch( e )
{
mark = e.children(0).children(0);
if( mark.innerText == "+" )
{
mark.innerText = "-";
for( var i = 1; i < e.children.length; i++ )
{
e.children(i).style.display = "block";
}
}
else if( mark.innerText == "-" )
{
mark.innerText = "+";
for( var i = 1; i < e.children.length; i++ )
{
e.children(i).style.display="none";
}
}
}

function ch2( e )
{
mark = e.children(0).children(0);
contents = e.children(1);
if( mark.innerText == "+" )
{
mark.innerText = "-";
if( contents.className == "db" || contents.className == "cb" )
{
contents.style.display = "block";
}
else
{
contents.style.display = "inline";
}
}
else if(mark.innerText == "-")
{
mark.innerText = "+";
contents.style.display = "none";
}
}

function cl()
{
e = window.event.srcElement;
if( e.className != "c" )
{
e = e.parentElement;
if( e.className != "c" )
{
return;
}
}
e = e.parentElement;
if( e.className == "e" )
{
ch( e );
if( e.className == "k" )
ch2( e );
}
}

function ex()
{

}

function h()
{
window.status=" ";
}

document.onclick = cl;

function Finish()
{
self.close();
}

function ESC_KeyDown()
{
if( 27 == event.keyCode )
{
self.close();
}
}

]]>
</xsl:comment>
</script>
</head>
<body onbeforeunload="window.dialogArguments.g_oXMLDlg = null;" onkeydown="ESC_KeyDown();" class="st">
<div style="overflow-y:auto;height:80%;">
<h5></h5>
<hr style="color:navy;height:1px;margin:0px 13px 0px 13px;" />
<xsl:apply-templates />
</div>
<button id="eBtnClose" onclick="Finish();" style="left:expression( (document.body.clientWidth - eBtnClose.offsetWidth) / 2 );"></button>
</body>
</html>
</xsl:template>
<!-- Templates for each node type follows. The output of each template has a similar structure
to enable script to walk the result tree easily for handling user interaction. -->

<!-- Template for the DOCTYPE declaration. No way to get the DOCTYPE, so we just put in a placeholder -->
<xsl:template match="node()[nodeType()=10]">
<div class="e">
<span>
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="d">&!DOCTYPE <xsl:node-name/><I> (View Source for full doctype...)</I>&</span>
</span>
</div>
</xsl:template>

<!-- Template for pis not handled elsewhere -->
<xsl:template match="pi()">
<div class="e">
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="m">&?</span>
<span class="pi"><xsl:node-name/> <xsl:value-of/></span>
<span class="m">?&</span>
</div>
</xsl:template>

<!-- Template for the XML declaration. Need a separate template because the pseudo-attributes
are actually exposed as attributes instead of just element content, as in other pis -->
<xsl:template match="pi('xml')">
<div class="e">
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="m">&?</span>
<span class="pi">xml <xsl:for-each select="@*"><xsl:node-name/>="<xsl:value-of/>" </xsl:for-each></span>
<span class="m">?&</span>
</div>
</xsl:template>

<!-- Template for attributes not handled elsewhere -->
<xsl:template match="@*" xml:space="preserve">
<span><xsl:attribute name="class"><xsl:if match="xsl:*/@*">x</xsl:if>t</xsl:attribute> <xsl:node-name/></span>
<span class="m">="</span>
<B><xsl:value-of/></B><span class="m">"</span>
</xsl:template>

<!-- Template for attributes in the xmlns or xml namespace -->
<xsl:template match="@xmlns:*|@xmlns|@xml:*">
<span class="ns"> <xsl:node-name/></span>
<span class="m">="</span>
<B class="ns"><xsl:value-of/></B>
<span class="m">"</span>
</xsl:template>

<!-- Template for attributes in the dt namespace -->
<xsl:template match="@dt:*|@d2:*">
<span class="dt"> <xsl:node-name/></span>
<span class="m">="</span>
<B class="dt"><xsl:value-of/></B>
<span class="m">"</span>
</xsl:template>

<!-- Template for text nodes -->
<xsl:template match="textNode()">
<div class="e">
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="tx"><xsl:value-of/></span>
</div>
</xsl:template>

<!-- Template for comment nodes -->
<xsl:template match="comment()">
<div class="k">
<span>
<a class="b" onclick="return false" onfocus="h()" STYLE="visibility:hidden">-</a>
<span class="m">&!--</span>
</span>
<span id="clean" class="ci"><PRE><xsl:value-of/></PRE></span>
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="m">--&</span>
<script type="text/jscript">
f( clean );
</script>
</div>
</xsl:template>

<!-- Template for cdata nodes -->
<xsl:template match="cdata()">
<div class="k">
<span>
<A class="b" onclick="return false" onfocus="h()" STYLE="visibility:hidden">-</A>
<span class="m">&![CDATA[</span>
</span>
<span id="clean" class="di">
<PRE><xsl:value-of/></PRE>
</span>
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="m">]]&</span>
<script type="text/jscript">
f( clean );
</script>
</div>
</xsl:template>

<!-- Template for elements not handled elsewhere (leaf nodes) -->
<xsl:template match="*">
<div class="e">
<div STYLE="margin-left:1em;text-indent:-2em">
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="m">&</span>
<span><xsl:attribute name="class"><xsl:if match="xsl:*">x</xsl:if>t</xsl:attribute><xsl:node-name/></span>
<xsl:apply-templates select="@*"/>
<span class="m"> /&</span>
</div>
</div>
</xsl:template>

<!-- Template for elements with comment, pi and/or cdata children -->
<xsl:template match="*[node()]">
<div class="e">
<div class="c">
<A href="#" onclick="return false" onfocus="h()" class="b">-</A>
<span class="m">&</span>
<span><xsl:attribute name="class"><xsl:if match="xsl:*">x</xsl:if>t</xsl:attribute><xsl:node-name/></span>
<xsl:apply-templates select="@*"/>
<span class="m">&</span>
</div>
<div>
<xsl:apply-templates/>
<div>
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="m">&/</span>
<span><xsl:attribute name="class"><xsl:if match="xsl:*">x</xsl:if>t</xsl:attribute><xsl:node-name/></span>
<span class="m">&</span>
</div>
</div>
</div>
</xsl:template>

<!-- Template for elements with only text children -->
<xsl:template match="*[textNode()$and$$not$(comment()$or$pi()$or$cdata())]">
<div class="e">
<div STYLE="margin-left:1em;text-indent:-2em">
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="m">&</span>
<span><xsl:attribute name="class"><xsl:if match="xsl:*">x</xsl:if>t</xsl:attribute><xsl:node-name/></span>
<xsl:apply-templates select="@*"/>
<span class="m">&</span>
<span class="tx"><xsl:value-of/></span>
<span class="m">&/</span>
<span><xsl:attribute name="class"><xsl:if match="xsl:*">x</xsl:if>t</xsl:attribute><xsl:node-name/></span>
<span class="m">&</span>
</div>
</div>
</xsl:template>

<!-- Template for elements with element children -->
<xsl:template match="*[*]">
<div class="e">
<div class="c" STYLE="margin-left:1em;text-indent:-2em">
<A href="#" onclick="return false" onfocus="h()" class="b">-</A>
<span class="m">&</span>
<span><xsl:attribute name="class"><xsl:if match="xsl:*">x</xsl:if>t</xsl:attribute><xsl:node-name/></span>
<xsl:apply-templates select="@*"/>
<span class="m">&</span>
</div>
<div>
<xsl:apply-templates/>
<div>
<span class="b"><xsl:entity-ref name="nbsp"/></span>
<span class="m">&/</span>
<span><xsl:attribute name="class"><xsl:if match="xsl:*">x</xsl:if>t</xsl:attribute><xsl:node-name/></span>
<span class="m">&</span>
</div>
</div>
</div>
</xsl:template>
</xsl:stylesheet>
[/code:1]
   
0 请登录后投票
论坛首页 Java版

跳转论坛:
JavaEye推荐