vbs访问xml文件

上一篇 / 下一篇  2011-08-15 16:42:11 / 个人分类:VBScript

 
xml文件
<bookstore>51Testing软件测试网/Mw[,O;YS }A ~ z
 <book category="children">51Testing软件测试网yi#X5yL/z
  <title lang="en">9898</title>
I_ ^#cOG`4AQ:b0  <author>J K. Rowling</author>51Testing软件测试网H wq+B6|],c
  <year>2005</year>
:\6{d.O]0  <price>29.99</price>51Testing软件测试网C1Bh I)v/~5`
 </book>51Testing软件测试网9?u H C G OmA
 <book category="cooking">
;[V2G7M9Ox0  <title lang="en">Everyday Italian</title>51Testing软件测试网1j"Q+IK7U-]
  <author>Giada De Laurentiis</author>51Testing软件测试网 X7LQ O j B\
  <year>2005</year>
(^s ~1y S C)a0  <price>30.00</price>51Testing软件测试网pZ:p;JsA*[+fk
 </book>51Testing软件测试网6I6xh8cfaeA
 <book category="web" cover="paperback">
X fNQ mU M0  <title lang="en">Learning XML</title>
,Npzj h#T#h0  <author>Erik T. Ray</author>51Testing软件测试网 Az(Q'xA
  <year>2003</year>51Testing软件测试网N#jT`vK)euF*G
  <price>39.95</price>51Testing软件测试网k#z^ Bts fU
 </book>
(f6e4tW qO ])f0</bookstore>
 
通过节点之间的关系来查找51Testing软件测试网nI;cQA8mc\
Set xmldoc=CreateObject("microsoft.xmldom")
'建立xmldom对象及加载
c%K}@ iGD9}0xmldoc.load("d:\123.xml")
6Y?Zp(w3C0Set x=xmldoc.documentElement.firstChild.firstChild.nextSibling.childNodes(0)
'返回为J K. Rowling
G_Du5jJA [0MsgBox x.nodeValue
 
通过getelementbytagname查找,tagname为元素节点的标签,只能用来查找元素节点
M5Vn_Vn |0Set xmldoc=CreateObject("microsoft.xmldom")
Xu*PY,|0xmldoc.load("d:\123.xml")
}{ t T.Of0'x相当于</name>john</name>的john,为文本节点,nodetype=3文本节点不是元素节点name的值,是其的子节点
3K.{YBM0\4e:z X z:}0Set x=xmldoc.getElementsByTagName("book")(0).ownerDocument.getElementsByTagName("author")(0).childNodes(0)51Testing软件测试网]AF5nrR
MsgBox x.nodeValue

a:[#yXRj0通过遍历节点
F(D*kC(FzHS0Set xmldoc=CreateObject("microsoft.xmldom")51Testing软件测试网pO(]b(s"W,Zl:c-{
xmldoc.load("d:\123.xml")
&P}&~I#T)K(I0Set x=xmldoc.getElementsByTagName("book")(0).firstChild51Testing软件测试网a/r~zX,Zr
Set y=xmldoc.getElementsByTagName("book")(0).childNodes51Testing软件测试网0D-^*LOJ
For i=0 To y.length-151Testing软件测试网 S6w3g3]3j,p,s+v
'1为元素节点51Testing软件测试网4nf$fnC}1{
If x.nodeType=1 Then MsgBox x.childNodes(0).nodeValue51Testing软件测试网'~7Ad,i6_&`8XA g _b
Set x=x.nextSibling51Testing软件测试网0`!_j l&I!d\B ]q j
Next

TAG:

 

评分:0

我来说两句

Open Toolbar