2014-10-28 31 views
2

我有一個XML有效載荷如下傳統的ASP循環通過XML

<tsResponse> 
<sites> 
    <site id="site-id" name="site1-name" contentUrl="site1-content-url" /> 
    <projects> 
    <project id="project1-id" name="project1-name"/> 
    <project id="project2-id" name="project2-name"/> 
    </projects> 
    </site> 
    <site id="site2-id" name="site2-name" contentUrl="site2-content-url" /> 
    <projects> 
    <project id="project3-id" name="project3-name"/> 
    <project id="project4-id" name="project4-name"/> 
    </projects> 
    </site> 
</sites> 
</tsResponse> 

我可以通過每個站點循環,收集每個站點ID,名稱和的contentURL,我有以下

Set oXML = Server.CreateObject("Microsoft.XMLDOM") 
    oXML.LoadXML(postResponse) ' sXML is a variable containing the content of your XML file 

    For Each oNode In oXML.SelectNodes("/tsResponse/sites/site") 
     sID = oNode.GetAttribute("id") 
     sName = oNode.GetAttribute("name") 
     sContentURL = oNode.GetAttribute("contentUrl") 
    Next 
    Set oXML = Nothing 

但是,如何獲取每個站點中的每個項目ID和名稱?我現在需要另一個For循環還是效率太低?如何循環訪問特定屬性= X?

UPDATE

For Each oNode In oXML.SelectNodes("/tsResponse/sites/site") 
    sID = oNode.GetAttribute("id") 
    sName = oNode.GetAttribute("name") 
    sContentURL = oNode.GetAttribute("contentUrl") 
    Response.write("<h2>"&sName&"/"&sID&"/"&sContentURL&"</h2>") 
    For Each oNode In oXML.SelectNodes("/tsResponse/sites/site[@id='"&sID&"']/projects/project") 
    pID = oNode.GetAttribute("id") 
    pName = oNode.GetAttribute("name") 
    Response.write("<h3>"&pID&"/"&pName&"</h3>") 
    Next 
Next 
Set oXML = Nothing 

在第二For loop我得到

更改oNode到oNode2和它的作品!

+0

這是否工作?您在'For'循環中缺少'SelectNodes'方法中的'projects'節點... – Paul 2014-10-28 09:38:21

+0

這可以獲取網站,但我想要網站,然後是每個網站的所有項目。修改問題 – pee2pee 2014-10-28 09:39:28

回答

1
For Each oNode In oXML.SelectNodes("/tsResponse/sites/site") 
    sID = oNode.GetAttribute("id") 
    sName = oNode.GetAttribute("name") 
    sContentURL = oNode.GetAttribute("contentUrl") 
    Response.write("<h2>"&sName&"/"&sID&"/"&sContentURL&"</h2>") 
    For Each oNode2 In oXML.SelectNodes("/tsResponse/sites/site[@id='"&sID&"']/projects/project") 
    pID = oNode2.GetAttribute("id") 
    pName = oNode2.GetAttribute("name") 
    Response.write("<h3>"&pID&"/"&pName&"</h3>") 
    Next 
Next 
Set oXML = Nothing 
+2

您不能重複使用'oNode'它已經是一個循環控制變量,給它一個不同的名稱。 – Lankymart 2014-10-28 09:50:43

+0

複製/粘貼錯誤代碼 - 修復! – pee2pee 2014-10-28 09:59:31