菩提树的Framework架构

.net 架构,数据库设计,编码规范

导航

如何把 XML 数据显示为 HTML(加载XML/XSL几种方式)

加载XSLXML通常有几种方式:

1、    客户端加载(javascript

<html>

<body>

 

<script type="text/javascript">

var xmlDoc=null;

if (window.ActiveXObject)

{

// code for IE

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

}

else if (document.implementation.createDocument)

{

// code for Mozilla, Firefox, Opera, etc.

xmlDoc=document.implementation.createDocument("","",null);

}

else

{

alert('Your browser cannot handle this script');

}

if (xmlDoc!=null)

{

xmlDoc.async=false;

xmlDoc.load("cd_catalog.xml");

 

document.write("<table border='1'>");

 

var x=xmlDoc.getElementsByTagName("CD");

for (i=0;i<x.length;i++)

{

document.write("<tr>");

document.write("<td>");

document.write(

x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);

document.write("</td>");

 

document.write("<td>");

document.write(

x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);

document.write("</td>");

document.write("</tr>");

}

document.write("</table>");

}

</script>

 

</body>

</html>

 

 

2、 服务器端加载(Asp.net)

 

<%@ Control Language="c#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server" language="c#">
public string xmlSource, xslSource;
void Page_Load(){
    XmlDocument docXml = new XmlDocument();
    docXml.Load(Server.MapPath(xmlSource));
    XslTransform docXsl = new XslTransform();
    docXsl.Load(Server.MapPath(xslSource));
    docXsl.Transform(docXml,null,Response.Output);
//    chapter.Text = docXml.TransformNode(docXsl);
}
</script>

 

posted on 2010-08-11 09:05  菩提树下  阅读(765)  评论(0编辑  收藏  举报