Asp classic 从XML获取多个值(经典ASP)
我有一个包含大量数据的XML文件,我想获得一些值,以便在经典ASP中进行进一步处理 我的XML文件如下所示Asp classic 从XML获取多个值(经典ASP),asp-classic,xml-parsing,Asp Classic,Xml Parsing,我有一个包含大量数据的XML文件,我想获得一些值,以便在经典ASP中进行进一步处理 我的XML文件如下所示 <result> <items> <client> <clientid>12345</clientid> <name>Acme Inc</name> <site> <siteid>98765</si
<result>
<items>
<client>
<clientid>12345</clientid>
<name>Acme Inc</name>
<site>
<siteid>98765</siteid>
<name>Acme Site</name>
<workstations>
<workstation>
<id>12345</id>
<name>LAPTOP1</name>
<failed_checks>
<check>
<checkid>9876543</checkid>
<check_type>687</check_type>
<description>Smart Error</description>
<startdate>2015-04-09</startdate>
<starttime>12:01:00</starttime>
<formatted_output>Smart Error</formatted_output>
</check>
</failed_checks>
</workstation>
</workstations>
</site>
</client>
<client>
<clientid>67543</clientid>
<name>Contoso Ltd</name>
<site>
<siteid>98732</siteid>
<name>Contoso Site A</name>
<servers>
<server>
<id>789999</id>
<name>SERVER1</name>
<failed_checks>
<check>
<checkid>76543555</checkid>
<check_type>2918</check_type>
<description>Disk Space Error - C:</description>
<startdate>2015-04-09</startdate>
<starttime>12:01:00</starttime>
<formatted_output>Total: 136.70GB, Free: 0.57GB</formatted_output>
</check>
</failed_checks>
</server>
</servers>
</site>
</client>
</items>
12345
顶点公司
98765
顶点站点
12345
LAPTOP1
9876543
687
智能错误
2015-04-09
12:01:00
智能错误
67543
康托索有限公司
98732
Contoso站点A
789999
服务器1
76543555
2918
磁盘空间错误-C:
2015-04-09
12:01:00
总计:136.70GB,免费:0.57GB
我所有的尝试都没有完全满足我的需要。
我可以为每个
获取一个值。我可以检索
,也可以轻松获取
的值。我的问题是,当我想将
与
或
以及
的值结合在一起时
换句话说,我想得到这个输出;
Acme Inc.LAPTOP1智能错误2015-04-09
Contoso Ltd服务器1磁盘空间错误-C:2015-04-09
我得到的最好结果是使用此代码;(它正在工作,但显示不正确-
与
不匹配)
所以我的输出现在更像这样;
Acme Inc服务器1磁盘空间错误-C:2015-04-09
Contoso有限公司LAPTOP1智能错误2015-04-09
Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False
xmlDOM.setProperty "ServerHTTPRequest", True
xmlDOM.Load("my xml file")
Set Root = xmlDOM.documentElement
Set NodeList = Root.getElementsByTagName("client")
For i = 0 to NodeList.length -1
Set clientid = xmlDOM.getElementsByTagName("clientid")(i)
Set name = xmlDOM.getElementsByTagName("name")(i)
Set ckdesc = xmlDOM.getElementsByTagName("site/*/*/failed_checks/check/description")(i)
Set ckdev = xmlDOM.getElementsByTagName("site/*/*/name")(i)
Response.Write clientid.text & " " & name.text & " " & ckdev.text & " " & ckdesc.text & "<br>"
Next
Set xmlDOM = Nothing
Set NodeList = Nothing
Set xmlDOM=Server.CreateObject(“MSXML2.DOMDocument”)
xmlDOM.async=False
xmlDOM.setProperty“ServerHTTPRequest”,True
Load(“我的xml文件”)
Set Root=xmlDOM.documentElement
Set NodeList=Root.getElementsByTagName(“客户端”)
对于i=0到NodeList.length-1
Set clientid=xmlDOM.getElementsByTagName(“clientid”)(i)
Set name=xmlDOM.getElementsByTagName(“名称”)(i)
Set ckdesc=xmlDOM.getElementsByTagName(“site/*/*/failed_checks/check/description”)(i)
设置ckdev=xmlDOM.getElementsByTagName(“site/*/*/name”)(i)
响应。写入clientid.text&&name.text&&ckdev.text&&ckdesc.text&&br>“
下一个
设置xmlDOM=Nothing
设置节点列表=无
我用
selectNodes
和selectSingleNode
做了一些atempts,它们只显示客户机名称或支票说明。我没有成功地将这些值组合在一起。使用selectNodes
和selectSingleNode
如下:
Dim xmlDOM
Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.LoadXml(sXml)
Dim NodeList, i, clientid, name, ckdesc, ckdev
Set NodeList = xmlDom.selectNodes("/result/items/client")
For i = 0 to NodeList.length -1
Set clientid = NodeList(i).selectSingleNode("./clientid")
Set name = NodeList(i).selectSingleNode("./name")
Set ckdesc = NodeList(i).selectSingleNode("./site/*/*/failed_checks/check/description")
Set ckdev = NodeList(i).selectSingleNode("./site/*/*/name")
Response.Write clientid.text & " " & name.text & " " & ckdev.text & " " & ckdesc.text & "<br>"
Next
Set xmlDOM = Nothing
Set NodeList = Nothing
12345 Acme Inc LAPTOP1 SERVER1 Smart Error Disk Space Error - C:
67543 Contoso Ltd LAPTOP1 SERVER1 Smart Error Disk Space Error - C:
Dim xmlDOM
设置xmlDOM=Server.CreateObject(“MSXML2.DOMDocument”)
LoadXml(sXml)
Dim节点列表,i,clientid,name,ckdesc,ckdev
设置NodeList=xmlDom.selectNodes(“/result/items/client”)
对于i=0到NodeList.length-1
设置clientid=NodeList(i)。选择SingleNode(“./clientid”)
集合名称=节点列表(i)。选择单节点(“./名称”)
设置ckdesc=NodeList(i)。选择SingleNode(“./site/*/*/failed\u checks/check/description”)
设置ckdev=NodeList(i)。选择SingleNode(“./site/*/*/name”)
响应。写入clientid.text&&name.text&&ckdev.text&&ckdesc.text&&br>“
下一个
设置xmlDOM=Nothing
设置节点列表=无
此代码并不理想,因为在获取属性值
clientid.text
和其他之前,您需要检查对象是否存在(不是空的),并进行更多的错误检查。我正在向VMVs应答添加一个应答,以便能够更容易地显示我的代码和输出
VMV,你建议的代码工作得很好!直到看到您的工作代码的输出,我才看到整个过程。
现在的问题是,每个
可以有多个
,每个
可以有多个
或者
当然有多个
如何在每个(i)
中执行循环
我已尝试执行以下代码:
设置NodeList=xmlDom.selectNodes(“/result/items/client”)
我更希望看到这个结果(如果有一台或多台计算机出现单个或多个错误):
所以我的问题是,我如何在循环中循环
在我的代码中有一些明显的错误,我设置了NodeList2和NodeList3,因为这会再次通过XML文件,这就是为什么我会得到错误的输出
我也尝试过这样做(基于VMVs代码)
Set clientid=NodeList(i)。选择SingleNode(“./clientid”)
集合名称=节点列表(i)。选择单节点(“./名称”)
设置ckdev=NodeList(i)。选择节点(“./site/*/*/name”)
对于ckdev中的每个设备
若并没有,那个么这个设备什么都不是
strckdev=strckdev&device&“
其他的
strckdev=strckdev&“
”
如果结束
下一个
斯林特,对不起,我无法完成您所有的工作,但我认为此算法将帮助您解决问题:
Dim nodeListClient, nodeClient
Dim nodeListSite, nodeSite
Dim nodeListMachine, nodeMachine
' getting multiply <client> nodes
Set nodeListClient = xmlDom.selectNodes("/result/items/client")
' looping through collection
For Each nodeClient In nodeListClient
' getting multiply <site> nodes
Set nodeListSite = nodeClient.selectNodes("./site")
' looping through collection
For Each nodeSite In nodeListSites
' getting multiply <workstation> or <server> nodes
Set nodeListMachine = nodeSite.selectNodes("./*/*")
' looping through collection
For Each nodeMachine In nodeListMachine
' there you will working with nodeMachine object
' nodeMachine contains <id>, <name> and other nodes
Next
Next
Next
Dim nodeListClient,nodeClient
暗点状斜长岩,点状斜长岩
弱节点机器
“获取多个节点
设置nodeListClient=xmlDom.selectNodes(“/result/items/client”)
"循环收集",
对于nodeListClient中的每个nodeClient
“获取多个节点
设置nodeListSite=nodeClient。选择节点(“./site”)
"循环收集",
对于节点列表站点中的每个节点站点
'获取多个或多个节点
设置nodeListMachine=nodeSite.selectNodes(“./*/*”)
"循环收集",
对于nodeListMachine中的每个nodeMachine
'在那里,您将使用nodeMachine对象
'n
12345 Acme Inc
LAPTOP1 Smart Error
LAPTOP3 Some other error
Second error
67543 Contoso Ltd
SERVER1 Disk Space Error - C:
Server7 Some other error
Set clientid = NodeList(i).selectSingleNode("./clientid")
Set name = NodeList(i).selectSingleNode("./name")
Set ckdev = NodeList(i).selectNodes("./site/*/*/name")
For Each device In ckdev
If Not device Is Nothing Then
strckdev = strckdev & device & " "
Else
strckdev = strckdev & "<br>"
End If
Next
Dim nodeListClient, nodeClient
Dim nodeListSite, nodeSite
Dim nodeListMachine, nodeMachine
' getting multiply <client> nodes
Set nodeListClient = xmlDom.selectNodes("/result/items/client")
' looping through collection
For Each nodeClient In nodeListClient
' getting multiply <site> nodes
Set nodeListSite = nodeClient.selectNodes("./site")
' looping through collection
For Each nodeSite In nodeListSites
' getting multiply <workstation> or <server> nodes
Set nodeListMachine = nodeSite.selectNodes("./*/*")
' looping through collection
For Each nodeMachine In nodeListMachine
' there you will working with nodeMachine object
' nodeMachine contains <id>, <name> and other nodes
Next
Next
Next
Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False
xmlDOM.setProperty "ServerHTTPRequest", True
xmlDOM.Load("XMLfiletoload")
Dim nodeListClient, nodeClient
Dim nodeListSite, nodeSite
Dim nodeListMachine, nodeMachine
Dim nodeError, nodeErrorList
' getting multiply <client> nodes
Set nodeListClient = xmlDom.selectNodes("/result/items/client")
' looping through collection
For Each nodeClient In nodeListClient
Set clientid = nodeClient.selectSingleNode("./clientid")
Set name = nodeClient.selectSingleNode("./name")
' getting multiply <site> nodes
strCLIENT = clientid.text & " " & name.text & "<br>"
Set nodeListSite = nodeClient.selectNodes("./site")
' looping through collection
For Each nodeSite In nodeListSite
Set cksite = nodeSite.selectSingleNode("./name")
If Not cksite Is Nothing Then
strSITE = cksite.text & " "
Else
strSITE = " "
End If
' getting multiply <workstation> or <server> nodes
Set nodeListMachine = nodeSite.selectNodes("./*/*")
' looping through collection
For Each nodeMachine In nodeListMachine
' there you will working with nodeMachine object
' nodeMachine contains <id>, <name> and other nodes
Set ckdev = nodeMachine.selectSingleNode("./name")
If Not ckdev Is Nothing Then
strONEDEVICE = ckdev.text & " "
Else
strONEDEVICE = ""
End If
Set nodeErrorList = nodeMachine.selectNodes("./failed_checks/check")
' looping through collection
For Each nodeError In nodeErrorList
' there you will working with nodeError object
' nodeError contains <id>, <name> and other nodes
Set ckdesc = nodeError.selectSingleNode("./description")
If Not ckdesc Is Nothing Then
strERROR = strERROR & ckdesc.text & "<br>"
Else
strERROR = strERROR & "<br>"
End If
Next
strDEVICE = strDEVICE & strONEDEVICE & strERROR
Next
strSITE = strSITE & strDEVICE
Next
Response.Write strCLIENT & " " & strSITE & "<br>"
strCLIENT = ""
strSITE = ""
strONEDEVICE = ""
strDEVICE = ""
strERROR = ""
Next