欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

XML DOM介绍和例子(二)

程序员文章站 2022-03-02 21:50:19
5. parseerror对象     打开xml文档时,xml parser产生错误代码,并存在parseerror对象中,包括错误代码、错误文本和错误行号...
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