网络编程
位置:首页>> 网络编程>> XML编程>> XML DOM介绍和例子(2)

XML DOM介绍和例子(2)

  发布时间:2007-10-15 20:23:00 

标签:dom,xml

4.装载一个XML文件到parser中

下面的代码装载存在的XML文档进入XML parser:

<script language="JavaScript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
// ....... processing the document goes here
</script>

第一行脚本增加了一个Microsoft XML parser实例,第三行装载名为”note.xml”的XML文档进入parser中。第二行保证文档装载完成以后parser进行下一步工作。

5. parseError对象

打开XMl文档时,XML Parser产生错误代码,并存在parseError对象中,包括错误代码、错误文本和错误行号,等信息。

6.文件错误

下面的例子将试图装载一个不存在的文件,然后产生相应的错误代码:


var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("ksdjf.xml")
document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)


7.XML错误

下面使用不正确的格式装载XMl文档,


var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note_error.xml")
document.write("<br>Error Code: ")
document.write(xmlDoc.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xmlDoc.parseError.reason)
document.write("<br>Error Line: ")
document.write(xmlDoc.parseError.line)


8. parseError属性

属性描述:
errorCode 返回长整型错误代码
reason 返回字符串型错误原因
line 返回长整型错误行号
linePos 返回长整型错误行号位置
srcText 返回字符串型产生错误原因
url 返回url装载文档指针
filePos 返回长整型错误文件位置

9.遍历节点树

一种通用的析取XML文档的方法是遍历节点树和它的元素值。下面是使用VBScript写的遍历节点树的程序代码:


set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
for each x in xmlDoc.documentElement.childNodes
document.write(x.nodename)
document.write(": ")
document.write(x.text)
next 


0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com