2017-02-10 9 views
0

asp VBscriptでXMLオブジェクトに変換したいxml文字列があります。 は、それから私は、親のid属性に基づいて、子項目にアクセスしたい:ASPへの文字列とXMLによるIDへのアクセスVBScript

… 
<subset> 
<subtitle>DEMAND</subtitle> 
<information id="dat1" timestamp="2017-01-26T10:00:00.000-05:00"> 
    <info_title>Market Demand</info_title> 
    <new_val>19887.4</new_val> 
    <old_val>19584.3</old_val> 
</information> 
<information id="dat2" timestamp="2017-01-26T10:45:00.000-05:00"> 
    <info_title>5-Minute Market Demand</info_title> 
    <new_val>19742.2</new_val> 
    <old_val>19712.7</old_val> 
</information> 
<information id="dat3" timestamp="2017-01-26T10:00:00.000-05:00"> 
    <info_title>Ontario Demand</info_title> 
    <new_val>17204.7</new_val> 
    <old_val>17076.4</old_val> 
</information> 
</subset> 
… 

は、例えば、私は情報ID =」DAT2” new_val値を取得したいです。

function getXMLValue(strXMLfile, XMLelement, infoID, XMLattrib) 

    'Declare local variables 
    Dim objXML, return_value 
    return_value = null 

    'Instantiate the XMLDOM Object that will hold the XML file. 
    set objXML = Server.CreateObject("Microsoft.XMLDOM") 

    'Turn off asyncronous file loading. 
    objXML.async = false 

    objXML.LoadXml(strXMLFile) 
    objXML.setProperty "SelectionLanguage", "XPath" 

    if XMLelement = "date" then 
     set return_value = objXML.selectSingleNode("date/@" & XMLattrib) 

    elseif XMLelement = "id" then 
     set return_value = objXML.selectSingleNode("subset/information[@id='" & infoID & "']/" & XMLattrib) 

    elseif XMLelement = "page_title" then 
     set return_value = objXML.selectSingleNode("page_title") 

    elseif XMLelement = "date_stamp" then 
     set return_value = objXML.selectSingleNode("date" & XMLvalue) 

    elseif XMLelement = "timestamp" then 
     set return_value = objXML.selectSingleNode("subset/information/[@id='" & infoID & "']/timestamp/@" & XMLattrib) 

    end if 

    if not(isnull(return_value)) then 
     getXMLvalue = return_value.text 
    end if 

    set return_value = nothing 
    set objXML = nothing 
end function 

このコードスニペットは、私の最初のnew_valの値が得られますが、どのように私は情報id =」DAT2” の値を取得するために指定するのですか?

答えて

2

selectSingleNode methodxpathクエリを実行できます。このような

何か:

objXML.setProperty "SelectionLanguage", "XPath" 
set objNode = objXML.selectSingleNode("/subset/information[@id='dat2']/new_val") 
if not objNode is nothing then 
    MsgBox objNode.text 
end if 
+0

HIケビン、動作するようには思えませんでした。上記のコードを更新しました。注:strXMLfileは上記で提供したXML文字列です。 – Vipresh

+0

getXMLValue関数を呼び出すコードを投稿してください。また、ここでxmlはからstrXMLfile全体か、それ以上あるのですか? –

+0

getXMLValueの例を呼び出しています。getXMLvalue(strXMLfile、 "id"、 "dat2"、 "new_val")。 strXMLfile xmlにはさらに多くのことがあります。また、xmlファイルをローカルに指すようにstrXMLfileを変更すると動作しますが、文字列として渡すのが好きです。 – Vipresh

関連する問題