2017-10-19 5 views
-2

이것은 내 코드 조각입니다.이 부분은 XML 파일을 읽고 작업의 마지막 하위 특성을 가져 오는 것입니다. 이 경우에는 C 타입을 얻고 싶습니다. 스크립트는 전체 For 루프를 건너 뛰었습니다. 검색을 몇 번 했는데도 코드에 어떤 문제가 있는지 찾을 수 없었습니다.VBScript에서 XML 노드를 반복 할 수 없습니다.

Set FSO = CreateObject("Scripting.FileSystemObject") 
Set xmlDoc = CreateObject("Microsoft.XMLDOM") 
xmlDoc.Async = "False" 
counter = 0 
xmlDoc.Load(mostrecent(i).Name) 

Set colNodes = xmlDoc.SelectNodes("/Runs/Run/Operations") 

WScript.Echo counter  '<--appears 
For Each objNode In colNodes 
    WScript.Echo counter '<--didn't appear 

    If Attr.Exists(objNode.LastChild.GetAttribute("type")) Then 
     counter = counter + 1 
     WScript.Echo counter 
    End If 
Next 

XML : 나를 위해 잘 작동 :

<Runs> 
<Run> 
    <Operations> 
    <Operation type="A"></Operation> 
    <Operation type="B"></Operation> 
    <Operation type="C"></Operation> 
    </Operations> 
</Run> 
</Runs> 
+0

의 Attr는 Dictionary 객체 인 아래의 코드를 사용할 수 있습니다 + 1. – Zephyros

+1

루프에서 출력이 전혀 표시되지 않으면 'SelectNodes'가 노드를 반환하지 않는다는 의미입니다. 이것은 대개 XML 데이터에 네임 스페이스가있을 때 발생합니다. 실제 XML에'xmlns = ...'속성 및/또는'' 노드가 있습니까? –

+0

아니요, 내 XML은 Zephyros

답변

1

부모 노드 Operations

Dim objXML, strPath, objCol 
strPath = mostrecent(i).Name 
Set objXML = CreateObject("Microsoft.XMLDOM") 
objXML.async=False 
objXML.load strPath 
strQuery = "/Runs/Run/Operations/Operation" 
Set objCol = objXML.selectNodes(strQuery)   'collection of all the <Operation> nodes 
MsgBox objCol.item(objCol.length-1).attributes.getnameditem("type").text 

업데이트의 마지막 자식의 type 속성을 얻기 위해이 코드를 사용해보십시오 :

(210)

+0

BTW 코드 스니 펫 앞에''를 추가하여 구문 강조를 고칠 수 있습니다. – Tomalak

+0

@Tomalak 제안에 감사드립니다. 업데이트 됨. – Gurman

+0

필요한 개체 : 'objCol.item (...)'오류 – Zephyros

0

그냥 = 유형으로 C를 모든 작업을 얻고 싶은 경우에, 당신은 속성 값이 사전, 카운터 내부의 값 중 하나 인 경우

Set objXML = CreateObject("MSXML2.DOMDocument.6.0") 
With objXML 
    .SetProperty "SelectionLanguage", "XPath" 
    .ValidateOnParse = True 
    .Async = False 
    .Load "C:\Users\Pankrit\Desktop\test.xml" 
End With 

Set nodesC = objXML.DocumentElement.SelectNodes("/Runs/Run/Operations/Operation[@type='C']") 
If nodesC.Length >= 1 Then 
    For Each nodeC In nodesC 
     MsgBox nodeC.NodeName 
    Next 
End If