<tour-objects>
<object model="tourname" tn="1">
<field name="name" type="CharField">Tour Name 1</field>
<field name="tour_image" type="FileField">http://url.com/images/picture.jpg</field>
</object>
<object model="tourname" tn="2">
<field name="name" type="CharField">Tour Name 2</field>
</object>
<object model="tourname" tn="3">
<field name="name" type="CharField">Tour Name 3</field>
</object>
</tour-objects>
使用ASP解析XML的问题。 XML代码结构类似于上面的代码。
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.Async = False
objXML.SetProperty "ServerHTTPRequest", True
objXML.ResolveExternals = True
objXML.ValidateOnParse = True
objXML.Load(Server.MapPath("data.xml"))
Set Objects = objXML.SelectNodes("//tour-objects/object[@model='tourname']")
For i = 0 To Objects.Length-1
For Each objNode In Objects(i).childNodes
Select Case Lcase(objNode.nodeName)
Case "field"
fld = objNode.getAttribute("name")
Select Case fld
Case "name"
txt_name = objNode.text
Case "tour_image"
txt_tour_image = objNode.text
End Select
End Select
Next
Response.Write("name: " & txt_name &"<br>")
Response.Write("tour_image: " & txt_tour_image &"<br><br>")
Next
并非每个节点都包含Tour_image。因此,当我解析时,出现以下错误。看来tour_image部分位于第二和第三节点上。
name: Tour Name 1
tour_image: http://url.com/images/picture.jpg
name: Tour Name 2
tour_image: http://url.com/images/picture.jpg
name: Tour Name 3
tour_image: http://url.com/images/picture.jpg
我在哪里犯错误?如果您能帮助我,我会很高兴。
答案 0 :(得分:0)
txt_name和txt_tour_image定义一次。发生的事情是第一个对象节点将txt_tour_image设置为http://url.com/images/picture.jpg。下一个对象没有名称为tour_image的字段。由于它没有一个,因此txt_tour_image不会更改,并且将保持不变,直到有一个带有tour_image的节点为止。
一旦进入for循环,就需要重置这两个变量
For i = 0 To Objects.Length-1
txt_name = ""
txt_tour_image = ""