当前位置: 移动技术网 > IT编程>开发语言>Asp > ASP XML编程objXML.async = False第1/2页

ASP XML编程objXML.async = False第1/2页

2017年12月12日  | 移动技术网IT编程  | 我要评论
从技术上来说,在asp环境中,读入并管理xml文本的主要方法有三种:
创建msxml对象,并且将xml文档载入dom;
使用服务器端嵌入(server-side include,ssi);
就如同访问其他文本文件一样,使用filesystemobject来访问xml文档;
第四种方法是在客户端创建内置的数据岛,有关的内容以后讲解。
一、使用dom
为了在asp代码中使用dom,需要创建一个microsoft xml分析器的实例,它像任何别的com组件一样被实例化,在页面的开始处顼要增加几行标准代码。这些代码创建一个分析器实例,加载xml文档至dom,并且将根元素(即文档元素)设置为当前节点。
‘instatiate the xml processor
set objxml = server.createobject("microsoft.xmldom")
load the xml document
objxml.load(server.mappath("mydata.xml")
set the document element
set objrootelement = objxml.documentelement
在xml文档被加载之前,还需要执行第四步,即设置validateonparse属性为true,这可确保被加载的文档为有效的xml文档。这可避免后面遇到的各种麻烦:
instatiate the xml processor
set objxml = server.createobject("microsoft.xmldom")
the processos should validate the document
objxml.validateonparse = true
load the xml document
objxml.load(server.mappath("mydata.xml")
set the document element
set objrootelement = objxml.documentelement
最后,还有一个可选步骤,它也是出现在加载之前。它要求同步加载文件:
objxml.async = false
这说时加载并验证一个相当大的文件需要占用一些时间。另一种替换方案是忽略这一步,允许非同步加载,这是缺省情况,一旦完成这些初始化步骤,xml文档就被加载,并且做好了被处理的准备。dom所有重要的功能都是可配置的。
当然,就如同任何com对象一样,在使用完之后,请记住必须销毁它:
set objxml = nothing
二、服务器端嵌入
服务器端嵌入可用于将xml文档代码插入asp页面。
三、用asp代码处理xml的示例
<html>
<head>
</head>
<body>
<%
dim sourcefile,source,rootelement,htmlcode
sourcefile = request.servervariables("appl_physical_path") & "xml\contacts.xml"
set source = server.createobject("microsoft.xmldom")
source.async = false
source.load sourcefile
set rootelement = source.documentelement
htmlcode = htmlcode & "<font size=4 face=verdana>"
htmlcode = htmlcode & rootelement.childnodes(0).text
htmlcode = htmlcode & "</font><p></p><font size=3 face=verdana><i>"
htmlcode = htmlcode & rootelement.childnodes(0).text
htmlcode = htmlcode & "</i></font><p></p><font size=3 face=verdana>"
htmlcode = htmlcode & rootelement.childnodes(0).text
htmlcode = htmlcode & "</font><p></p>"
response.write(htmlcode)
set source = nothing
%>
</body>
</html>
contacts.xml
<?xml version="1.0" ?>
<contact_info>
<contact>
<name>john</name>
<phone>111-1111-111</phone>
</contact>
<contact>
<name>smith</name>
<phone>222-2222-222</phone>
</contact>
<contact>
<name>mike</name>
<phone>333-3333-333</phone>
</contact>
</contact_info>
经xsl格式化的xml数据
stylecontact.asp
<html>
<body>
<%
sourcefile = server.mappath("contact.xml")
stylefile = server.mappath("contact.xsl")
set source = server.createobject("microsoft.xmldom")
source.async = false
source.load(sourcefile)
set style = server.createobject("microsoft.xmldom")
style.async = false
style.load(stylefile)
response.write(source.transformnode(style))
%>
</body>
</html>
contact.xml
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="contact.xsl" ?>
<contact_info>
<contact>
<name>zhou.zf</name>
<phone>11111111111</phone>
</contact>
<contact>
<name>listen</name>
<phone>22222222222</phone>
</contact>
<contact>
<name>bubu</name>
<phone>33333333333</phone>
</contact>
</contact_info>
contact.xsl
<?xml version="1.0" ?>
<xsl:template xmlns:xsl="http://www.w3.org/tr/wd-xsl">
<html>
<body>
<xsl:for-each select="contact_info/contact">
<div>
<xsl:value-of select="name"/>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
1

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网