2016-03-03 28 views
0

我在VBA改變每個循環成環

Dim userBeanList As MSXML2.IXMLDOMNodeList 
Dim userbean As MSXML2.IXMLDOMNode 
Dim beanChild As MSXML2.IXMLDOMNode 

XMLDOC.Load ("https://www.catch.api") 

r = 4 
Set userBeanList = XMLDOC.SelectNodes("/response/responseBody/responseList/item[recordType='TPI']") 
For Each userbean In userBeanList 
    For Each beanChild In userbean.ChildNodes 
    If beanChild.nodeName = "catch" Then GoTo NextIteration 

     Sheets("Sheet2").Cells(r, 1) = beanChild.nodeName 
     Sheets("Sheet2").Cells(r, 2) = beanChild.Text 

    r = r + 1 
NextIteration: 
    Next beanChild 
Next userbean 

下面的代碼目前,我公司通過所有的節點,然後在這種情況下,忽略了節點稱爲「捕獲」循環再進入下一個迭代,因爲我不需要該節點或節點值。 而不是這樣做我怎麼會改變我的循環到一個我可以直接去感興趣的節點,因此不必跳過似乎效率低下的迭代?

/////之後,從凍糕

Dim userBeanList As MSXML2.IXMLDOMNodeList 
Dim userbean As MSXML2.IXMLDOMNode 
Dim beanChild As MSXML2.IXMLDOMNode 

XMLDOC.Load ("http://www.catch.api") 

r = 4 

Set userBeanList = XMLDOC.SelectNodes("/response/responseBody/responseList/item[recordType='TPI']/*[not(local-name)='catch']") 
For Each userbean In userBeanList 

     Sheets("Sheet2").Cells(r, 1) = userbean.nodeName 

     Sheets("Sheet2").Cells(r, 2) = userbean.Text 

     r = r + 1 

Next userbean 
+0

http://stackoverflow.com/questions/24188773/xpath-to-select-all-and-exclude-child-and-its-children –

+0

它可能會更高效地說它beanChild.nodeName <>「catch」然後做東西 – justkrys

回答

1

幫助考慮XPath表達式排除。以下指定任何name()local-name()不等於'catch'的孩子。這甚至可以避免子節點上的內部For Each循環。

Set userBeanList = XMLDOC.SelectNodes("/response/responseBody/responseList" _ 
            & "/item[recordType='TPI']/*[not(local-name)='catch']") 

或者,具有self

Set userBeanList = XMLDOC.SelectNodes("/response/responseBody/responseList" _ 
            & "/item[recordType='TPI']/*[not(self::catch)]") 
+0

這看起來非常有趣,如果我有一個或,例如'catch'或'reprive',我將如何更改集合 – Ingram

+1

只需添加'或'條件:'[not(self :: catch或self :: reprive)]' – Parfait