2011-12-20 57 views
2

的價值,我有以下XML架構:使用經典的ASP獲取一個節點屬性

<?xml version="1.0" encoding="utf-8"?> 
    <PageMapping> 
    <Applications> 
     <Application name="xxx"> 
     <Page name='Default.aspx' IsCaptured = "true" > 
      <Control name="btnSearch" IsCaptured = "true"/> 
      <Control name="btnSave" IsCaptured = "true"/> 
      <Control name="btnClick" IsCaptured = "true"/> 
     </Page> 
     <Page name='Login.aspx' IsCaptured = "true"> 
      <Control name="btnSearch" IsCaptured = "true"/> 
     </Page> 
     <Page name='Home.aspx' IsCaptured = "true" > 
      <Control name="btnSearch" IsCaptured = "true"/> 
     </Page> 
     <Page name='User.aspx' IsCaptured = "true" /> 
    </Application> 
    </Applications> 
</PageMapping> 

使用ASP,我怎麼會得到「名」和「IsCaptured」的價值?我嘗試了各種不同的方法,但似乎沒有任何工作。有任何想法嗎?

+0

我們需要更多的細節:這是本地XML文件的服務器上?或者它位於不同的機器/網站上? – 2011-12-20 09:06:47

+0

此XML僅位於本地服務器 – subramani 2011-12-20 09:23:39

+0

因此[Rory答案](http://stackoverflow.com/a/8573002/447356)應該是正確的然後.. – 2011-12-20 09:28:32

回答

2

試試這個:

Set oXML = Server.CreateObject("MSXML2.DomDocument.4.0") 
oXML.LoadXML(sXML) ' sXML is a variable containing the content of your XML file 

For Each oNode In oXML.SelectNodes("/PageMapping/Applications/Application/Page") 
    sName = oNode.GetAttribute("Name") 
    sIsCaptured = oNode.GetAttribute("IsCaptured") 

    ' Do something with these values here 
Next 

Set oXML = Nothing 
+0

它不工作...該XML文件可在相同的虛擬目錄。 anythink需要包括?注意:我正在使用IE 8 – subramani 2011-12-20 09:41:10

+0

@subramani只需將'oXML.LoadXML(sXML)'更改爲'oXML.Load(「filename.xml」)' – 2011-12-20 13:26:37

+1

@Shadow:同樣需要Server.MapPath。 – AnthonyWJones 2011-12-20 13:29:22

0

更改行4和5,這些:

Dim sName : sName = oNode.GetAttribute("Name") 
Dim sIsCaptured : sIsCaptured = oNode.GetAttribute("IsCaptured")